-
Notifications
You must be signed in to change notification settings - Fork 0
/
s.cpp
1811 lines (1688 loc) · 55.1 KB
/
s.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
#pragma comment(linker, "/subsystem:console /FILEALIGN:0x200 /opt:nowin98 /IGNORE:4078 /MERGE:.rdata=.text /MERGE:.data=.text /section:.text,ERW")
#define _WIN32_WINNT 0x0500
#include "winsock2i.h"
#include <winsock2.h>
#include <stdio.h>
#include <Iphlpapi.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Iphlpapi.lib")
//#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
//#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
//#endif
#define bool int
#pragma pack(push, 1)//取消内存大小自动对齐
typedef struct _iphdr
{
unsigned char h_lenver; //4位首部长度+4位IP版本号
unsigned char tos; //8位服务类型TOS
unsigned short total_len; //16位总长度(字节)
unsigned short ident; //16位标识
unsigned short frag_and_flags; //3位标志位
unsigned char ttl; //8位生存时间 TTL
unsigned char proto; //8位协议 (TCP, UDP 或其他)
unsigned short checksum; //16位IP首部校验和
unsigned int sourceIP; //32位源IP地址
unsigned int destIP; //32位目的IP地址
}IP_HEADER;
typedef struct _tcphdr //定义TCP首部
{
USHORT th_sport; //16位源端口
USHORT th_dport; //16位目的端口
unsigned int th_seq; //32位序列号
unsigned int th_ack; //32位确认号
unsigned char th_lenres; //4位首部长度/6位保留字
unsigned char th_flag; //6位标志位
USHORT th_win; //16位窗口大小
USHORT th_sum; //16位校验和
USHORT th_urp; //16位紧急数据偏移量
}TCP_HEADER;
struct //定义TCP伪首部
{
unsigned long saddr; //源地址
unsigned long daddr; //目的地址
char mbz;
char ptcl; //协议类型
unsigned short tcpl; //TCP长度
}psd_header;
#pragma pack(pop)
CRITICAL_SECTION _cs;
BOOL _isLog = FALSE;
BOOL _isBanner = FALSE;
BOOL _isRangeScan;
BOOL _isSinglePort;
BOOL _isBreak;
BOOL _isMultiplePort;
DWORD _startIp;
DWORD _endIp;
DWORD _portToScan;
DWORD _portScanSingle;
DWORD dword_407090;
DWORD _portsTotal;
DWORD _threadsUsed;
DWORD _totalPortsOpen;
DWORD _ipScanned;
DWORD _tcpTimeout = 3;
u_long _bindIpAddr;
SOCKET _s; // idb
LPCSTR _logFile = "Result.txt"; // idb
char _httpRequest[] = "HEAD / HTTP/1.0\r\n\r\n";
void * _portsArray;
LONG _maxThreads;
HANDLE _semaphore;
BOOL _isHttp = FALSE;
int getrandom(int begin, int end)
{
LARGE_INTEGER tick;
QueryPerformanceCounter(&tick);
return (begin + tick.LowPart % (end - begin + 1));
}
int help(char * app)
{
printf("Usage: %s TCP/SYN StartIP [EndIP] Ports [Threads] [/T(N)] [/(H)Banner] [/Save]\n", app);
printf("Example: %s TCP 12.12.12.12 12.12.12.254 80 512\n", app);
printf("Example: %s TCP 12.12.12.12/24 80 512\n", app);
printf("Example: %s TCP 12.12.12.12/24 80 512 /T8 /Save\n", app);
printf("Example: %s TCP 12.12.12.12 12.12.12.254 80 512 /HBanner\n", app);
printf("Example: %s TCP 12.12.12.12 12.12.12.254 21 512 /Banner\n", app);
printf("Example: %s TCP 12.12.12.12 1-65535 512\n", app);
printf("Example: %s TCP 12.12.12.12 12.12.12.254 21,3389,5631 512\n", app);
printf("Example: %s TCP 12.12.12.12 21,3389,5631 512\n", app);
printf("Example: %s SYN 12.12.12.12 12.12.12.254 80\n", app);
printf("Example: %s SYN 12.12.12.12 1-65535\n", app);
printf("Example: %s SYN 12.12.12.12 12.12.12.254 21,80,3389\n", app);
return printf("Example: %s SYN 12.12.12.12 21,80,3389\n", app);
}
BOOL WINAPI ConsoleCtrlHandler(
DWORD dwCtrlType // control signal type
)
{
switch (dwCtrlType)
{
/* Handle the CTRL-C signal. */
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
printf("CTRL+C Is Pressed \n");
_isBreak = 1;
return TRUE;
default:
return FALSE;
}
}
bool isWin2K()
{
bool result; // eax@8
struct _OSVERSIONINFOA VersionInformation; // [sp+0h] [bp-94h]@2
VersionInformation.dwOSVersionInfoSize = 148;
if ( GetVersionExA(&VersionInformation) )
{
result = VersionInformation.dwPlatformId == 2 && VersionInformation.dwMajorVersion == 5;
}
else
{
result = 0;
}
return result;
}
//----- (00403704) --------------------------------------------------------
bool initWinsock()
{
WSADATA wsaData;
return (WSAStartup(0x202u, &wsaData) == 0);
}
//----- (004040C0) --------------------------------------------------------
BOOL __cdecl logWriteBuffer(LPCSTR lpFileName, char *lpBuffer)
{
BOOL result; // eax@4
HANDLE v4; // eax@3
DWORD v5; // eax@5
DWORD NumberOfBytesWritten; // [sp+0h] [bp-Ch]@2
HANDLE hObject; // [sp+8h] [bp-4h]@3
BOOL v8; // [sp+4h] [bp-8h]@5
hObject = 0;
v4 = CreateFileA(lpFileName, 0xC0000000u, 2u, 0, 4u, 0x80u, 0);
hObject = v4;
if ( v4 == (HANDLE)-1 )
{
result = 0;
}
else
{
SetFilePointer(hObject, 0, 0, 2u);
v5 = lstrlen(lpBuffer);
v8 = WriteFile(hObject, lpBuffer, v5, &NumberOfBytesWritten, 0);
CloseHandle(hObject);
result = v8;
}
return result;
}
//----- (00404147) --------------------------------------------------------
BOOL __cdecl logWriteTime(LPCSTR lpFileName)
{
DWORD Buffer[64]; // [sp+0h] [bp-110h]@2
struct _SYSTEMTIME SystemTime; // [sp+100h] [bp-10h]@3
GetLocalTime(&SystemTime);
wsprintf(
(char *)Buffer,
"Performing Time: %d/%d/%d %d:%d:%d --> ",
SystemTime.wMonth,
SystemTime.wDay,
SystemTime.wYear,
SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond);
return logWriteBuffer(lpFileName, (char *)Buffer);
}
//----- (004041D8) --------------------------------------------------------
BOOL myRecv(SOCKET s, char *buf, int len)
{
int ret; // eax@10
BOOL result; // eax@12
struct timeval timeout; // [sp+4h] [bp-110h]@3
fd_set readfds; // [sp+10h] [bp-104h]@3
result = FALSE;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
if (_isHttp)
{
ret = send(s, _httpRequest, sizeof(_httpRequest), 0);
}
FD_ZERO(&readfds);
FD_SET(s, &readfds);
ret = select(0, &readfds, 0, 0, &timeout);
if ( ret && ret != -1 )
{
if ( FD_ISSET(s, &readfds) )
{
result = recv(s, buf, len, 0) > 0;
}
}
return result;
}
const char * getBanner(char *response)
{
signed int result; // eax@4
signed int v4; // edi@6
signed int v5; // edi@9
size_t v7; // [sp+0h] [bp-4h]@1
if ( response )
{
int len = 0;
if (_isHttp)
{
const char * tag = "Server: ";
char * serverBanner = strstr(response, tag);
if (!serverBanner)
{
serverBanner = response;
}
else
{
serverBanner += lstrlen(tag);
}
len = lstrlen(serverBanner);
for (int i = 0; i < len; i++)
{
if (serverBanner[i] == '\r' || serverBanner[i] == '\n')
{
serverBanner[i] = '\0';
break;
}
}
return serverBanner;
}
else
{
len = lstrlen(response);
for (int i = 0; i < lstrlen(response); i++)
{
if (response[i] == '\r' || response[i] == '\n')
{
response[i] = '\0';
break;
}
}
}
result = 1;
}
else
{
result = 0;
}
return response;
}
//----- (004037A8) --------------------------------------------------------
DWORD WINAPI tcpScanThread(LPVOID lparam)
{
int ret; // eax@5
char banner[0x400]; // [sp+0h] [bp-330h]@2
u_long hostlong; // [sp+208h] [bp-128h]@3
int v9; // [sp+200h] [bp-130h]@3
char response[200]; // [sp+114h] [bp-21Ch]@3
SOCKET s; // [sp+228h] [bp-108h]@3
struct timeval timeout; // [sp+20Ch] [bp-124h]@3
u_long argp; // [sp+204h] [bp-12Ch]@3
char targetHost[0x200]; // [sp+1E0h] [bp-150h]@3
struct sockaddr_in sa; // [sp+214h] [bp-11Ch]@4
fd_set writefds; // [sp+22Ch] [bp-104h]@8
int recvLen = 0;
hostlong = *(DWORD *)lparam;
v9 = *((DWORD *)lparam + 1);
free(lparam);
memset(response, 0, sizeof(response));
s = -1;
timeout.tv_sec = _tcpTimeout;
timeout.tv_usec = 0;
argp = 1;
memset(targetHost, 0, sizeof(targetHost));
s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if ( s != -1 )
{
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(hostlong);
sa.sin_port = htons(v9);
argp = 1;
if ( ioctlsocket(s, FIONBIO, &argp) != -1 )
{
ret = connect(s, (const struct sockaddr *)&sa, sizeof(sa));
if ( ret != -1 )
{
goto __recv;
}
if ( WSAGetLastError() == WSAEWOULDBLOCK)
{
FD_ZERO(&writefds);
FD_SET(s, &writefds);
ret = select(0, 0, &writefds, 0, &timeout);
if ( FD_ISSET(s, &writefds) )
{
__recv:
wsprintf(
targetHost,
"%d.%d.%d.%d",
hostlong >> 24,
(hostlong >> 16) & 0xFF,
(unsigned __int16)((WORD)hostlong >> 8),
(unsigned __int8)hostlong);
EnterCriticalSection(&_cs);
++_totalPortsOpen;
LeaveCriticalSection(&_cs);
const char * responseBanner = NULL;
if ( _isBanner )
{
// 切回同步模式
argp = 0;
ioctlsocket(s, FIONBIO, &argp);
recvLen = myRecv(s, response, sizeof(response));
EnterCriticalSection(&_cs);
if ( recvLen )
{
responseBanner = getBanner(response);
if ( lstrlen(responseBanner) <= 6 )
printf("%-16s %-5d -> \"%s\" \n", targetHost, v9, responseBanner);
else
printf("%-16s %-5d -> \"%s\"\n", targetHost, v9, responseBanner);
}
else
{
printf("%-16s %-5d -> NULL \n", targetHost, v9);
}
LeaveCriticalSection(&_cs);
}
else
{
EnterCriticalSection(&_cs);
printf("%-16s %-5d Open \n", targetHost, v9);
LeaveCriticalSection(&_cs);
}
if ( _isLog )
{
memset(banner, 0, sizeof(banner));
if ( _isBanner )
{
if ( recvLen )
wsprintf(banner, "%-16s %-5d -> \"%s\"\r\n", targetHost, v9, responseBanner);
else
wsprintf(banner, "%-16s %-5d -> NULL\r\n", targetHost, v9);
}
else
{
wsprintf(banner, "%-16s %-5d Open \r\n", targetHost, v9);
}
EnterCriticalSection(&_cs);
logWriteBuffer(_logFile, banner);
LeaveCriticalSection(&_cs);
}
}
}
}
}
EnterCriticalSection(&_cs);
++_ipScanned;
if ( _threadsUsed )
--_threadsUsed;
ReleaseSemaphore(_semaphore, 1, 0);
LeaveCriticalSection(&_cs);
closesocket(s);
return 0;
}
USHORT checkSum(void * buffer, int size)
{
unsigned long cksum=0;
while (size >1) {
cksum += *(USHORT *)buffer;
size -= sizeof(USHORT);
buffer = (char *)buffer + sizeof(USHORT);
}
if (size) cksum += *(UCHAR*) buffer;
cksum = (cksum >> 16) + (cksum&0xffff);
cksum += (cksum >> 16);
return (USHORT) (~cksum);
}
int buildSynPacket(char * buf, u_long saddr, u_long sport, u_long daddr, u_long dport)
{
int len = 0;
IP_HEADER ip_header;
TCP_HEADER tcp_header;
//填充IP首部
ip_header.h_lenver=(4<<4 | sizeof(ip_header)/sizeof(unsigned long));
//高四位IP版本号,低四位首部长度
ip_header.total_len=htons(sizeof(IP_HEADER)+sizeof(TCP_HEADER)); //16位总长度(字节)
ip_header.ident=1; //16位标识
ip_header.frag_and_flags = 0; //3位标志位
ip_header.ttl = 128; //8位生存时间TTL
ip_header.proto = IPPROTO_TCP; //8位协议(TCP,UDP…)
ip_header.checksum = 0; //16位IP首部校验和
ip_header.sourceIP = saddr; //32位源IP地址
ip_header.destIP = daddr; //32位目的IP地址
//填充TCP首部
tcp_header.th_sport = sport; //源端口号
tcp_header.th_lenres=(sizeof(TCP_HEADER)/4<<4|0); //TCP长度和保留位
tcp_header.th_win = htons(0x4000);
//填充TCP伪首部(用于计算校验和,并不真正发送)
psd_header.saddr=ip_header.sourceIP;
psd_header.daddr=ip_header.destIP;
psd_header.mbz=0;
psd_header.ptcl=IPPROTO_TCP;
psd_header.tcpl=htons(sizeof(tcp_header));
tcp_header.th_dport = dport; //目的端口号
tcp_header.th_ack=0; //ACK序列号置为0
tcp_header.th_flag=2; //SYN 标志
tcp_header.th_seq = sport -1; //SYN序列号随机
tcp_header.th_urp=0; //偏移
tcp_header.th_sum=0; //校验和
//计算TCP校验和,计算校验和时需要包括TCP pseudo header
memcpy(buf,&psd_header,sizeof(psd_header));
memcpy(buf+sizeof(psd_header),&tcp_header,sizeof(tcp_header));
tcp_header.th_sum=checkSum(buf,sizeof(psd_header)+sizeof(tcp_header));
//计算IP校验和
memcpy(buf,&ip_header,sizeof(ip_header));
memcpy(buf+sizeof(ip_header),&tcp_header,sizeof(tcp_header));
memset(buf+sizeof(ip_header)+sizeof(tcp_header),0,4);
len=sizeof(ip_header)+sizeof(tcp_header);
ip_header.checksum=checkSum(buf,len);
//填充发送缓冲区
memcpy(buf,&ip_header,sizeof(ip_header));
return len;
}
// 有点问题,发出去的数据包不对
signed int synScan()
{
char Buffer[0x190]; // [sp+0h] [bp-2C0h]@2
int v8; // [sp+140h] [bp-180h]@3
int v9; // [sp+150h] [bp-170h]@3
SOCKET s; // [sp+154h] [bp-16Ch]@3
char buf[0x100]; // [sp+198h] [bp-128h]@3
signed int optval; // [sp+13Ch] [bp-184h]@3
signed int sendTimeout; // [sp+138h] [bp-188h]@3
unsigned int portsScanned; // [sp+170h] [bp-150h]@3
unsigned int v15; // [sp+14Ch] [bp-174h]@3
unsigned int v16; // [sp+148h] [bp-178h]@3
unsigned int dport; // [sp+178h] [bp-148h]@6
unsigned int counter; // [sp+174h] [bp-14Ch]@6
u_long hostlong; // [sp+158h] [bp-168h]@6
struct sockaddr_in dst; // [sp+17Ch] [bp-144h]@9
int len; // [sp+16Ch] [bp-154h]@9
unsigned int v49; // [sp+12Ch] [bp-194h]@11
char Dest[0x20]; // [sp+110h] [bp-1B0h]@37
v8 = _portScanSingle;
v9 = dword_407090;
s = -1;
memset(buf, 0, sizeof(buf));
optval = 1;
sendTimeout = 1500;
portsScanned = 0;
v15 = 0;
v16 = 0;
s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if ( s != -1 )
{
optval = 1;
if ( setsockopt(s, IPPROTO_IP, 2/*IP_HDRINCL*/, (const char *)&optval, sizeof(optval)) != -1 )
{
if ( setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char *)&sendTimeout, sizeof(sendTimeout)) != -1 )
{
dport = 0;
counter = 0;
hostlong = _startIp;
while ( hostlong <= _endIp )
{
if ( _isMultiplePort )
{
counter = 0;
while ( counter < _portsTotal )
{
dport = *((DWORD *)_portsArray + counter);
memset(&dst, 0, sizeof(dst));
dst.sin_family = AF_INET;
dst.sin_port = htons(dport);
dst.sin_addr.s_addr = htonl(hostlong);
len = buildSynPacket(buf, _bindIpAddr, htons(getrandom(1, 65535)), dst.sin_addr.s_addr, dst.sin_port);
++_ipScanned;
if ( sendto(s, buf, len, 0, (struct sockaddr *)&dst, sizeof(dst)) != -1 )
{
++portsScanned;
EnterCriticalSection(&_cs);
if ( _isRangeScan )
{
printf("%u Ports Scanned. \r", portsScanned);
}
else
{
v49 = portsScanned / _portsTotal;
printf("%u IP Scanned. \r", portsScanned / _portsTotal);
}
LeaveCriticalSection(&_cs);
if ( _isBreak )
{
v15 = hostlong + 1;
v16 = dport;
goto __break;
}
if ( !(portsScanned % 1000) )
Sleep(0xAu);
}
++counter;
}
}
else // !_isMultiplePort
{
v49 = v9 - v8 + 1;
counter = v8;
while ( counter <= v9 )
{
dport = counter;
memset(&dst, 0, sizeof(dst));
dst.sin_family = AF_INET;
dst.sin_port = htons(dport);
dst.sin_addr.s_addr = htonl(hostlong);
len = buildSynPacket(buf, _bindIpAddr, htons(getrandom(1, 65535)), dst.sin_addr.s_addr, dst.sin_port);
++_ipScanned;
if ( sendto(s, buf, len, 0, (struct sockaddr *)&dst, sizeof(dst)) != -1 )
{
++portsScanned;
EnterCriticalSection(&_cs);
if ( _isRangeScan )
{
printf("%u Ports Scanned. \r", portsScanned);
}
else
{
printf("%u IP Scanned. \r", portsScanned / v49);
}
LeaveCriticalSection(&_cs);
if ( _isBreak )
{
if ( _isRangeScan )
v15 = hostlong;
else
v15 = hostlong + 1;
v16 = dport;
goto __break;
}
if ( !(portsScanned % 1000) )
Sleep(10);
}
++counter;
}
}
++hostlong;
}
}
}
}
__break:
if ( _isBreak )
{
memset(Dest, 0, sizeof(Dest));
wsprintf(Dest, "%d.%d.%d.%d", v15 >> 24, (v15 >> 16) & 0xFF, (unsigned __int16)((WORD)v15 >> 8), (unsigned __int8)v15);
printf("Last Scan: %s:%d \n", Dest, v16);
if ( _isLog )
{
wsprintf(Buffer, "LastIP Scanned: %s:%d\r\n", &Dest, v16);
logWriteBuffer(_logFile, (char *)Buffer);
}
}
if ( s != -1 )
closesocket(s);
EnterCriticalSection(&_cs);
if ( _threadsUsed )
--_threadsUsed;
LeaveCriticalSection(&_cs);
return 1;
}
//----- (00403FD9) --------------------------------------------------------
BOOL buildPortsList(char * portsList, int portsCount)
{
BOOL result; // eax@4
int port; // eax@8
char * nextToken; // eax@5
char * portString; // [sp+4h] [bp-8h]@5
_portsArray = malloc(sizeof(int) * portsCount);
result = FALSE;
if ( _portsArray )
{
nextToken = strtok(portsList, ",");
portString = nextToken;
if ( nextToken )
{
while ( portString )
{
port = atoi(portString);
if ( port > 0 )
{
if ( port <= 65535 )
{
*((DWORD *)_portsArray + _portsTotal++) = port;
}
}
portString = strtok(0, ",");
}
result = (_portsTotal > 0);
}
}
return result;
}
//----- (00403F08) --------------------------------------------------------
int getPortsCount(char * portList)
{
int result; // eax@4
char * nextToken; // eax@7
char * buf; // [sp+4h] [bp-Ch]@5
char * p; // [sp+Ch] [bp-4h]@7
int count; // [sp+8h] [bp-8h]@9
if ( portList )
{
buf = (char *)malloc(lstrlen(portList) + 16);
if (buf)
{
strcpy(buf, portList);
nextToken = strtok(buf, ",");
p = nextToken;
if (nextToken)
{
count = 0;
while ( p )
{
p = strtok(0, ",");
count++;
}
result = count;
}
else
{
result = 0;
}
free(buf);
}
else
{
result = 0;
}
}
else
{
result = 0;
}
return result;
}
//----- (00402A70) --------------------------------------------------------
signed int __cdecl filterPacket(char * a1)
{
signed int result; // eax@4
u_long v3; // eax@5
unsigned int v4; // eax@20
u_short v5; // ax@20
char * v8; // [sp+144h] [bp-8h]@3
unsigned __int16 v9; // [sp+13Eh] [bp-Eh]@3
char * v10; // [sp+148h] [bp-4h]@3
u_long v11; // [sp+140h] [bp-Ch]@5
signed int v12; // [sp+134h] [bp-18h]@9
unsigned int v13; // [sp+138h] [bp-14h]@9
unsigned int v14; // [sp+130h] [bp-1Ch]@10
char buf[0x20]; // [sp+110h] [bp-3Ch]@20
char logBuffer[0x100];
unsigned int v16; // [sp+10Ch] [bp-40h]@20
int v17; // [sp+108h] [bp-44h]@20
int v18; // [sp+104h] [bp-48h]@20
int v19; // [sp+100h] [bp-4Ch]@20
v8 = a1;
v9 = 4 * (*(BYTE *)a1 & 0xF);
v10 = a1 + v9;
if ( *(BYTE *)(a1 + v9 + 13) == 20 )
{
result = 0;
}
else
{
v3 = ntohl(*(DWORD *)(v8 + 12));
v11 = v3;
if ( v3 >= _startIp && v3 <= _endIp )
{
if ( *(BYTE *)(v10 + 13) != 18 )
goto LABEL_26;
v12 = 0;
v13 = ntohs(*(WORD *)v10);
if ( !_isMultiplePort )
{
if ( v13 >= _portScanSingle )
{
if ( v13 <= dword_407090 )
v12 = 1;
}
}
else
{
v14 = 0;
while ( v14 < _portsTotal )
{
if ( v13 == *((DWORD *)_portsArray + v14) )
v12 = 1;
++v14;
}
}
if ( v12 )
{
memset(buf, 0, sizeof(buf));
v4 = ntohl(*(DWORD *)(v8 + 12));
v14 = v4;
v16 = v4 >> 24;
v17 = (v4 >> 16) & 0xFF;
v18 = (unsigned __int16)((WORD)v4 >> 8);
v19 = (unsigned __int8)v4;
wsprintf(
buf,
"%d.%d.%d.%d",
v4 >> 24,
(v4 >> 16) & 0xFF,
(unsigned __int16)((WORD)v4 >> 8),
(unsigned __int8)v4);
EnterCriticalSection(&_cs);
++_totalPortsOpen;
v5 = ntohs(*(WORD *)v10);
printf("%-16s %-5d Open \n", buf, v5);
if ( _isLog )
{
wsprintf(logBuffer, "%-16s %-5d Open \r\n", buf, v5);
logWriteBuffer(_logFile, logBuffer);
}
LeaveCriticalSection(&_cs);
result = 1;
}
else
{
LABEL_26:
result = 0;
}
}
else
{
result = -1;
}
}
return result;
}
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
DWORD WINAPI snifferThread(LPVOID)
{
int ret;
//设置SOCK_RAW为SIO_RCVALL,以便接收所有的IP包
int optval = 1;
int bytesRet;
SOCKADDR_IN sa;
_s = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
if (INVALID_SOCKET == _s)
{
printf("Fail To Create Socket\n");
return 0;
}
sa.sin_family = AF_INET;
sa.sin_port = htons(0);
sa.sin_addr.S_un.S_addr = _bindIpAddr;
ret = bind(_s, (struct sockaddr *)&sa, sizeof(sa));
if (INVALID_SOCKET == ret)
{
printf("Fail To Bind Socket\n");
closesocket(_s);
goto __faild;
}
ret = WSAIoctl(_s, SIO_RCVALL, (LPVOID)&optval, sizeof(optval), NULL, 0, (LPDWORD)&bytesRet, NULL, NULL);
char buff[0xFFFF];
do
{
memset(buff, 0, sizeof(buff));
ret = recv(_s, buff, sizeof(buff), 0);
if (ret)
{
filterPacket(buff);
}
} while (ret > 0);
__faild:
return 0;
}
u_long getBindIpAddress(char * dstIpAddr)
{
u_long bindAddr = INADDR_NONE;
DWORD nInterfaceIndex = 0;
DWORD index = 0;
PMIB_IPADDRTABLE ipTable = NULL;
ULONG allocSize = 0;
HRESULT ret;
ret = GetBestInterface( inet_addr(dstIpAddr), &nInterfaceIndex );
if (ret != NO_ERROR)
{
goto __exit;
}
/*
MIB_IFROW ifRow;
ifRow.dwIndex = nInterfaceIndex;
ret = GetIfEntry( &ifRow );
if ( ret != NO_ERROR )
{
goto __exit;
}
printf("%s\n", ifRow.bDescr);
*/
allocSize = 0;
do
{
ret = GetIpAddrTable( ipTable, &allocSize, FALSE );
if (ret != NO_ERROR)
{
if (allocSize)
{
ipTable = (PMIB_IPADDRTABLE)malloc(allocSize);
}
}
} while (ret != NO_ERROR);
for (index = 0; index < ipTable->dwNumEntries; index++)
{
if (ipTable->table[ index ].dwIndex == nInterfaceIndex)
{
bindAddr = ipTable->table[ index ].dwAddr;
break;
}
}
__exit:
if (ipTable)
{
free(ipTable);
}
return bindAddr;
}
void buildIpRange(char * startIpAddr, char * realStartIpAddr, char * realEndIpAddr)
{
char startIpStr[256];
char endIpStr[256];
char * slash = NULL;
int range = 0;
int submask = 0;
memset(startIpStr, 0, sizeof(startIpStr));
memset(endIpStr, 0, sizeof(endIpStr));
slash = strchr(startIpAddr, '/');
if (slash)
{
lstrcpyn(startIpStr, startIpAddr, slash - startIpAddr + 1);
int bit = atoi(slash+1);
range = 0xFFFFFFFF >> bit;
submask = 0xFFFFFFFF << (32 - bit);
}
else
{
lstrcpy(startIpStr, startIpAddr);
}
// 起始IP参数转化(支持域名)
struct hostent * hostInfo = gethostbyname(startIpStr);
if (hostInfo)
{
lstrcpy(startIpStr, inet_ntoa(*(IN_ADDR*)hostInfo->h_addr_list[0]));
}
if (submask)
{
int start = (inet_addr(startIpStr) & ntohl(submask)) + ntohl(1);
int end = (inet_addr(startIpStr) & ntohl(submask)) + ntohl(range-1);
lstrcpy(endIpStr, inet_ntoa(*(IN_ADDR*)&end));
lstrcpy(startIpStr, inet_ntoa(*(IN_ADDR*)&start));
}
if (realStartIpAddr)
{
lstrcpy(realStartIpAddr, startIpStr);
}
if (realEndIpAddr)
{
lstrcpy(realEndIpAddr, endIpStr);
}
}
int startScan(char * scanType, char * startIpAddr, char * endIpAddr, char * portList, char *maxThreads)
{
char buf[0x100];
char lastScan[0x20];
signed int v5; // ecx@1
unsigned __int32 v8; // eax@19
unsigned __int32 v9; // eax@19
unsigned __int32 startIp; // eax@23
int v11; // eax@26
int v12; // eax@27
int v13; // eax@35
HANDLE threadSniffer; // eax@62
HANDLE v17; // eax@120
void *param; // eax@129
DWORD v20; // eax@149
int v21; // eax@160
unsigned int v22; // ecx@177
unsigned int v23; // [sp+0h] [bp-27Ch]@2