-
Notifications
You must be signed in to change notification settings - Fork 6
/
Snoop.pas
1663 lines (1539 loc) · 40.8 KB
/
Snoop.pas
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
{
Snoop.pas
Pakcet capture class component library version 2.0
This class(component) runs on WinPcap 3.01 alpha or higher environment.
made by gilgil
to contact with me : http://www.gilgil.co.kr
}
unit Snoop;
interface
uses
Windows, Messages, SysUtils, Classes, SyncObjs, Dialogs, Forms,
Winsock, Pcap, Bpf,
IpHlpApi, IpTypes,
SnoopTrace; // gilgil temp 2003.07.22
const
SNOOP_DEFAULT_SNAPLEN = 1600;
SNOOP_MAC_SIZE = 6;
type
TPcap = pcap_t;
PPcap = ppcap_t;
PCAP_PKTHDR = Pcap.pcap_pkthdr;
PPCAP_PKTHDR = ^PCAP_PKTHDR;
PPPCAP_PKTHDR = ^PPCAP_PKTHDR;
// ----------------------------------------------------------------------------
// Ethernet Header
// ----------------------------------------------------------------------------
const
PROTO_IP = $0800;
PROTO_ARP = $0806;
PROTO_XNS = $0600;
PROTO_SNMP = $814C;
PROTO_OLD_IPX = $8137;
PROTO_NOVELL = $8138;
PROTO_IPNG = $86DD;
type
SNOOPMACADDRESS = array[0 .. SNOOP_MAC_SIZE - 1] of UCHAR;
PSNOOPMACADDRESS = ^SNOOPMACADDRESS;
PPChar = ^PChar;
type
ETHERNET_HDR = packed record
Destination: SNOOPMACADDRESS;
Source: SNOOPMACADDRESS;
Protocol: WORD;
end;
PETHERNET_HDR = ^ETHERNET_HDR;
// ----------------------------------------------------------------------------
// IP Header
// ----------------------------------------------------------------------------
const
PROTO_ICMP = 1;
PROTO_TCP = 6;
PROTO_UDP = 17;
type
IP_HDR = packed record
VerLen: UCHAR;
Service: UCHAR;
Length: WORD;
Ident: WORD;
FlagOff: WORD;
TimeLive: UCHAR;
Protocol: UCHAR;
Checksum: WORD;
Source: DWORD;
Destination: DWORD;
end;
PIP_HDR = ^IP_HDR;
PPIP_HDR = ^PIP_HDR;
// ----------------------------------------------------------------------------
// ARP Header
// ----------------------------------------------------------------------------
const
ARP_REQUEST = $0001;
ARP_REPLY = $0002;
type
ARP_HDR = packed record
HardwareType: WORD;
ProtocolType: WORD;
HLen: UCHAR;
PLen: UCHAR;
Operation: WORD;
SenderHA: SNOOPMACADDRESS;
SenderIP: DWORD;
TargetHA: SNOOPMACADDRESS;
TargetIP: DWORD;
end;
PARP_HDR = ^ARP_HDR;
PPARP_HDR = ^PARP_HDR;
// ----------------------------------------------------------------------------
// ICMP Header
// ----------------------------------------------------------------------------
type
ICMP_HDR = packed record
Type_: UCHAR;
Code: UCHAR;
Checksum: WORD;
Data: PChar;
end;
PICMP_HDR = ^ICMP_HDR;
PPICMP_HDR = ^PICMP_HDR;
// ----------------------------------------------------------------------------
// TCP Header
// ----------------------------------------------------------------------------
const
TCP_FLAG_FIN = $01;
TCP_FLAG_SYN = $02;
TCP_FLAG_RST = $04;
TCP_FLAG_PSH = $08;
TCP_FLAG_ACK = $10;
TCP_FLAG_URG = $20;
type
TCP_HDR = packed record
Source: WORD;
Destination: WORD;
Seq: DWORD;
Ack: DWORD;
Off_Rsvd: UCHAR;
Rsvd_Flags: UCHAR;
Window: WORD;
Checksum: WORD;
UrgPoint: WORD;
end;
PTCP_HDR = ^TCP_HDR;
PPTCP_HDR = ^PTCP_HDR;
// ----------------------------------------------------------------------------
// UDP Header
// ----------------------------------------------------------------------------
type
UDP_HDR = packed record
Source: WORD;
Destination: WORD;
Length: WORD;
Checksum: WORD;
end;
PUDP_HDR = ^UDP_HDR;
PPUDP_HDR = ^PUDP_HDR;
// ----------------------------------------------------------------------------
// Event
// ----------------------------------------------------------------------------
TOnGetRemoteAdapterInfo = procedure (
Sender: TObject;
AdapterNames: TStringList;
AdapterDescriptions: TStringList;
var AdapterIndex: Integer) of object;
TOnCaptureEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR) of object;
TOnCaptureIPEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
IPHeader: PIP_HDR) of object;
TOnCaptureARPEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
ARPHeader: PARP_HDR) of object;
TOnCaptureICMPEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
IPHeader: PIP_HDR;
ICMPHeader: PICMP_HDR) of object;
TOnCaptureTCPEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
IPHeader: PIP_HDR;
TCPHeader: PTCP_HDR) of object;
TOnCaptureUDPEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
IPHeader: PIP_HDR;
UDPHeader: PUDP_HDR) of object;
TOnCaptureTCPDataEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
IPHeader: PIP_HDR;
TCPHeader: PTCP_HDR;
TCPData: PChar;
TCPDataLength: Integer) of object;
TOnCaptureUDPDataEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
EthernetHeader: PETHERNET_HDR;
IPHeader: PIP_HDR;
UDPHeader: PUDP_HDR;
UDPData: PChar;
UDPDataLength: Integer) of object;
TOnCaptureStatisticsEvent = procedure (
Sender: TObject;
PacketHeader: PPCAP_PKTHDR;
Packets: Int64;
Bytes: Int64) of object;
// ----------------------------------------------------------------------------
// Component
// ----------------------------------------------------------------------------
TCustomSnoop = class;
TSnoopThread = class(TThread)
protected
FSnoop: TCustomSnoop;
public
property Snoop: TCustomSnoop read FSnoop write FSnoop;
public
constructor Create(Snoop: TCustomSnoop);
destructor Destroy; override;
procedure Open;
procedure Close;
procedure Execute; override;
end;
TCustomSnoop = class(TComponent)
protected
// property
FActive: Boolean;
FAdapterIndex: Integer;
FAdapterName: String;
FAdapterDescription: String;
FAdapterNames: TStringList;
FAdapterDescriptions: TStringList;
FThreadSafe: Boolean;
FFilter: String;
FSnapLen: Integer;
FFlags: Integer;
FReadTimeOut: Integer;
FError: String;
FSourceName: String;
FOnGetRemoteAdapterInfo: TOnGetRemoteAdapterInfo;
procedure SetAdapterIndex(Value: Integer);
procedure SetAdapterName(Value: String);
procedure SetAdapterDescription(Value: String);
procedure DoOpen(Source: PChar; Auth: ppcap_rmtauth; Dev: ppcap_if_t);
procedure CreateSnoopThread;
protected
// used internally
FThread: TSnoopThread;
FPcap: PPcap;
FPacketHeader: PPCAP_PKTHDR;
FPacketData: PETHERNET_HDR;
procedure OnCaptureCB; virtual; abstract;
procedure ThreadTerminate(Sender:TObject);
procedure GetAdapterNames;
procedure GetAdapterDescriptions;
function ProcessFilter(Dev: ppcap_if_t): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open; overload;
procedure Open(Host: String; UserName: String; Password: String); overload;
function LoadFromFile(FileName: String): Boolean;
procedure Close;
public
property Active: Boolean read FActive;
property Pcap: PPcap read FPcap write FPcap;
property AdapterNames: TStringList read FAdapterNames;
property AdapterDescriptions: TStringList read FAdapterDescriptions;
property Error: String read FError write FError;
property SourceName: String read FSourceName;
published
property AdapterIndex: Integer read FAdapterIndex write SetAdapterIndex;
property AdapterName: String read FAdapterName write SetAdapterName;
property AdapterDescription: String read FAdapterDescription write SetAdapterDescription;
property ThreadSafe: Boolean read FThreadSafe write FThreadSafe;
property Filter: String read FFilter write FFilter;
property SnapLen: Integer read FSnapLen write FSnapLen;
property Flags: Integer read FFlags write FFlags;
property ReadTimeOut: Integer read FReadTimeOut write FReadTimeOut;
property OnGetRemoteAdapterInfo: TOnGetRemoteAdapterInfo read
FOnGetRemoteAdapterInfo write FOnGetRemoteAdapterInfo;
end;
TSnoop = class(TCustomSnoop)
protected
// property
FOnCapture: TOnCaptureEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCapture: TOnCaptureEvent read FOnCapture write FOnCapture;
end;
TSnoopIP = class(TCustomSnoop)
protected
// property
FOnCaptureIP: TOnCaptureIPEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCaptureIP: TOnCaptureIPEvent read FOnCaptureIP write FOnCaptureIP;
end;
TSnoopARP = class(TCustomSnoop)
protected
// property
FOnCaptureARP: TOnCaptureARPEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCaptureARP: TOnCaptureARPEvent read FOnCaptureARP write FOnCaptureARP;
end;
TSnoopICMP = class(TCustomSnoop)
protected
// property
FOnCaptureICMP: TOnCaptureICMPEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCaptureICMP: TOnCaptureICMPEvent read FOnCaptureICMP write FOnCaptureICMP;
end;
TCustomSnoopTCP = class(TCustomSnoop)
protected
// property
FOnCaptureTCP: TOnCaptureTCPEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
end;
TSnoopTCP = class(TCustomSnoopTCP)
published
property OnCaptureTCP: TOnCaptureTCPEvent read FOnCaptureTCP write FOnCaptureTCP;
end;
TSnoopUDP = class(TCustomSnoop)
protected
// property
FOnCaptureUDP: TOnCaptureUDPEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCaptureUDP: TOnCaptureUDPEvent read FOnCaptureUDP write FOnCaptureUDP;
end;
TSnoopTCPData = class(TCustomSnoop)
protected
// property
FOnCaptureTCPData: TOnCaptureTCPDataEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCaptureTCPData: TOnCaptureTCPDataEvent read FOnCaptureTCPData write FOnCaptureTCPData;
end;
TSnoopUDPData = class(TCustomSnoop)
protected
// property
FOnCaptureUDPData: TOnCaptureUDPDataEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
published
property OnCaptureUDPData: TOnCaptureUDPDataEvent read FOnCaptureUDPData write FOnCaptureUDPData;
end;
TSnoopStatistics = class(TCustomSnoop)
protected
// property
FOnCaptureStatistics: TOnCaptureStatisticsEvent;
protected
// used internally
procedure OnCaptureCB; override;
public
constructor Create(AOwner: TComponent); override;
procedure Open;
published
property OnCaptureStatistics: TOnCaptureStatisticsEvent read FOnCaptureStatistics write FOnCaptureStatistics;
end;
TSnoopDump = class(TComponent)
protected
FActive: Boolean;
FError: String;
FPcapDumper: ppcap_dumper_t;
public
property Active: Boolean read FActive;
property Error: String read FError;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open(Pcap: PPcap; FileName: String);
procedure Close;
procedure Write(PacketHeader: PPCAP_PKTHDR; Data: PChar);
end;
TSnoopMyNetwork = class(TComponent)
protected
// used internally
FSnoop: TSnoop;
public
// property
IP: DWORD;
Mac: SNOOPMACADDRESS;
SubnetMask: DWORD;
Gateway: DWORD;
protected
function GetAdapterIndex: Integer;
procedure SetAdapterIndex(Value: Integer);
function GetAdapterName: String;
function GetAdapterDescription: String;
procedure SetAdapterName(Value: String);
procedure SetAdapterDescription(Value: String);
function GetAdapterNames: TStringList;
function GetAdapterDescriptions: TStringList;
function GetIP: String;
function GetMac: String;
function GetGateway: String;
function GetSubnetMask: String;
procedure SetIP(Value: String);
procedure SetMac(Value: String);
procedure SetGateway(Value: String);
procedure SetSubnetMask(Value: String);
public
property AdapterNames: TStringList read GetAdapterNames;
property AdapterDescriptions: TStringList read GetAdapterDescriptions;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Refresh;
published
property AdapterIndex: Integer read GetAdapterIndex write SetAdapterIndex;
property AdapterName: String read GetAdapterName write SetAdapterName;
property AdapterDescription: String read GetAdapterDescription write SetAdapterDescription;
property StrIP: String read GetIP write SetIP;
property StrMac: String read GetMac write SetMac;
property StrSubnetMask: String read GetSubnetMask write SetSubnetMask;
property StrGateway: String read GetGateway write SetGateway;
end;
// ----------------------------------------------------------------------------
// General Functions
// ----------------------------------------------------------------------------
function snoopIsIP(EthernetHeader: PETHERNET_HDR; IPHeader: PPIP_HDR = nil): Boolean;
function snoopIsARP(EthernetHeader: PETHERNET_HDR; ARPHeader: PPARP_HDR= nil): Boolean;
function snoopIsICMP(IPHeader: PIP_Hdr; ICMPHeader: PPICMP_HDR = nil): Boolean;
function snoopIsTCP(IPHeader: PIP_HDR; TCPHeader: PPTCP_HDR = nil): Boolean;
function snoopIsUDP(IPHeader: PIP_HDR; UDPHeader: PPUDP_HDR = nil): Boolean;
function snoopIsTCPData(IPHeader: PIP_HDR; TCPHeader: PTCP_HDR; TCPData: PPChar = nil; TCPDataLength: PInteger = nil): Boolean;
function snoopIsUDPData(IPHeader: PIP_HDR; UDPHeader: PUDP_HDR; UDPData: PPChar = nil; UDPDataLength: PInteger = nil): Boolean;
function snoopMac2Str(Mac: SNOOPMACADDRESS): String;
function snoopMac2StrFormat(Mac: SNOOPMACADDRESS; Format: String): String; // by gilgil 2003.10.28
function snoopIP2Str(IP: DWORD): String;
function snoopStr2Mac(s: String): SNOOPMACADDRESS;
function snoopStr2IP(s: String): DWORD;
function snoopCleanMac: SNOOPMACADDRESS;
function snoopIsCleanMac(Mac: PSNOOPMACADDRESS): Boolean; overload;
function snoopCompareMac(Mac1: PSNOOPMACADDRESS; Mac2: PSNOOPMACADDRESS): Integer;
function snoopIPChecksum(IPHeader: PIP_HDR): WORD;
function snoopTCPChecksum(IPHeader: PIP_HDR; TCPHeader: PTCP_HDR): WORD;
function snoopUDPChecksum(IPHeader: PIP_HDR; UDPHeader: PUDP_HDR): WORD;
function snoopSendPacket(Pcap: PPcap; Buffer: PChar; Size: Integer): Integer;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Snoop', [
TSnoop,
TSnoopIP,
TSnoopARP,
TSnoopICMP,
TSnoopTCP,
TSnoopUDP,
TSnoopTCPData,
TSnoopUDPData,
TSnoopStatistics,
TSnoopDump,
TSnoopMyNetwork]);
end;
// ----------------------------------------------------------------------------
// TSnoopThread Class
// ----------------------------------------------------------------------------
constructor TSnoopThread.Create(Snoop: TCustomSnoop);
begin
FSnoop := Snoop;
inherited Create(true);
end;
destructor TSnoopThread.Destroy;
begin
inherited;
end;
procedure TSnoopThread.Open;
begin
Resume;
end;
procedure TSnoopThread.Close;
begin
Terminate;
WaitFor;
end;
procedure TSnoopThread.Execute;
var
i: Integer;
Pcap: PPcap;
PacketHeader: PPCAP_PKTHDR;
PacketData: PETHERNET_HDR;
Snoop: TCustomSnoop;
begin
Snoop := FSnoop; // use local variable for execute optimization
if Snoop = nil then
exit;
Pcap := Snoop.Pcap;
while not Terminated do
begin
i := pcap_next_ex(Pcap, @PacketHeader, @PacketData);
if Terminated or Application.Terminated then break;
case i of
-1: begin// if an error occurred
FSnoop.FError := 'Snoop Error: pcap_next_ex return -1';
break;
end;
0: continue; // if the timeout set with pcap_open_live() has elapsed.
end;
Snoop.FPacketHeader := PacketHeader;
Snoop.FPacketData := PacketData;
if Snoop.ThreadSafe then
Synchronize(Snoop.OnCaptureCB)
else
Snoop.OnCaptureCB;
end;
end;
// ----------------------------------------------------------------------------
// TCustomSnoop Class
// ----------------------------------------------------------------------------
{
procedure TCustomSnoop.OnCaptureCB;
begin
end;
}
procedure TCustomSnoop.ThreadTerminate(Sender:TObject);
begin
FActive := false;
pcap_close(FPcap);
end;
procedure TCustomSnoop.GetAdapterNames;
var
i: Integer;
Dev: ppcap_if_t;
AllDevs: ppcap_if_t;
ErrBuf: array[0 .. PCAP_ERRBUF_SIZE - 1] of Char;
begin
if AdapterNames = nil then exit;
AdapterNames.Capacity:= 10;
AdapterNames.Sorted:= false;
i := pcap_findalldevs(@AllDevs, ErrBuf);
if i <> 0 then // if error occured
begin
Error := ErrBuf;
exit;
end;
Dev := AllDevs;
while Dev <> nil do
begin
AdapterNames.Add(Dev^.name);
Dev := Dev^.next;
end;
FError := '';
pcap_freealldevs(AllDevs);
end;
procedure TCustomSnoop.GetAdapterDescriptions;
var
i: Integer;
Dev: ppcap_if_t;
AllDevs: ppcap_if_t;
ErrBuf: array[0 .. PCAP_ERRBUF_SIZE - 1] of Char;
begin
if AdapterDescriptions = nil then exit;
AdapterDescriptions.Capacity:= 10;
AdapterDescriptions.Sorted:= false;
i := pcap_findalldevs(@AllDevs, ErrBuf);
if i <> 0 then // if error occured
begin
Error := ErrBuf;
exit;
end;
Dev := AllDevs;
while Dev <> nil do
begin
AdapterDescriptions.Add(Dev^.description);
Dev := Dev^.next;
end;
FError := '';
pcap_freealldevs(AllDevs);
end;
function TCustomSnoop.ProcessFilter(Dev: ppcap_if_t): Boolean;
var
NetMask: u_int;
FCode: bpf_program;
begin
Result := false;
if (Dev <> nil) and (Dev^.address <> nil) then
NetMask := Dev^.address.netmask.sin_addr.S_addr
else
NetMask := $FFFFFFFF;
if pcap_compile(FPcap, @FCode, PChar(FFilter), 1, NetMask) < 0 then
begin
FError := String(pcap_geterr(FPcap));
exit;
end;
if pcap_setfilter(FPcap, @FCode) < 0 then
begin
FError := String(pcap_geterr(FPcap));
exit;
end;
Result := true;
end;
procedure TCustomSnoop.SetAdapterIndex(Value: Integer);
begin
if Value = FAdapterIndex then
exit;
if (Value < 0) or (Value >= FAdapterNames.Count) then
begin
FAdapterIndex := -1;
FAdapterName := '';
end else
begin
FAdapterIndex := Value;
FAdapterName := FAdapterNames[Value];
FAdapterDescription := FAdapterDescriptions[Value];
end;
end;
procedure TCustomSnoop.SetAdapterName(Value: String);
var
i: Integer;
begin
if Value = FAdapterName then
exit;
for i := 0 to FAdapterNames.Count -1 do
begin
if Value = FAdapterNames[i] then
begin
FAdapterIndex := i;
FAdapterName := FAdapterNames[i];
FAdapterDescription := FAdapterDescriptions[i];
exit;
end;
end;
FAdapterIndex := -1;
FAdapterName := '';
FAdapterDescription := '';
end;
procedure TCustomSnoop.SetAdapterDescription(Value: String);
var
i: Integer;
begin
if Value = FAdapterDescription then
exit;
for i := 0 to FAdapterDescriptions.Count -1 do
begin
if Value = FAdapterDescriptions[i] then
begin
FAdapterIndex := i;
FAdapterName := FAdapterNames[i];
FAdapterDescription := FAdapterDescriptions[i];
exit;
end;
end;
FAdapterIndex := -1;
FAdapterName := '';
FAdapterDescription := '';
end;
procedure TCustomSnoop.DoOpen(Source: PChar; Auth: ppcap_rmtauth; Dev: ppcap_if_t);
var
ErrBuf: array [0 .. PCAP_ERRBUF_SIZE - 1] of Char;
begin
FPcap := pcap_open(Source, FSnapLen, FFlags, FReadTimeOut, Auth, ErrBuf);
if FPcap = nil then
begin
FError := ErrBuf;
exit;
end;
FSourceName := String(Source);
if not ProcessFilter(Dev) then exit;
FActive := true;
end;
procedure TCustomSnoop.CreateSnoopThread;
begin
// Start Snoop Read Thread
FThread := TSnoopThread.Create(Self);
FThread.OnTerminate := ThreadTerminate;
FThread.FreeOnTerminate := false;
FThread.Resume;
end;
constructor TCustomSnoop.Create(AOwner: TComponent);
begin
inherited;
FActive := false;
FAdapterIndex := -1;
FAdapterName := '';
FAdapterNames := TStringList.Create;
GetAdapterNames;
FAdapterDescriptions := TStringList.Create;
GetAdapterDescriptions;
if FError <> '' then
begin
if not (csDesigning in ComponentState) then
ShowMessage(FError);
end;
FThreadSafe := true;
FFilter := '';
FSnapLen := SNOOP_DEFAULT_SNAPLEN;
FFlags := PCAP_OPENFLAG_PROMISCUOUS;
FReadTimeOut := 100;
FError := '';
FThread := nil;
FPcap := nil;
end;
destructor TCustomSnoop.Destroy;
begin
Close;
if FAdapterNames <> nil then
begin
FAdapterNames.Free;
FAdapterNames := nil;
end;
inherited;
end;
procedure TCustomSnoop.Open;
var
i: Integer;
Dev: ppcap_if_t;
AllDevs: ppcap_if_t;
ErrBuf: array[0 .. PCAP_ERRBUF_SIZE - 1] of Char;
begin
if FActive then
begin
FError := 'Snoop Error: Already open';
exit;
end;
i := pcap_findalldevs(@AllDevs, ErrBuf);
if i <> 0 then // if error occured
begin
Error := ErrBuf;
exit;
end;
Dev := AllDevs;
while Dev <> nil do
begin
if StrComp(Dev^.name, PChar(FAdapterName)) = 0 then break;
Dev := Dev^.next;
end;
if Dev = nil then
FError := 'Snoop Error: Invalid AdapterName'
else
begin
DoOpen(PChar(FAdapterName), nil, Dev);
if FActive then
CreateSnoopThread;
end;
pcap_freealldevs(AllDevs);
end;
procedure TCustomSnoop.Open(Host: String; UserName: String; Password: String);
var
i: Integer;
Source: String;
AdapterIndex: Integer;
Auth: pcap_rmtauth;
Dev: ppcap_if_t;
AllDevs: ppcap_if_t;
ErrBuf: array[0 .. PCAP_ERRBUF_SIZE - 1] of Char;
AdapterNames: TStringList;
AdapterDescriptions: TStringList;
begin
if FActive then
begin
FError := 'Snoop Error: Already open';
exit;
end;
Source := 'rpcap://' + Host + '/';
Auth.type_ := RPCAP_RMTAUTH_PWD;
Auth.username := PChar(UserName);
Auth.password := PChar(Password);
i := pcap_findalldevs_ex(PChar(Source), @Auth, @Alldevs, ErrBuf);
if i <> 0 then // if error occured
begin
Error := ErrBuf;
exit;
end;
AdapterNames := TStringList.Create;
AdapterDescriptions := TStringList.Create;
Dev := AllDevs;
while Dev <> nil do
begin
AdapterNames.Add(Dev^.name);
AdapterDescriptions.Add(Dev^.description);
Dev := Dev^.next;
end;
AdapterIndex := -1;
if @FOnGetRemoteAdapterInfo <> nil then
FOnGetRemoteAdapterInfo(Self, AdapterNames, AdapterDescriptions, AdapterIndex);
if AdapterIndex <> -1 then
Dev := AllDevs;
for i := 0 to AdapterIndex - 1 do
Dev := Dev^.next;
if Dev = nil then
FError := 'Snoop Error: Cancel By User or Invalid AdapterName'
else
begin
Source := Dev^.name;
DoOpen(PChar(Source), @Auth, Dev);
if FActive then
CreateSnoopThread;
end;
pcap_freealldevs(AllDevs);
end;
function TCustomSnoop.LoadFromFile(FileName: String): Boolean;
var
i: Integer;
Pcap: PPcap;
PacketHeader: PPCAP_PKTHDR;
PacketData: PETHERNET_HDR;
begin
Result := false;
if FActive then
begin
FError := 'Snoop Error: Already open';
exit;
end;
if FileName = '' then
begin
FError := 'Snoop Error: Invalid FileName';
exit;
end;
DoOpen(PChar('file://' + FileName), nil, nil);
if not FActive then exit;
Pcap := Self.FPcap;
while true do
begin
i := pcap_next_ex(Pcap, @PacketHeader, @PacketData);
case i of
-2: break; // EOF was reached reading from an offline capture
-1: begin// if an error occurred
FError := 'Snoop Error: pcap_next_ex return -1';
break;
end;
0: begin
snoopGTrace('[Snoop.pas] Oh, impossible brance(pcap_next_ex)'); // gilgil temp 2003.08.05
continue; // if the timeout set with pcap_open_live() has elapsed.
end;
end;
FPacketHeader := PacketHeader;
FPacketData := PacketData;
OnCaptureCB;
end;
FActive := false;
pcap_close(Pcap);
Result := true;
end;
procedure TCustomSnoop.Close;
begin
if not FActive then
exit;
if FThread = nil then
begin
FError := 'Snoop Error: No thread to stop';
ShowMessage(FError);
exit;
end;
FActive := false;
// Stop Snooping Thread
FThread.Close;
FThread.Free;
FThread := nil;
end;
// ----------------------------------------------------------------------------
// TSnoop Class
// ----------------------------------------------------------------------------
procedure TSnoop.OnCaptureCB;
begin
if @FOnCapture = nil then exit;
FOnCapture(Self, FPacketHeader, FPacketData);
end;
constructor TSnoop.Create(AOwner: TComponent);
begin
inherited;
end;
// ----------------------------------------------------------------------------
// TSnoopIP Class
// ----------------------------------------------------------------------------
procedure TSnoopIP.OnCaptureCB;
var
IPHeader: PIP_HDR;
begin
if @FOnCaptureIP = nil then exit;
if not snoopIsIP(FPacketData, @IPHeader) then exit;
FOnCaptureIP(Self, FPacketHeader, FPacketData, IPHeader);
end;
constructor TSnoopIP.Create(AOwner: TComponent);
begin
inherited;
Filter := 'ip';
end;
// ----------------------------------------------------------------------------
// TSnoopARP Class
// ----------------------------------------------------------------------------
procedure TSnoopARP.OnCaptureCB;
var
ARPHeader: PARP_HDR;
begin
if @FOnCaptureARP = nil then exit;
if not snoopIsARP(FPacketData, @ARPHeader) then exit;
FOnCaptureARP(Self, FPacketHeader, FPacketData, ARPHeader);
end;
constructor TSnoopARP.Create(AOwner: TComponent);
begin
inherited;
Filter := 'arp';
end;
// ----------------------------------------------------------------------------
// TSnoopICMP Class
// ----------------------------------------------------------------------------
procedure TSnoopICMP.OnCaptureCB;
var
IPHeader: PIP_HDR;
ICMPHeader: PICMP_HDR;
begin
if @FOnCaptureICMP = nil then exit;
if not snoopIsIP(FPacketData, @IPHeader) then exit;
if not snoopIsICMP(IPHeader, @ICMPHeader) then exit;
FOnCaptureICMP(Self, FPacketHeader, FPacketData, IPHeader, ICMPHeader);
end;
constructor TSnoopICMP.Create(AOwner: TComponent);
begin
inherited;