-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunit1.pas
More file actions
2073 lines (1702 loc) · 55.7 KB
/
unit1.pas
File metadata and controls
2073 lines (1702 loc) · 55.7 KB
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
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes,SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Spin, Grids, pingsend, winsock, sockets, Windows,
SyncObjs, Clipbrd, Menus, ComCtrls, ExtCtrls, unit2, sqlite3conn, sqldb,
unit3, blcksock, Types, ssl_openssl3,httpsend;
var
ThreadLock: TCriticalSection;
MaxThreads: integer = 100; // Set your max threads here
IPList: TStringList;
ActiveTasks: integer = 0;
crlf: string = #13#10;
type
TScanResult = record
IPAddress: string;
Port: integer;
Status: string;
Banner: string;
end;
type
TPortScanThread = class(TThread)
private
FScanResult: TScanResult;
procedure UpdateGrid;
procedure UpdateGridWrapper;
procedure DoScan;
function HexToString(H: string): string;
function RandomHex(Length: Integer): string;
protected
procedure Execute; override;
public
constructor Create(const IPAddress: string; Port: integer);
end;
type
// Assuming TPingTask is a simple record for demonstration
TPingTask = record
IPAddress: string;
Data: string;
end;
type
// TPingTaskQueue class definition
TPingTaskQueue = class
private
FTaskList: TList;
FCriticalSection: TCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure Lock;
procedure Unlock;
procedure AddTask(const Task: TPingTask);
function GetCount: integer;
function LockList: TList;
procedure UnlockList;
function TryGetTask(out Task: TPingTask): boolean;
end;
type
TPingThread = class(TThread)
private
FTask: TPingTask;
FHostName, FMacAddress: string;
FPingResult: string;
procedure UpdateUI;
function PingHostfun(const Host: string): string;
function GetMacAddr(const IPAddress: string; var ErrCode: DWORD): string;
function IPAddrToName(IPAddr: string): string;
protected
procedure Execute; override;
public
end;
PPingTask = ^TPingTask; // Pointer to TPingTask
{ TForm1 }
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
EditBoxTotal: TLabeledEdit;
EditBoxUsed: TLabeledEdit;
EditBoxFree: TLabeledEdit;
Label5: TLabel;
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
ProgressBar1: TProgressBar;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
procedure MenuItem3Click(Sender: TObject);
procedure MenuItem4Click(Sender: TObject);
procedure SortStringGrid;
procedure MoveRow(Grid: TStringGrid; FromIndex, ToIndex: integer);
procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: integer;
aRect: TRect; aState: TGridDrawState);
procedure StringGrid1HeaderClick(Sender: TObject; IsColumn: boolean;
Index: integer);
procedure TrimAppMemorySize;
function CalculateNumberOfIPsInRange: integer;
function GetLocalIPAddress: string;
procedure FinalizeTasks;
function GetPortDescription(Port: integer): string;
procedure ButtonScanPortsClick(Sender: TObject);
procedure StartPortScanning(const IPAddress: string; Ports: array of integer);
procedure PortScanTerminated(Sender: TObject);
procedure SortGridports;
procedure SortStringGrid2(Grid: TStringGrid; ColIndex: integer);
function CompareRows(const Row1, Row2: integer; Grid: TStringGrid;
const ColIndex: integer): integer;
procedure LookupMACMenuItemClick(Sender: TObject);
function LookupMAC(macPrefix: string; out ShortName, LongName: string;
dbname: string): boolean;
procedure tracertMenuItemClick(Sender: TObject);
procedure PingMenuItemClick(Sender: TObject);
procedure buttonpingClick(Sender: TObject);
procedure buttontracertClick(Sender: TObject);
function PingHostfun2(const Host: string): string;
function TraceRouteHostfun(const Host: string): string;
function Pingtracertrttl(const Host: string): string;
function SplitString(const aString, Delimiter: string): TStringList;
function CompareIPs2(IP1, IP2: string): integer;
procedure OpenURL(URL: string);
private
CompletedScans: integer;
SortAscending: array of boolean;
// Array to keep track of sort order for each column
TotalScans: integer;
ActiveScanThreads: integer;
LastSortedColumn: integer;
TaskQueue: TPingTaskQueue;
ThreadPool: array of TPingThread;
procedure StartThreads;
procedure StopThreads;
//procedure DumpExceptionCallStack(E: Exception);
procedure CopyMenuItemClick(Sender: TObject);
procedure PortScanMenuItemClick(Sender: TObject);
procedure UpdateProgressBar;
public
{ Public declarations }
end;
function SendArp(DestIP, SrcIP: ULONG; pMacAddr: pointer; PhyAddrLen: pointer): DWord;
stdcall; external 'iphlpapi.dll' Name 'SendARP';
function CompareIPs(const IP1, IP2: string): integer;
procedure DumpExceptionCallStack(E: Exception);
//procedure GlobalExceptionHandler(Sender: TObject; E: Exception);
var
Form1: TForm1;
implementation
{$R *.lfm}
//procedure GlobalExceptionHandler(Sender: TObject; E: Exception);
//begin
// DumpExceptionCallStack(E);
// // You can add additional handling here if needed
//end;
procedure TForm1.OpenURL(URL: string);
begin
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
constructor TPortScanThread.Create(const IPAddress: string; Port: integer);
begin
inherited Create(True);
FScanResult.IPAddress := IPAddress;
FScanResult.Port := Port;
FreeOnTerminate := True;
end;
procedure TPortScanThread.Execute;
begin
DoScan;
Synchronize(@UpdateGridWrapper);
InterlockedIncrement(Form1.CompletedScans);
Synchronize(@Form1.UpdateProgressBar);
end;
procedure TForm1.UpdateProgressBar;
var
PercentComplete: integer;
begin
if TotalScans > 0 then
begin
PercentComplete := (CompletedScans * 100) div TotalScans;
FormScanResults.ProgressBar1.Position := PercentComplete;
end;
end;
function TPortScanThread.RandomHex(Length: Integer): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length do
Result := Result + IntToHex(Random(16), 1);
end;
procedure TPortScanThread.DoScan;
var
TcpSock: TTCPBlockSocket;
SSLPorts: array[0..16] of integer;
IsSSLPort: boolean;
TriggerString: string;
i: integer;
begin
// Define SSL/TLS ports
// Correctly define SSL/TLS ports
SSLPorts[0] := 443;
SSLPorts[1] := 993;
SSLPorts[2] := 995;
SSLPorts[3] := 465;
SSLPorts[4] := 587;
SSLPorts[5] := 636;
SSLPorts[6] := 989;
SSLPorts[7] := 990;
SSLPorts[8] := 992;
SSLPorts[9] := 1311;
SSLPorts[10] := 2083;
SSLPorts[11] := 2087;
SSLPorts[12] := 2096;
SSLPorts[13] := 8443;
SSLPorts[14] := 8888;
SSLPorts[15] := 9091;
SSLPorts[16] := 10000;
////
// Initialize TriggerString based on the port
case FScanResult.Port of
80, 443, 2082, 2083, 2086, 2087, 2095, 2096, 8080, 8081, 8000, 8443, 8888, 10000:
// TriggerString := 'GET / HTTP/1.1'#13#10'Host: localhost'#13#10#13#10; // HTTP and common alternate ports
TriggerString := 'GET / HTTP/1.1'#13#10 +
'Host: localhost'#13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'#13#10 +
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'#13#10 +
'Accept-Language: en-US,en;q=0.5'#13#10 +
'Connection: keep-alive'#13#10#13#10;
21, 20:
TriggerString :=
'USER anonymous' + #13#10 +
'PASS guest' + #13#10 +
'SYST' + #13#10 +
'FEAT' + #13#10 + // Get supported features
'QUIT' + #13#10; // FTP
22:
TriggerString := 'SSH-2.0-bannergrab'#13#10; // SSH
25, 465, 587:
TriggerString :=
'EHLO bannergrab.com' + #13#10 +
'HELO bannergrab.com' + #13#10 + // Try both EHLO and HELO
'HELP' + #13#10 +
'QUIT' + #13#10; // SMTP
23:
TriggerString := #13#10; // Telnet
110, 995:
TriggerString :=
'USER anonymous' + #13#10 +
'PASS guest' + #13#10 +
'STAT' + #13#10 +
'UIDL' + #13#10 + // Get unique identifiers for messages
'QUIT' + #13#10; // POP3
143, 993:
TriggerString :=
'a1 LOGIN anonymous guest' + #13#10 +
'a2 LIST "" "*"' + #13#10 +
'a3 CAPA' + #13#10 + // Get supported capabilities
'a4 LOGOUT' + #13#10; // IMAP
119, 563:
TriggerString :=
'MODE READER' + #13#10 +
'LIST' + #13#10 +
'GROUP' + #13#10 + // List available newsgroups
'ARTICLE 1' + #13#10 + // Retrieve specific article
'QUIT' + #13#10;
161:
//TriggerString := HexToString('302902010004067075626c6963a01c0204ffffffff020100020100300e300c06082b060102010101000500302a020100040770726976617465a01c0204fffffffe020100020100300e300c06082b060102010101000500'); // SNMP
TriggerString := HexToString('302602010004067075626c6963a01b0204' + RandomHex(4) + '0201000201003011300f060a2b060102010101000500');
389, 636:
// TriggerString := HexToString('300c0201016007020103040080003035020102633004000a01000a0100020100020100010100870b6f626a656374436c6173733010040e6e616d696e67636f6e7465787473'); // LDAP
TriggerString := HexToString('300c02010160070201030400800000');
1433:
// TriggerString := HexToString('100100e000000100d80000000100007100000000000000076c04000000000000e0030000000000000908000056000a006a000a007e0000007e002000be00090000000000d0000400d8000000d8000000000c29c6634200000000c8000000420061006e006e00650072004700720061006200420061006e006e006500720047007200610062004d006900630072006f0073006f0066007400200044006100740061002000410063006300650073007300200043006f006d0070006f006e0065006e00740073003100320037002e0030002e0030002e0031004f00440042004300'); // Microsoft SQL Server
TriggerString := HexToString('12010034000000000000000015000000010000000000000000000000000000000000000000000000');
3306, 5432:
TriggerString := ''; // MySQL and PostgreSQL, typically don't use a banner
7, 9:
TriggerString := 'Echo'#13#10; // Echo
137:
// TriggerString := HexToString('a2480000000100000000000020434b4141414141414141414141414141414141414141414141414141414141410000210001'); // NetBIOS Name Service
TriggerString := HexToString('a40000010000000000000020434b4141414141414141414141414141414141414141414141414141414141410000200001');
123:
//TriggerString := HexToString('e30004fa000100000001000000000000000000000000000000000000000000000000000000000000ca9ba3352d7f950b160200010000000000000000160100010000000000000000'); // NTP
TriggerString := HexToString('e30006ec000000000000000000000000000000000000000000000000000000000000000000000000');
79:
TriggerString := 'root bin lp wheel spool adm mail postmaster news uucp snmp daemon'#13#10; // Finger
else
TriggerString := '';
end;
// Check if the port is an SSL/TLS port
IsSSLPort := False;
for i := Low(SSLPorts) to High(SSLPorts) do
begin
if SSLPorts[i] = FScanResult.Port then
begin
IsSSLPort := True;
Break;
end;
end;
TcpSock := TTCPBlockSocket.Create;
try
// Set connection timeout
TcpSock.ConnectionTimeout := Form1.SpinEdit1.Value;
// Connect to the server
TcpSock.Connect(FScanResult.IPAddress, IntToStr(FScanResult.Port));
if TcpSock.LastError = 0 then
begin
FScanResult.Status := 'Open';
// Determine the trigger string based on the port, customize as needed
// ... (Your logic for setting TriggerString based on FScanResult.Port)
if IsSSLPort then
begin
// Attempt SSL/TLS connection
TcpSock.SSLDoConnect;
if TcpSock.LastError <> 0 then
begin
// Handle SSL/TLS connection error, perhaps log or set FScanResult.Status
Exit;
end;
// SSL/TLS connection successful, send trigger string
TcpSock.SendString(TriggerString);
FScanResult.Banner := TcpSock.RecvTerminated(5000, CRLF);
end
else
begin
// Normal connection, send trigger string
TcpSock.SendString(TriggerString);
FScanResult.Banner := TcpSock.RecvTerminated(5000, CRLF);
end;
// Handle the received banner or data as per your requirement
// ...
end
else
FScanResult.Status := 'Closed';
finally
TcpSock.Free;
end;
end;
procedure TPortScanThread.UpdateGridWrapper;
begin
UpdateGrid;
end;
procedure TPortScanThread.UpdateGrid;
begin
with FormScanResults.StringGridResults do
begin
RowCount := RowCount + 1;
Cells[0, RowCount - 1] := FScanResult.IPAddress;
Cells[1, RowCount - 1] := IntToStr(FScanResult.Port);
Cells[2, RowCount - 1] := FScanResult.Status; // "Open" or "Closed"
Cells[3, RowCount - 1] := Form1.GetPortDescription(FScanResult.Port);
Cells[4, RowCount - 1] := FScanResult.Banner; // Banner column
end;
end;
procedure TForm1.StartPortScanning(const IPAddress: string; Ports: array of integer);
var
i: integer;
PortScanThread: TPortScanThread;
begin
Inc(ActiveScanThreads, Length(Ports)); // Increment the thread count
for i := Low(Ports) to High(Ports) do
begin
PortScanThread := TPortScanThread.Create(IPAddress, Ports[i]);
PortScanThread.OnTerminate := @PortScanTerminated;
// Set the event handler // Set the OnTerminate event
PortScanThread.Start;
end;
end;
procedure TForm1.PortScanTerminated(Sender: TObject);
begin
// Decrement the count in a thread-safe way
InterlockedDecrement(ActiveScanThreads);
if ActiveScanThreads = 0 then
begin
SortGridports;
FormScanResults.StringGridResults.AutoSizeColumns;
if (unit2.stoppressed = 0) then FormScanResults.edit1.Text := 'Complete!'
else
begin
FormScanResults.edit1.Text := 'Stopped!';
FormScanResults.progressbar1.Position := 0;
end;
TrimAppMemorySize;
end;
end;
procedure TForm1.SortGridports;
begin
// Implement your sorting logic here
// Example: Sort by port number (column index 1)
SortStringGrid2(FormScanResults.StringGridResults, 1); // Sort by column 1
end;
function tform1.CompareRows(const Row1, Row2: integer; Grid: TStringGrid;
const ColIndex: integer): integer;
var
Val1, Val2: string;
begin
Val1 := Grid.Cells[ColIndex, Row1];
Val2 := Grid.Cells[ColIndex, Row2];
// Compare as integer if they are numbers, otherwise as strings
if TryStrToInt(Val1, Result) and TryStrToInt(Val2, Result) then
Result := StrToInt(Val1) - StrToInt(Val2)
else
Result := CompareStr(Val1, Val2);
end;
procedure TForm1.SortStringGrid2(Grid: TStringGrid; ColIndex: integer);
var
i, j, k: integer;
Temp: string;
begin
for i := Grid.FixedRows to Grid.RowCount - 2 do
for j := Grid.FixedRows to Grid.RowCount - 2 do
if CompareRows(j, j + 1, Grid, ColIndex) > 0 then
begin
// Swap the rows
for k := 0 to Grid.ColCount - 1 do
begin
Temp := Grid.Cells[k, j];
Grid.Cells[k, j] := Grid.Cells[k, j + 1];
Grid.Cells[k, j + 1] := Temp;
end;
end;
end;
function TForm1.GetPortDescription(Port: integer): string;
begin
case Port of
20: Result := 'FTP Data Transfer';
21: Result := 'FTP Command Control';
22: Result := 'Secure Shell (SSH)';
23: Result := 'Telnet';
25: Result := 'Simple Mail Transfer Protocol (SMTP)';
53: Result := 'Domain Name System (DNS)';
80: Result := 'Hypertext Transfer Protocol (HTTP)';
110: Result := 'Post Office Protocol (POP3)';
119: Result := 'Network News Transfer Protocol (NNTP)';
123: Result := 'Network Time Protocol (NTP)';
135: Result := 'Microsoft RPC';
139: Result := 'NetBIOS';
143: Result := 'Internet Message Access Protocol (IMAP)';
161: Result := 'Simple Network Management Protocol (SNMP)';
194: Result := 'Internet Relay Chat (IRC)';
389: Result := 'LDAP';
443: Result := 'HTTPS - HTTP over TLS/SSL';
445: Result := 'Microsoft-DS (SMB)';
465: Result := 'SMTPS - Secure SMTP over SSL (deprecated)';
554: Result := 'Real Time Streaming Protocol (RTSP)';
587: Result := 'SMTP Mail Submission';
631: Result := 'Internet Printing Protocol (IPP)';
636: Result := 'LDAP over SSL/TLS';
993: Result := 'IMAPS - IMAP over SSL';
995: Result := 'POP3S - POP3 over SSL';
989: Result := 'FTPS implicit mode';
990: Result := 'FTPS control (command) connection in implicit mode';
992: Result := 'Telnet over SSL';
1311: Result := 'Dell OpenManage';
1433: Result := 'Microsoft SQL Server';
1521: Result := 'Oracle database default listener';
1723: Result := 'Point-to-Point Tunneling Protocol (PPTP)';
2049: Result := 'Network File System (NFS)';
2082: Result := 'cPanel default';
2083: Result := 'cPanel over SSL';
2086: Result := 'Web Host Manager default';
2087: Result := 'Web Host Manager over SSL';
2095: Result := 'cPanel Webmail';
2096: Result := 'cPanel Secure Webmail';
3306: Result := 'MySQL Database Server';
3389: Result := 'Remote Desktop Protocol (RDP)';
5060: Result := 'Session Initiation Protocol (SIP)';
5222: Result := 'XMPP/Jabber Client Connection';
5269: Result := 'XMPP/Jabber Server Connection';
5432: Result := 'PostgreSQL database';
5900: Result := 'Virtual Network Computing (VNC)';
6001: Result := 'X11:1';
8080: Result := 'HTTP Alternate (http_alt)';
8443: Result := 'HTTPS Alternate';
8888: Result := 'HTTPS Applications Misc';
9091: Result := 'BitTorrent';
10000: Result := 'Webmin';
else
Result := 'Unknown';
end;
end;
procedure TForm1.ButtonScanPortsClick(Sender: TObject);
var
SelectedIP: string;
SelectedRow: integer;
PortListString: string;
PortArray: array of integer;
i, PortNumber: integer;
begin
PortListstring :=
'20,21,22,23,25,53,80,110,119,123,135,139,143,161,194,389,443,445,465,554,587,631,636,'
+ '993,995,989,990,992,1311,1433,1521,1723,2049,2082,2083,2086,2087,2095,2096,3306,3389,5060,5222,5269,'
+ '5432,5900,6001,8080,8443,8888,9091,10000';
unit2.stoppressed := 0;
// Check if a row is selected
SelectedRow := StringGrid1.Row; // Assuming StringGrid1 is your main form StringGrid
if (SelectedRow > 0) and (SelectedRow < StringGrid1.RowCount) then
begin
SelectedIP := StringGrid1.Cells[0, SelectedRow];
// Assuming IPs are in the first column
FormScanResults.WindowState := wsnormal;
FormScanResults.Show;
// Prepare the StringGrid on FormScanResults
with FormScanResults.StringGridResults do
begin
RowCount := 1; // Reset to 1 to keep the header row
Cells[0, 0] := 'IP';
Cells[1, 0] := 'Port';
Cells[2, 0] := 'Status';
Cells[3, 0] := 'Description';
Cells[4, 0] := 'Banner';
end;
FormScanResults.StringGridResults.AutoSizeColumns;
// Perform the port scan for the selected IP
FormScanResults.edit1.Text := 'Scanning Ports!';
formscanresults.edit1.Refresh;
// Split the string and convert each part to an integer
with TStringList.Create do
try
Delimiter := ',';
DelimitedText := PortListString;
SetLength(PortArray, Count);
for i := 0 to Count - 1 do
begin
if TryStrToInt(Strings[i], PortNumber) then
PortArray[i] := PortNumber
else
raise Exception.Create('Invalid port number: ' + Strings[i]);
end;
finally
Free;
end;
CompletedScans := 0;
FormScanResults.ProgressBar1.Min := 0;
FormScanResults.ProgressBar1.Max := 100; // For percentage
FormScanResults.ProgressBar1.Position := 0;
TotalScans := Length(PortArray);
StartPortScanning(SelectedIP, PortArray); // Example IP and ports
end
else
ShowMessage('Please select a row with an IP address.');
end;
constructor TPingTaskQueue.Create;
begin
inherited Create;
FTaskList := TList.Create;
FCriticalSection := TCriticalSection.Create;
end;
destructor TPingTaskQueue.Destroy;
begin
FCriticalSection.Free;
FTaskList.Free;
inherited Destroy;
end;
procedure TPingTaskQueue.Lock;
begin
FCriticalSection.Acquire;
end;
procedure TPingTaskQueue.Unlock;
begin
FCriticalSection.Release;
end;
function TPingTaskQueue.GetCount: integer;
begin
Result := FTaskList.Count;
end;
function TPingTaskQueue.TryGetTask(out Task: TPingTask): boolean;
var
TaskPtr: PPingTask;
begin
FCriticalSection.Acquire;
try
Result := FTaskList.Count > 0;
if Result then
begin
TaskPtr := PPingTask(FTaskList[0]);
Task := TaskPtr^;
Dispose(TaskPtr); // Free the memory allocated for the task
FTaskList.Delete(0);
end;
finally
FCriticalSection.Release;
end;
end;
procedure TPingTaskQueue.AddTask(const Task: TPingTask);
var
NewTask: PPingTask;
begin
New(NewTask);
NewTask^ := Task;
FCriticalSection.Acquire;
try
FTaskList.Add(NewTask);
finally
FCriticalSection.Release;
end;
end;
procedure TPingThread.Execute;
var
Task: TPingTask;
ErrCode: DWORD;
begin
ErrCode := 0;
while not Terminated do // Continue looping until the thread is terminated
begin
// Attempt to get a task from the queue
if Form1.TaskQueue.TryGetTask(Task) then
begin
FTask := Task;
// Process the task
FPingResult := PingHostFun(Task.IPAddress);
// Optional: Get additional information
if Form1.CheckBox1.Checked then
FHostName := IPAddrToName(Task.IPAddress)
else
FHostName := 'N/A';
if Form1.CheckBox2.Checked then
FMacAddress := GetMacAddr(Task.IPAddress, ErrCode)
else
FMacAddress := 'N/A';
// Synchronize the UI update
Synchronize(@UpdateUI);
InterlockedDecrement(ActiveTasks);
end
else
begin
// If no task is available, wait before trying again
Sleep(100);
Break; // Exit the loop if there are no more tasks
end;
end;
if (ActiveTasks = 0) then
begin
Synchronize(@Form1.FinalizeTasks);
end;
end;
procedure TForm1.SortStringGrid;
var
i, j: integer;
begin
edit3.Text := 'Sorting!';
// Basic bubble sort for demonstration
for i := 1 to StringGrid1.RowCount - 1 do
for j := i + 1 to StringGrid1.RowCount - 1 do
if CompareIPs(StringGrid1.Cells[0, i], StringGrid1.Cells[0, j]) > 0 then
begin
// Swap rows if the IP address at i is greater than the one at j
MoveRow(StringGrid1, i, j);
end;
end;
procedure TForm1.MoveRow(Grid: TStringGrid; FromIndex, ToIndex: integer);
var
i: integer;
Temp: string;
begin
for i := 0 to Grid.ColCount - 1 do
begin
Temp := Grid.Cells[i, FromIndex];
Grid.Cells[i, FromIndex] := Grid.Cells[i, ToIndex];
Grid.Cells[i, ToIndex] := Temp;
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: integer;
aRect: TRect; aState: TGridDrawState);
var
Grid: TStringGrid;
Arrow: string;
begin
Grid := Sender as TStringGrid;
if (aRow = 0) and (aCol = LastSortedColumn) then
begin
Grid.Canvas.FillRect(aRect); // Fill the cell background
if SortAscending[aCol] then
Arrow := ' ↑' // Arrow pointing upwards for ascending order
else
Arrow := ' ↓'; // Arrow pointing downwards for descending order
Grid.Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, Grid.Cells[aCol, aRow] + Arrow);
//stringgrid1.AutoSizeColumns;
end
else if aRow = 0 then
begin
// For other header cells without sorting arrow
Grid.Canvas.FillRect(aRect);
Grid.Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, Grid.Cells[aCol, aRow]);
end
else
begin
// For non-header cells
Grid.Canvas.FillRect(aRect);
Grid.Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, Grid.Cells[aCol, aRow]);
end;
end;
function tform1.SplitString(const aString, Delimiter: string): TStringList;
var
s: string;
p: integer;
begin
Result := TStringList.Create;
s := aString;
while Length(s) > 0 do
begin
p := Pos(Delimiter, s);
if p > 0 then
begin
Result.Add(Copy(s, 1, p - 1));
s := Copy(s, p + Length(Delimiter), Length(s));
end
else
begin
Result.Add(s);
Break;
end;
end;
end;
function tform1.CompareIPs2(IP1, IP2: string): integer;
var
Parts1, Parts2: TStringList;
i, Num1, Num2: integer;
begin
Result := 0;
Parts1 := SplitString(IP1, '.');
Parts2 := SplitString(IP2, '.');
try
for i := 0 to 3 do
begin
if Parts1.Count > i then
Num1 := StrToInt(Parts1[i])
else
Num1 := 0;
if Parts2.Count > i then
Num2 := StrToInt(Parts2[i])
else
Num2 := 0;
if Num1 < Num2 then
begin
Result := -1;
Break;
end
else if Num1 > Num2 then
begin
Result := 1;
Break;
end;
end;
finally
Parts1.Free;
Parts2.Free;
end;
end;
procedure TForm1.StringGrid1HeaderClick(Sender: TObject; IsColumn: boolean;
Index: integer);
var
i, j: integer;
Temp: string;
Sorted: boolean;
CompareResult: integer;
begin
if IsColumn then
begin
repeat
Sorted := True;
for i := 1 to StringGrid1.RowCount - 2 do
begin
// Compare the cells in the clicked column
if Index = 0 then
begin
if SortAscending[Index] then
CompareResult := CompareIPs(StringGrid1.Cells[Index, i],
StringGrid1.Cells[Index, i + 1])
else
CompareResult := CompareIPs(StringGrid1.Cells[Index, i + 1],
StringGrid1.Cells[Index, i]);
end
else if SortAscending[Index] then
// Standard text comparison for other columns
CompareResult := AnsiCompareText(StringGrid1.Cells[Index, i],
StringGrid1.Cells[Index, i + 1])
else
CompareResult := AnsiCompareText(StringGrid1.Cells[Index, i + 1],
StringGrid1.Cells[Index, i]);
// Swap rows if the comparison result indicates they are out of order
if CompareResult > 0 then
begin
for j := 0 to StringGrid1.ColCount - 1 do
begin
Temp := StringGrid1.Cells[j, i];
StringGrid1.Cells[j, i] := StringGrid1.Cells[j, i + 1];
StringGrid1.Cells[j, i + 1] := Temp;
end;
Sorted := False;
end;
end;
until Sorted;
// Toggle the sort order for the next click
SortAscending[Index] := not SortAscending[Index];
LastSortedColumn := Index;
StringGrid1.Invalidate;
end;
end;
procedure TPingThread.UpdateUI;
var
RowIndex, PercentComplete: integer;
begin
with Form1.StringGrid1 do
begin
// Lock the grid or ensure thread safety before updating
ThreadLock.Acquire;
try
RowIndex := RowCount; // Get current row count
RowCount := RowCount + 1; // Increase row count to add a new row
// Update the grid with the ping results
Cells[0, RowIndex] := FTask.IPAddress; // IP Address
Cells[1, RowIndex] := FPingResult; // Ping Result
Cells[2, RowIndex] := FHostName; // Host Name
Cells[3, RowIndex] := FMacAddress; // MAC Address