This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Vcl.ConnectionMonitor.pas
903 lines (803 loc) · 25.5 KB
/
EvilWorks.Vcl.ConnectionMonitor.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
unit EvilWorks.Vcl.ConnectionMonitor;
interface
uses
Winapi.Windows,
Winapi.Messages,
Winapi.IpExport,
Winapi.IpHlpApi,
Winapi.IpTypes,
Winapi.PsApi,
System.SysUtils,
System.Classes,
EvilWorks.Api.Winsock2,
EvilWorks.Api.IpHlpApi,
EvilWorks.Api.TcpEStats,
EvilWorks.System.SysUtils,
EvilWorks.System.ProcessUtils;
type
{ Exceptions }
EConnectionMonitor = class(Exception);
{ Forward declarations }
TConnectionMonitor = class;
TConnectionInfo = class;
{ TProtocol }
TProtocol = (ptTCP, ptUDP);
{ TDataRowType }
TDataRowType = (drtTCP4, drtTCP6, drtUDP4, drtUDP6);
TDataRowTypes = set of TDataRowType;
{ Events }
TOnHostnameLookupDone = procedure(aMonitor: TConnectionMonitor; aConnection: TConnectionInfo) of object;
{ TConnectionInfo }
TConnectionInfo = class(TPersistent)
private
FOwner : TConnectionMonitor;
FDataRow : pointer;
FDataRowType: TDataRowType;
FIndex : integer;
private
function GetCompany: string;
function GetConnectionCreationTime: TDateTime;
function GetFileAttributes: DWORD;
function GetFileDescription: string;
function GetFileVersion: string;
function GetLocalAddr: string;
function GetLocalHostName: string;
function GetLocalPort: string;
function GetLocalPortName: string;
function GetModuleFileName: string;
function GetProcessCreationTime: TDateTime;
function GetProcessID: DWORD;
function GetProcessName: string;
function GetProcessPath: string;
function GetProcessServices: string;
function GetProductName: string;
function GetProtocol: TProtocol;
function GetRemoteAddr: string;
function GetRemoteHostName: string;
function GetRemoteIPCountry: string;
function GetRemotePort: string;
function GetRemotePortName: string;
function GetState: DWORD;
function GetUserName: string;
function GetWindowTitle: string;
public
constructor Create(aOwner: TConnectionMonitor);
destructor Destroy; override;
protected
property DataRow: pointer read FDataRow write FDataRow;
public
property index: integer read FIndex;
published
property ProcessID : DWORD read GetProcessID;
property ProcessName : string read GetProcessName;
property ProcessPath : string read GetProcessPath;
property ProcessCreationTime : TDateTime read GetProcessCreationTime;
property UserName : string read GetUserName;
property ModuleFileName : string read GetModuleFileName;
property FileAttributes : DWORD read GetFileAttributes;
property FileVersion : string read GetFileVersion;
property FileDescription : string read GetFileDescription;
property ProductName : string read GetProductName;
property Company : string read GetCompany;
property WindowTitle : string read GetWindowTitle;
property ProcessServices : string read GetProcessServices;
property Protocol : TProtocol read GetProtocol;
property State : DWORD read GetState;
property LocalAddr : string read GetLocalAddr;
property LocalPort : string read GetLocalPort;
property LocalHostName : string read GetLocalHostName;
property LocalPortName : string read GetLocalPortName;
property RemoteAddr : string read GetRemoteAddr;
property RemotePort : string read GetRemotePort;
property RemoteHostName : string read GetRemoteHostName;
property RemotePortName : string read GetRemotePortName;
property RemoteIPCountry : string read GetRemoteIPCountry;
property ConnectionCreationTime: TDateTime read GetConnectionCreationTime;
end;
{ TProcessInfo }
TProcessInfo = class
private
FFileVersion : string;
FProcessServices : string;
FProductName : string;
FFileAttributes : DWORD;
FModuleFileName : string;
FProcessID : DWORD;
FCompany : string;
FProcessCreationTime: TDateTime;
FFileDescription : string;
FProcessPath : string;
FProcessName : string;
FUserName : string;
FWindowTitle : string;
public
property ProcessID : DWORD read FProcessID;
property ProcessName : string read FProcessName;
property ProcessPath : string read FProcessPath;
property ProcessCreationTime: TDateTime read FProcessCreationTime;
property UserName : string read FUserName;
property ModuleFileName : string read FModuleFileName;
property FileAttributes : DWORD read FFileAttributes;
property FileVersion : string read FFileVersion;
property FileDescription : string read FFileDescription;
property ProductName : string read FProductName;
property Company : string read FCompany;
property WindowTitle : string read FWindowTitle;
property ProcessServices : string read FProcessServices;
end;
{ THostName Item }
THostNameCacheItem = class
private
FHostName: string;
FIP : string;
public
property IP : string read FIP;
property HostName: string read FHostName;
end;
{ TConnectionMonitor }
TConnectionMonitor = class(TComponent)
private
FConnections : array of TConnectionInfo;
FConnectionCount : integer;
FProcessInfos : array of TProcessInfo;
FProcessInfoCount : integer;
FHostNameCacheItems : array of THostNameCacheItem;
FHostNameCacheItemCount : integer;
FWaitingHostNameLookups : array of THostNameCacheItem;
FWaitingHostNameLookupCount: integer;
FMaxParallelHostnameLookups: integer;
FDataRetrievalTypes : TDataRowTypes;
FOnHostnameLookupDone : TOnHostnameLookupDone;
function GetConnection(const aIndex: integer): TConnectionInfo;
procedure SetMaxParalleHostnamelLookups(const Value: integer);
protected
FActiveHostnameLookups: integer;
function HostNameLookup(aConnection: TConnectionInfo; const aRemote: boolean): string;
procedure EventHostnameLookupDone(aConnection: TConnectionInfo);
function AddConnection(const aDataRowType: TDataRowType): TConnectionInfo;
procedure ClearConnectionTable;
procedure ClearHostNameCache;
procedure GetTCP4Table;
procedure GetTCP6Table;
procedure GetUDP4Table;
procedure GetUDP6Table;
procedure GetProcessTable;
function GetCompany(const aProcessID: DWORD): string;
function GetFileAttributes(const aProcessID: DWORD): DWORD;
function GetFileDescription(const aProcessID: DWORD): string;
function GetFileVersion(const aProcessID: DWORD): string;
function GetModuleFileName(const aProcessID: DWORD): string;
function GetProcessCreationTime(const aProcessID: DWORD): TDateTime;
function GetProcessName(const aProcessID: DWORD): string;
function GetProcessPath(const aProcessID: DWORD): string;
function GetProcessServices(const aProcessID: DWORD): string;
function GetProductName(const aProcessID: DWORD): string;
function GetUserName(const aProcessID: DWORD): string;
function GetWindowTitle(const aProcessID: DWORD): string;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure Assign(aSource: TPersistent); override;
procedure Refresh;
property Connections[const aIndex: integer]: TConnectionInfo read GetConnection; default;
property Count: integer read FConnectionCount;
published
property DataRetrievalTypes : TDataRowTypes read FDataRetrievalTypes write FDataRetrievalTypes default [drtTCP4, drtTCP6, drtUDP4, drtUDP6];
property MaxParallelHostnameLookups: integer read FMaxParallelHostnameLookups write SetMaxParalleHostnamelLookups default 10;
property OnHostnameLookupDone : TOnHostnameLookupDone read FOnHostnameLookupDone write FOnHostnameLookupDone;
end;
implementation
{ TConnectionInfo }
constructor TConnectionInfo.Create(aOwner: TConnectionMonitor);
begin
FOwner := aOwner;
FDataRow := nil;
FIndex := - 1;
end;
destructor TConnectionInfo.Destroy;
begin
if (FDataRow <> nil) then
FreeMem(FDataRow);
inherited;
end;
function TConnectionInfo.GetCompany: string;
begin
Result := FOwner.GetCompany(ProcessID);
end;
function TConnectionInfo.GetConnectionCreationTime: TDateTime;
begin
Result := 0;
// case FDataRowType of
// drtTCP4:
// Result := SystemTimeToDateTime(TSystemTime(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.liCreateTimestamp));
// drtTCP6:
// Result := SystemTimeToDateTime(TSystemTime(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.liCreateTimestamp));
// drtUDP4:
// Result := SystemTimeToDateTime(TSystemTime(PMIB_UDPROW_OWNER_MODULE(FDataRow)^.liCreateTimestamp));
// drtUDP6:
// Result := SystemTimeToDateTime(TSystemTime(PMIB_UDP6ROW_OWNER_MODULE(FDataRow)^.liCreateTimestamp));
// end;
end;
function TConnectionInfo.GetFileAttributes: DWORD;
begin
Result := FOwner.GetFileAttributes(ProcessID);
end;
function TConnectionInfo.GetFileDescription: string;
begin
Result := FOwner.GetFileDescription(ProcessID);
end;
function TConnectionInfo.GetFileVersion: string;
begin
Result := FOwner.GetFileVersion(ProcessID);
end;
function TConnectionInfo.GetLocalAddr: string;
begin
case FDataRowType of
drtTCP4:
// Result := InAddr4ToStr(in_addr(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwLocalAddr));
// drtTCP6:
// Result := InAddr6ToStr(in6_addr(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.ucLocalAddr));
// drtUDP4:
// Result := InAddr4ToStr(in_addr(PMIB_UDPROW_OWNER_MODULE(FDataRow)^.dwLocalAddr));
// drtUDP6:
// Result := InAddr6ToStr(in6_addr(PMIB_UDP6ROW_OWNER_MODULE(FDataRow)^.ucLocalAddr));
end;
end;
function TConnectionInfo.GetLocalHostName: string;
begin
Result := FOwner.HostNameLookup(Self, False);
end;
function TConnectionInfo.GetLocalPort: string;
begin
case FDataRowType of
drtTCP4:
Result := IntToStr(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwLocalPort);
drtTCP6:
Result := IntToStr(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.dwLocalPort);
drtUDP4:
Result := IntToStr(PMIB_UDPROW_OWNER_MODULE(FDataRow)^.dwLocalPort);
drtUDP6:
Result := IntToStr(PMIB_UDP6ROW_OWNER_MODULE(FDataRow)^.dwLocalPort);
end;
end;
function TConnectionInfo.GetLocalPortName: string;
begin
end;
function TConnectionInfo.GetModuleFileName: string;
begin
Result := FOwner.GetModuleFileName(ProcessID);
end;
function TConnectionInfo.GetProcessCreationTime: TDateTime;
begin
Result := FOwner.GetProcessCreationTime(ProcessID);
end;
function TConnectionInfo.GetProcessID: DWORD;
begin
case FDataRowType of
drtTCP4:
Result := PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwOwningPid;
drtTCP6:
Result := PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.dwOwningPid;
drtUDP4:
Result := PMIB_UDPROW_OWNER_MODULE(FDataRow)^.dwOwningPid;
drtUDP6:
Result := PMIB_UDP6ROW_OWNER_MODULE(FDataRow)^.dwOwningPid;
else
Result := 0;
end;
end;
function TConnectionInfo.GetProcessName: string;
begin
Result := FOwner.GetProcessName(ProcessID);
end;
function TConnectionInfo.GetProcessPath: string;
begin
Result := FOwner.GetProcessPath(ProcessID);
end;
function TConnectionInfo.GetProcessServices: string;
begin
Result := FOwner.GetProcessServices(ProcessID);
end;
function TConnectionInfo.GetProductName: string;
begin
Result := FOwner.GetProductName(ProcessID);
end;
function TConnectionInfo.GetProtocol: TProtocol;
begin
case FDataRowType of
drtTCP4, drtTCP6:
Result := ptTCP;
drtUDP4, drtUDP6:
Result := ptUDP;
else
Result := ptTCP; // Compiler nagging.
end;
end;
function TConnectionInfo.GetRemoteAddr: string;
begin
case FDataRowType of
drtTCP4:
// Result := InAddr4ToStr(in_addr(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwRemoteAddr));
// drtTCP6:
// Result := InAddr6ToStr(in6_addr(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.ucRemoteAddr));
// drtUDP4:
// Result := EmptyStr;
// drtUDP6:
// Result := EmptyStr;
end;
end;
function TConnectionInfo.GetRemoteHostName: string;
begin
Result := FOwner.HostNameLookup(Self, True);
end;
function TConnectionInfo.GetRemoteIPCountry: string;
begin
end;
function TConnectionInfo.GetRemotePort: string;
begin
case FDataRowType of
drtTCP4:
Result := IntToStr(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwRemotePort);
drtTCP6:
Result := IntToStr(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.dwRemotePort);
drtUDP4:
Result := EmptyStr;
drtUDP6:
Result := EmptyStr;
end;
end;
function TConnectionInfo.GetRemotePortName: string;
begin
end;
function TConnectionInfo.GetState: DWORD;
begin
case FDataRowType of
drtTCP4:
Result := PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwState;
drtTCP6:
Result := PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.dwState;
drtUDP4:
Result := 0;
drtUDP6:
Result := 0;
else
Result := 0; // Compiler nagging.
end;
end;
function TConnectionInfo.GetUserName: string;
begin
Result := FOwner.GetUserName(ProcessID);
end;
function TConnectionInfo.GetWindowTitle: string;
begin
Result := FOwner.GetWindowTitle(ProcessID);
end;
{ TConnectionMonitor }
constructor TConnectionMonitor.Create(aOwner: TComponent);
begin
inherited;
FConnectionCount := 0;
FProcessInfoCount := 0;
FHostNameCacheItemCount := 0;
FWaitingHostNameLookupCount := 0;
FActiveHostnameLookups := 0;
FMaxParallelHostnameLookups := 10;
FDataRetrievalTypes := [drtTCP4, drtTCP6, drtUDP4, drtUDP6];
end;
destructor TConnectionMonitor.Destroy;
begin
ClearConnectionTable;
ClearHostNameCache;
inherited;
end;
function TConnectionMonitor.HostNameLookup(aConnection: TConnectionInfo; const aRemote: boolean): string;
var
IP : string;
i : integer;
begin
if (aRemote) then
begin
case aConnection.FDataRowType of
drtTCP4:
IP := InAddr4ToStr(in_addr(PMIB_TCPROW_OWNER_MODULE(aConnection.FDataRow)^.dwRemoteAddr));
drtTCP6:
IP := InAddr6ToStr(in6_addr(PMIB_TCP6ROW_OWNER_MODULE(aConnection.FDataRow)^.ucRemoteAddr));
drtUDP4:
IP := EmptyStr;
drtUDP6:
IP := EmptyStr;
end;
end
else
begin
case aConnection.FDataRowType of
drtTCP4:
IP := InAddr4ToStr(in_addr(PMIB_TCPROW_OWNER_MODULE(aConnection.FDataRow)^.dwLocalAddr));
drtTCP6:
IP := InAddr6ToStr(in6_addr(PMIB_TCP6ROW_OWNER_MODULE(aConnection.FDataRow)^.ucLocalAddr));
drtUDP4:
IP := InAddr4ToStr(in_addr(PMIB_UDPROW_OWNER_MODULE(aConnection.FDataRow)^.dwLocalAddr));
drtUDP6:
IP := InAddr6ToStr(in6_addr(PMIB_UDP6ROW_OWNER_MODULE(aConnection.FDataRow)^.ucLocalAddr));
end;
end;
if (IP = EmptyStr) then
Exit;
for i := 0 to FHostNameCacheItemCount - 1 do
begin
if (SameText(FHostNameCacheItems[i].IP, IP)) then
begin
Result := FHostNameCacheItems[i].HostName;
Exit;
end;
end;
if (FActiveHostnameLookups >= FMaxParallelHostnameLookups) then
begin
Inc(FWaitingHostNameLookupCount);
SetLength(FWaitingHostNameLookups, FWaitingHostNameLookupCount);
FWaitingHostNameLookups[FWaitingHostNameLookupCount - 1] := THostNameCacheItem.Create;
FWaitingHostNameLookups[FWaitingHostNameLookupCount - 1].FIP := IP;
end
else
begin
end;
// if (aRemote) then
// begin
// case aConnection.FDataRowType of
// drtTCP4:
// Result := InAddr4RevLookup(in_addr(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwRemoteAddr), False);
// drtTCP6:
// Result := InAddr6RevLookup(in6_addr(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.ucRemoteAddr), False);
// drtUDP4:
// Result := EmptyStr;
// drtUDP6:
// Result := EmptyStr;
// end;
// end
// else
// begin
// case aConnection.FDataRowType of
// drtTCP4:
// Result := InAddr4RevLookup(in_addr(PMIB_TCPROW_OWNER_MODULE(FDataRow)^.dwLocalAddr), False);
// drtTCP6:
// Result := InAddr6RevLookup(in6_addr(PMIB_TCP6ROW_OWNER_MODULE(FDataRow)^.ucLocalAddr), False);
// drtUDP4:
// Result := InAddr4RevLookup(in_addr(PMIB_UDPROW_OWNER_MODULE(FDataRow)^.dwLocalAddr), True);
// drtUDP6:
// Result := InAddr6RevLookup(in6_addr(PMIB_UDP6ROW_OWNER_MODULE(FDataRow)^.ucLocalAddr), True);
// end;
// end;
end;
procedure TConnectionMonitor.EventHostnameLookupDone(aConnection: TConnectionInfo);
begin
if (Assigned(FOnHostnameLookupDone)) then
FOnHostnameLookupDone(Self, aConnection);
end;
procedure TConnectionMonitor.Assign(aSource: TPersistent);
begin
if (aSource is TConnectionMonitor) then
begin
DataRetrievalTypes := TConnectionMonitor(aSource).DataRetrievalTypes;
end;
end;
procedure TConnectionMonitor.GetTCP4Table;
var
connectionInfo: TConnectionInfo;
ret : DWORD;
data : pointer;
ptr : PMIB_TCPROW_OWNER_MODULE;
datasize : DWORD;
c : cardinal;
begin
data := nil;
datasize := 0;
ret := GetExtendedTcpTable(data, datasize, False, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0);
if (ret = ERROR_INSUFFICIENT_BUFFER) then
begin
data := AllocMem(datasize);
try
ret := GetExtendedTcpTable(data, datasize, False, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0);
if (ret = NO_ERROR) then
begin
ptr := @PMIB_TCPTABLE_OWNER_MODULE(data)^.table;
c := 0;
while (c < PMIB_TCPTABLE_OWNER_MODULE(data)^.dwNumEntries) do
begin
connectionInfo := AddConnection(drtTCP4);
connectionInfo.DataRow := AllocMem(SizeOf(ptr^));
CopyMemory(connectionInfo.DataRow, ptr, SizeOf(ptr^));
Inc(c);
Inc(ptr);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedTcpTable failed when requesting connection table: (%d): %s', [ret, GetErrorText(ret)]));
end;
finally
FreeMem(data);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedTcpTable failed when requesting needed buffer size: (%d): %s', [ret, GetErrorText(ret)]));
end;
end;
procedure TConnectionMonitor.GetTCP6Table;
var
connectionInfo: TConnectionInfo;
ret : DWORD;
data : pointer;
ptr : PMIB_TCP6ROW_OWNER_MODULE;
datasize : DWORD;
c : cardinal;
begin
data := nil;
datasize := 0;
ret := GetExtendedTcpTable(data, datasize, False, AF_INET6, TCP_TABLE_OWNER_MODULE_ALL, 0);
if (ret = ERROR_INSUFFICIENT_BUFFER) then
begin
data := AllocMem(datasize);
try
ret := GetExtendedTcpTable(data, datasize, False, AF_INET6, TCP_TABLE_OWNER_MODULE_ALL, 0);
if (ret = NO_ERROR) then
begin
ptr := @PMIB_TCP6TABLE_OWNER_MODULE(data)^.table;
c := 0;
while (c < PMIB_TCP6TABLE_OWNER_MODULE(data)^.dwNumEntries) do
begin
connectionInfo := AddConnection(drtTCP6);
connectionInfo.DataRow := AllocMem(SizeOf(ptr^));
CopyMemory(connectionInfo.DataRow, ptr, SizeOf(ptr^));
Inc(c);
Inc(ptr);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedTcpTable failed requesting connection table: (%d): %s', [ret, GetErrorText(ret)]));
end;
finally
FreeMem(data);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedTcpTable failed requesting needed buffer size: (%d): %s', [ret, GetErrorText(ret)]));
end;
end;
procedure TConnectionMonitor.GetUDP4Table;
var
connectionInfo: TConnectionInfo;
ret : DWORD;
data : pointer;
ptr : PMIB_UDPROW_OWNER_MODULE;
datasize : DWORD;
c : cardinal;
begin
data := nil;
datasize := 0;
ret := GetExtendedUdpTable(data, datasize, False, AF_INET, UDP_TABLE_OWNER_MODULE, 0);
if (ret = ERROR_INSUFFICIENT_BUFFER) then
begin
data := AllocMem(datasize);
try
ret := GetExtendedUdpTable(data, datasize, False, AF_INET, UDP_TABLE_OWNER_MODULE, 0);
if (ret = NO_ERROR) then
begin
ptr := @PMIB_UDPTABLE_OWNER_MODULE(data)^.table;
c := 0;
while (c < PMIB_UDPTABLE_OWNER_MODULE(data)^.dwNumEntries) do
begin
connectionInfo := AddConnection(drtUDP4);
connectionInfo.DataRow := AllocMem(SizeOf(ptr^));
CopyMemory(connectionInfo.DataRow, ptr, SizeOf(ptr^));
Inc(c);
Inc(ptr);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedUdpTable failed requesting connection table: (%d): %s', [ret, GetErrorText(ret)]));
end;
finally
FreeMem(data);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedUdpTable failed requesting needed buffer size: (%d): %s', [ret, GetErrorText(ret)]));
end;
end;
procedure TConnectionMonitor.GetUDP6Table;
var
connectionInfo: TConnectionInfo;
ret : DWORD;
data : pointer;
ptr : PMIB_UDP6ROW_OWNER_MODULE;
datasize : DWORD;
c : cardinal;
begin
data := nil;
datasize := 0;
ret := GetExtendedUdpTable(data, datasize, False, AF_INET6, UDP_TABLE_OWNER_MODULE, 0);
if (ret = ERROR_INSUFFICIENT_BUFFER) then
begin
data := AllocMem(datasize);
try
ret := GetExtendedUdpTable(data, datasize, False, AF_INET6, UDP_TABLE_OWNER_MODULE, 0);
if (ret = NO_ERROR) then
begin
ptr := @PMIB_UDP6TABLE_OWNER_MODULE(data)^.table;
c := 0;
while (c < PMIB_UDP6TABLE_OWNER_MODULE(data)^.dwNumEntries) do
begin
connectionInfo := AddConnection(drtUDP6);
connectionInfo.DataRow := AllocMem(SizeOf(ptr^));
CopyMemory(connectionInfo.DataRow, ptr, SizeOf(ptr^));
Inc(c);
Inc(ptr);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedUdpTable failed requesting connection table: (%d): %s', [ret, GetErrorText(ret)]));
end;
finally
FreeMem(data);
end;
end
else
begin
ClearConnectionTable;
raise EConnectionMonitor.Create(Format('GetExtendedUdpTable failed requesting needed buffer size: (%d): %s', [ret, GetErrorText(ret)]));
end;
end;
procedure TConnectionMonitor.GetProcessTable;
var
ret : integer;
processTable: array [0 .. 1023] of DWORD;
dataReturned: DWORD;
i : integer;
begin
if (EnumProcesses(@processTable[0], SizeOf(processTable), dataReturned) = False) then
begin
ClearConnectionTable;
ret := GetLastError;
raise EConnectionMonitor.Create(Format('Error enumerating processes: (%d): %s.', [ret, GetErrorText(ret)]));
end;
FProcessInfoCount := (dataReturned div SizeOf(DWORD));
if (FProcessInfoCount = 0) then
raise EConnectionMonitor.Create('Error enumerating processes: Count of enumerated processes is 0.');
SetLength(FProcessInfos, FProcessInfoCount);
for i := 0 to FProcessInfoCount - 1 do
begin
FProcessInfos[i] := TProcessInfo.Create;
try
FProcessInfos[i].FProcessID := processTable[i];
FProcessInfos[i].FProcessName := GetPIDModuleName(processTable[i]);
except
on E: Exception do
{ silent };
end;
end;
{
property ProcessName
property ProcessPath
property ProcessCreationTime
property UserName
property ModuleFileName
property FileAttributes
property FileVersion
property FileDescription
property ProductName
property Company
property WindowTitle
property ProcessServices
}
end;
procedure TConnectionMonitor.Refresh;
begin
ClearConnectionTable;
if (drtTCP4 in DataRetrievalTypes) then
GetTCP4Table;
if (drtTCP6 in DataRetrievalTypes) then
GetTCP6Table;
if (drtUDP4 in DataRetrievalTypes) then
GetUDP4Table;
if (drtUDP6 in DataRetrievalTypes) then
GetUDP6Table;
if (DataRetrievalTypes <> []) then
GetProcessTable;
end;
procedure TConnectionMonitor.SetMaxParalleHostnamelLookups(const Value: integer);
begin
if (FMaxParallelHostnameLookups = Value) then
Exit;
FMaxParallelHostnameLookups := Value;
if (FMaxParallelHostnameLookups < 0) then
FMaxParallelHostnameLookups := 0;
end;
function TConnectionMonitor.AddConnection(const aDataRowType: TDataRowType): TConnectionInfo;
begin
Inc(FConnectionCount);
SetLength(FConnections, FConnectionCount);
FConnections[FConnectionCount - 1] := TConnectionInfo.Create(Self);
FConnections[FConnectionCount - 1].FIndex := (FConnectionCount - 1);
FConnections[FConnectionCount - 1].FDataRowType := aDataRowType;
Result := FConnections[FConnectionCount - 1];
end;
procedure TConnectionMonitor.ClearConnectionTable;
var
i: integer;
begin
for i := 0 to FConnectionCount - 1 do
FConnections[i].Free;
SetLength(FConnections, 0);
FConnectionCount := 0;
for i := 0 to FProcessInfoCount - 1 do
FProcessInfos[i].Free;
SetLength(FProcessInfos, 0);
FProcessInfoCount := 0;
end;
procedure TConnectionMonitor.ClearHostNameCache;
var
i: integer;
begin
for i := 0 to FHostNameCacheItemCount - 1 do
FHostNameCacheItems[i].Free;
SetLength(FHostNameCacheItems, 0);
FHostNameCacheItemCount := 0;
end;
function TConnectionMonitor.GetProcessCreationTime(const aProcessID: DWORD): TDateTime;
begin
Result := 0;
end;
function TConnectionMonitor.GetProcessName(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetProcessPath(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetProcessServices(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetUserName(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetWindowTitle(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetProductName(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetCompany(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetFileAttributes(const aProcessID: DWORD): DWORD;
begin
Result := 0;
end;
function TConnectionMonitor.GetFileDescription(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetFileVersion(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetModuleFileName(const aProcessID: DWORD): string;
begin
end;
function TConnectionMonitor.GetConnection(const aIndex: integer): TConnectionInfo;
begin
if (aIndex < 0) or (aIndex >= FConnectionCount) then
raise Exception.Create(Format('TConnectionMonitor(%s).GetConnection index(%d) out of bounds(%d).', [name, aIndex, FConnectionCount]));
Result := FConnections[aIndex];
end;
end.