2016-05-07 12:42:31 +02:00
|
|
|
; ssh_transport.inc - SSH transport layer
|
|
|
|
;
|
2021-07-20 22:14:53 +02:00
|
|
|
; Copyright (C) 2016-2021 Jeffrey Amelynck
|
2016-05-07 12:42:31 +02:00
|
|
|
;
|
|
|
|
; This program is free software: you can redistribute it and/or modify
|
|
|
|
; it under the terms of the GNU General Public License as published by
|
|
|
|
; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
; (at your option) any later version.
|
|
|
|
;
|
|
|
|
; This program is distributed in the hope that it will be useful,
|
|
|
|
; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
; GNU General Public License for more details.
|
|
|
|
;
|
|
|
|
; You should have received a copy of the GNU General Public License
|
|
|
|
; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
struct ssh_packet_header
|
|
|
|
packet_length dd ? ; The length of the packet in bytes, not including 'mac' or the
|
|
|
|
; 'packet_length' field itself.
|
|
|
|
padding_length db ? ; Length of 'random padding' (bytes).
|
2016-05-07 12:42:31 +02:00
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
message_code db ? ; First byte of payload
|
|
|
|
ends
|
2016-05-07 12:42:31 +02:00
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
|
|
|
|
proc ssh_recv_packet connection, flags
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
locals
|
2016-08-10 18:20:49 +02:00
|
|
|
data_length dd ? ; Total length of packet without MAC
|
2021-07-20 22:14:53 +02:00
|
|
|
socket_error dd ?
|
2016-05-07 12:42:31 +02:00
|
|
|
endl
|
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 2, "> "
|
2016-05-07 12:42:31 +02:00
|
|
|
; Receive first block (Read length, padding length, message code)
|
2016-08-10 18:20:49 +02:00
|
|
|
mov ebx, [connection]
|
|
|
|
mov ecx, [ebx+ssh_connection.socketnum]
|
|
|
|
mov esi, [ebx+ssh_connection.rx_crypt_blocksize]
|
|
|
|
lea edx, [ebx+ssh_connection.rx_buffer]
|
|
|
|
mov edi, [flags]
|
|
|
|
mcall recv
|
2021-07-20 22:14:53 +02:00
|
|
|
mov [socket_error], ebx
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 1, "chunk = %u ", eax
|
|
|
|
mov ebx, [connection]
|
|
|
|
cmp eax, [ebx+ssh_connection.rx_crypt_blocksize]
|
|
|
|
jne .fail
|
|
|
|
|
|
|
|
; Decrypt first block
|
|
|
|
cmp [ebx+ssh_connection.rx_crypt_proc], 0
|
|
|
|
je @f
|
|
|
|
pusha
|
|
|
|
lea esi, [ebx+ssh_connection.rx_buffer]
|
|
|
|
stdcall [ebx+ssh_connection.rx_crypt_proc], [ebx+ssh_connection.rx_crypt_ctx_ptr], esi, esi
|
|
|
|
popa
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; Check data length
|
|
|
|
mov esi, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
bswap esi ; convert length to little endian
|
|
|
|
mov [ebx+ssh_connection.rx_buffer.packet_length], esi
|
|
|
|
DEBUGF 1, "packet length=%u ", esi
|
|
|
|
cmp esi, BUFFERSIZE
|
|
|
|
ja .fail ; packet is too large
|
|
|
|
|
|
|
|
; Calculate amount of remaining data
|
|
|
|
add esi, 4 ; Packet length field itself is not included in the count
|
|
|
|
sub esi, [ebx+ssh_connection.rx_crypt_blocksize] ; Already received this amount of data
|
|
|
|
add esi, [ebx+ssh_connection.rx_mac_length]
|
|
|
|
jz .got_all_data
|
|
|
|
|
|
|
|
; Receive remaining data
|
|
|
|
lea edx, [ebx+ssh_connection.rx_buffer]
|
|
|
|
add edx, [ebx+ssh_connection.rx_crypt_blocksize]
|
|
|
|
mov ecx, [ebx+ssh_connection.socketnum]
|
|
|
|
mov edi, [flags]
|
2016-05-07 12:42:31 +02:00
|
|
|
.receive_loop:
|
2016-08-10 18:20:49 +02:00
|
|
|
mcall recv
|
|
|
|
DEBUGF 1, "chunk = %u ", eax
|
2016-05-07 12:42:31 +02:00
|
|
|
cmp eax, 0
|
|
|
|
jbe .fail
|
2016-08-10 18:20:49 +02:00
|
|
|
add edx, eax
|
|
|
|
sub esi, eax
|
|
|
|
jnz .receive_loop
|
|
|
|
|
|
|
|
; Decrypt data
|
|
|
|
mov ebx, [connection]
|
|
|
|
cmp [ebx+ssh_connection.rx_crypt_proc], 0
|
|
|
|
je .decrypt_complete
|
|
|
|
mov ecx, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
add ecx, 4 ; Packet_length field itself
|
|
|
|
sub ecx, [ebx+ssh_connection.rx_crypt_blocksize] ; Already decrypted this amount of data
|
|
|
|
jz .decrypt_complete
|
|
|
|
|
|
|
|
lea esi, [ebx+ssh_connection.rx_buffer]
|
|
|
|
add esi, [ebx+ssh_connection.rx_crypt_blocksize]
|
|
|
|
.decrypt_loop:
|
|
|
|
pusha
|
|
|
|
stdcall [ebx+ssh_connection.rx_crypt_proc], [ebx+ssh_connection.rx_crypt_ctx_ptr], esi, esi
|
|
|
|
popa
|
|
|
|
add esi, [ebx+ssh_connection.rx_crypt_blocksize]
|
|
|
|
sub ecx, [ebx+ssh_connection.rx_crypt_blocksize]
|
|
|
|
jnz .decrypt_loop
|
|
|
|
.decrypt_complete:
|
|
|
|
|
|
|
|
; Authenticate message
|
|
|
|
cmp [ebx+ssh_connection.rx_mac_proc], 0
|
|
|
|
je .mac_complete
|
|
|
|
lea esi, [ebx+ssh_connection.rx_seq]
|
|
|
|
mov ecx, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
add ecx, 8 ; packet_length field itself + sequence number
|
|
|
|
lea eax, [ebx+ssh_connection.rx_mac_ctx]
|
|
|
|
mov edx, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
bswap edx ; convert length to big endian
|
|
|
|
mov [ebx+ssh_connection.rx_buffer.packet_length], edx
|
|
|
|
stdcall [ebx+ssh_connection.rx_mac_proc], eax, esi, ecx
|
|
|
|
mov edx, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
bswap edx ; convert length to little endian
|
|
|
|
mov [ebx+ssh_connection.rx_buffer.packet_length], edx
|
|
|
|
|
|
|
|
lea esi, [ebx+ssh_connection.rx_mac_ctx]
|
|
|
|
lea edi, [ebx+ssh_connection.rx_buffer]
|
|
|
|
add edi, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
add edi, 4
|
|
|
|
mov ecx, [ebx+ssh_connection.rx_mac_length]
|
|
|
|
shr ecx, 2
|
|
|
|
repe cmpsd
|
|
|
|
jne .mac_failed
|
|
|
|
.mac_complete:
|
|
|
|
inc byte[ebx+ssh_connection.rx_seq+3] ; Update sequence counter
|
|
|
|
jnc @f
|
|
|
|
inc byte[ebx+ssh_connection.rx_seq+2]
|
|
|
|
jnc @f
|
|
|
|
inc byte[ebx+ssh_connection.rx_seq+1]
|
|
|
|
jnc @f
|
|
|
|
inc byte[ebx+ssh_connection.rx_seq+0]
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; Return useful data length to the caller via eax register
|
|
|
|
.got_all_data:
|
|
|
|
mov eax, [ebx+ssh_connection.rx_buffer.packet_length]
|
|
|
|
movzx ebx, [ebx+ssh_connection.rx_buffer.padding_length]
|
2016-05-07 12:42:31 +02:00
|
|
|
sub eax, ebx
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 1, "useful data length=%u\n", eax
|
2016-05-07 12:42:31 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 3, "ssh_recv_packet failed!\n"
|
|
|
|
mov eax, -1
|
2021-07-20 22:14:53 +02:00
|
|
|
mov ebx, [socket_error]
|
2016-08-10 18:20:49 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.mac_failed:
|
|
|
|
DEBUGF 3, "ssh_recv_packet MAC failed!\n"
|
2021-07-20 22:14:53 +02:00
|
|
|
mov eax, -2
|
|
|
|
mov ebx, [socket_error]
|
2016-05-07 12:42:31 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
proc ssh_send_packet connection, buf, payload_size, flags
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
locals
|
2016-08-10 18:20:49 +02:00
|
|
|
packet_size dd ?
|
2016-05-07 12:42:31 +02:00
|
|
|
endl
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 2, "< "
|
2016-05-07 12:42:31 +02:00
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
; Pad the packet with random data
|
|
|
|
mov eax, [payload_size]
|
|
|
|
inc eax ; padding length byte
|
|
|
|
lea edx, [eax+4] ; total packet size (without padding and MAC)
|
|
|
|
mov [packet_size], edx
|
|
|
|
mov ecx, [connection]
|
2021-07-20 22:14:53 +02:00
|
|
|
mov ebx, [ecx+ssh_connection.tx_padsize]
|
2016-05-07 12:42:31 +02:00
|
|
|
dec ebx
|
|
|
|
and edx, ebx
|
|
|
|
neg edx
|
2021-07-20 22:14:53 +02:00
|
|
|
add edx, [ecx+ssh_connection.tx_padsize]
|
2016-08-10 18:20:49 +02:00
|
|
|
cmp edx, 4 ; minimum padding size
|
2016-05-07 12:42:31 +02:00
|
|
|
jae @f
|
2021-07-20 22:14:53 +02:00
|
|
|
add edx, [ecx+ssh_connection.tx_padsize]
|
2016-05-07 12:42:31 +02:00
|
|
|
@@:
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 1, "padding %u bytes ", edx
|
|
|
|
add [packet_size], edx
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
add eax, edx
|
2016-08-10 18:20:49 +02:00
|
|
|
DEBUGF 1, "total size: %u ", eax
|
2016-05-07 12:42:31 +02:00
|
|
|
bswap eax
|
2016-08-10 18:20:49 +02:00
|
|
|
lea edi, [ecx+ssh_connection.tx_buffer]
|
|
|
|
stosd ; packet_length
|
2016-05-07 12:42:31 +02:00
|
|
|
mov al, dl
|
2016-08-10 18:20:49 +02:00
|
|
|
stosb ; padding_length
|
2016-05-07 12:42:31 +02:00
|
|
|
mov esi, [buf]
|
2016-08-10 18:20:49 +02:00
|
|
|
mov ecx, [payload_size]
|
2016-05-07 12:42:31 +02:00
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov ebx, edx
|
|
|
|
mov esi, edx
|
|
|
|
and ebx, 3
|
|
|
|
jz @f
|
|
|
|
call MBRandom
|
|
|
|
mov dword[edi], eax
|
|
|
|
add edi, ebx
|
|
|
|
@@:
|
|
|
|
|
|
|
|
shr esi, 2
|
|
|
|
@@:
|
|
|
|
call MBRandom
|
|
|
|
stosd
|
|
|
|
dec esi
|
|
|
|
jnz @r
|
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
; Message authentication
|
|
|
|
mov edx, [connection]
|
|
|
|
cmp [edx+ssh_connection.tx_mac_proc], 0
|
|
|
|
je .mac_complete
|
|
|
|
; DEBUGF 1, "MAC sequence number: 0x%x\n", [edx+ssh_connection.tx_seq]
|
|
|
|
lea esi, [edx+ssh_connection.tx_seq]
|
|
|
|
mov ecx, [packet_size]
|
|
|
|
add ecx, 4 ; Sequence number length
|
|
|
|
lea eax, [edx+ssh_connection.tx_mac_ctx]
|
|
|
|
stdcall [edx+ssh_connection.tx_mac_proc], eax, esi, ecx
|
|
|
|
|
|
|
|
lea esi, [edx+ssh_connection.tx_mac_ctx]
|
|
|
|
lea edi, [edx+ssh_connection.tx_buffer]
|
|
|
|
add edi, [packet_size]
|
|
|
|
mov ecx, [edx+ssh_connection.tx_mac_length]
|
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
|
|
|
.mac_complete:
|
|
|
|
inc byte[edx+ssh_connection.tx_seq+3] ; Update sequence counter
|
|
|
|
jnc @f
|
|
|
|
inc byte[edx+ssh_connection.tx_seq+2]
|
|
|
|
jnc @f
|
|
|
|
inc byte[edx+ssh_connection.tx_seq+1]
|
|
|
|
jnc @f
|
|
|
|
inc byte[edx+ssh_connection.tx_seq+0]
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; Encrypt data
|
|
|
|
cmp [edx+ssh_connection.tx_crypt_proc], 0
|
|
|
|
je .encrypt_complete
|
|
|
|
lea esi, [edx+ssh_connection.tx_buffer]
|
|
|
|
mov ecx, [packet_size]
|
|
|
|
.encrypt_loop:
|
|
|
|
pusha
|
|
|
|
stdcall [edx+ssh_connection.tx_crypt_proc], [edx+ssh_connection.tx_crypt_ctx_ptr], esi, esi
|
|
|
|
popa
|
|
|
|
add esi, [edx+ssh_connection.tx_crypt_blocksize]
|
|
|
|
sub ecx, [edx+ssh_connection.tx_crypt_blocksize]
|
|
|
|
jnz .encrypt_loop
|
|
|
|
.encrypt_complete:
|
|
|
|
|
|
|
|
; Send the packet
|
|
|
|
mov ebx, [connection]
|
|
|
|
mov ecx, [ebx+ssh_connection.socketnum]
|
|
|
|
lea edx, [ebx+ssh_connection.tx_buffer]
|
|
|
|
mov esi, [packet_size]
|
|
|
|
add esi, [ebx+ssh_connection.tx_mac_length]
|
|
|
|
mov edi, [flags]
|
|
|
|
mcall send
|
|
|
|
|
|
|
|
DEBUGF 1, "\n"
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
endp
|
|
|
|
|