论坛: 原创软件 标题: 老大,和尚,帮我看看这段扫描源码 复制本贴地址    
作者: sainthero [sainthero]    论坛用户   登录
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Commctrl,ComCtrls, ExtCtrls, CheckLst, Mask, Buttons, Spin,winsock;

const
  Max=100;
type
  TForm1 = class(TForm)
    Bevel1: TBevel;
    Label2: TLabel;
    Label1: TLabel;
    Panel1: TPanel;
    CheckListBox1: TCheckListBox;
    BitBtn1: TBitBtn;
    Label4: TLabel;
    BitBtn2: TBitBtn;
    Bevel2: TBevel;
    Label3: TLabel;
    Label5: TLabel;
    Bevel3: TBevel;
    Label6: TLabel;
    BitBtn3: TBitBtn;
    SpinEdit1: TSpinEdit;
    TreeView1: TTreeView;
    Label7: TLabel;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    procedure Sequence;
  public
    { Public declarations }
    ipEdit:HWND;
    ipEdit1:HWND;
    ipAddress:integer;
    endIp:integer;
    Single_Edit:HWND;
    SingleHostAddr:integer;
    end;

var
  Form1: TForm1;
  P:array[0..Max] of integer;
  Count:integer;
implementation

uses unit2;
{$R *.DFM}

procedure TForm1.Sequence; //功能是实现端口输入后自动排列
var
 i,j:integer;     //改过程采用的是冒泡排序的方法
 n:integer;
 temp:integer;
begin
 n:=Count;
 n:=n-1;
 while n>0 do
   begin
     j:=0;
     for i:=0 to n-1 do
     if P[i]>P[i+1] then
       begin
         temp:=P[i];
         P[i]:=P[i+1];
         P[i+1]:=temp;
         j:=i;
       end;
    n:=j;    
   end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 //ipAddress:=MAKEIPADDRESS(196,197,198,199);           //just testing
 InitCommonControl(ICC_INTERNET_CLASSES);
 ipEdit:=CreateWindow(WC_IPADDRESS,nil,WS_CHILD or WS_VISIBLE,10,50,130,23,self.Handle,0,
                      hInstance,nil);
 ipEdit1:=CreateWindow(WC_IPADDRESS,nil,WS_CHILD or WS_VISIBLE,150,50,130,23,self.Handle,0,
                   hInstance,nil);
 Single_Edit:=CreateWindow(WC_IPADDRESS,nil,WS_CHILD or WS_VISIBLE,100,110,130,23,self.Handle,0,
                      hInstance,nil);
 //TreeView1.Color:=clYellow;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendMessage(ipEdit,IPM_GETADDRESS,0,longint(@ipAddress));
  SendMessage(ipEdit1,IPM_GETADDRESS,0,longint(@endip));
  ShowMessage(IntToStr(endip-ipaddress));
 if ipAddress = 0 then
  begin
   beep;
   SendMessage(ipEdit,IPM_SETFOCUS,0,0);                //select the first field
   modalResult:=mrNone;
  end
 else
  Close;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.BitBtn4Click(Sender: TObject);
begin
  TreeView1.FullExpand;
end;

procedure TForm1.BitBtn5Click(Sender: TObject);
begin
  TreeView1.FullCollapse;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  ClientWidth:=543;
  ClientHeight:=445;
end;

//下面的过程作用是增加新的端口号
procedure TForm1.BitBtn3Click(Sender: TObject);
var
  i:integer;
  Size:integer;
  s:string;
  NewPort:integer;
begin
  NewPort:=StrToInt(SpinEdit1.Text);
  //判断端口输入是否合法
  if not (NewPort>0) then
   begin
    ShowMessage('端口必须大于0,请重新输入!'); //端口>0
    SpinEdit1.SetFocus;
    Exit;
   end;
  Count:=CheckListBox1.Items.Count;
  for i:=0 to Count-1 do
    P[i]:=StrToInt(CheckListBox1.Items[i]);
  for i:=0 to Count-1 do
    if NewPort=P[i] then
      begin
        ShowMessage('端口重复,请重新添加');
        SpinEdit1.Clear;                 //端口不能重复
        SpinEdit1.SetFocus;
        Exit;
      end;

  //将合法的端口号添加到CheckListBox中
  CheckListBox1.Items.Add(SpinEdit1.Text);
  Count:=CheckListBox1.Items.Count;
  Size:=Count;
  for i:=0 to Count-1 do
    P[i]:=StrToInt(CheckListBox1.Items[i]);
  Sequence;
  CheckListBox1.Clear;
  for i:=0 to Size-1 do
   begin
    s:=IntToStr(P[i]);
    CheckListBox1.Items.Add(s);
   end;

end;

//BitBtn1实现的是单机扫描,即只输入一个主机IP
//目前程序还有bug,当Host不通时,会导致程序不响应
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  wVersionRequested:Word;
  WSAData:TWSAData;
  sClntSock:TSocket;
  Addr:SockAddr_in;
  TimeOut:TimeVal;
  nSockErr:integer;
  i_port:integer;
  OpenNode:TTreeNode;
  First_Addr:Word;
  Second_Addr:Word;
  Third_Addr:Word;
  Fourth_Addr:Word;
  HostIP:string;
begin
  BitBtn1.Enabled:=False;
  SendMessage(Single_Edit,IPM_GETADDRESS,0,longint(@SingleHostAddr));
  if SingleHostAddr=0 then  //确定输入的ip不为空
  begin
   beep;
   ShowMessage('请输入对方主机IP');
   SendMessage(Single_Edit,IPM_SETFOCUS,0,0);                //select the first field
   //modalResult:=mrNone;
   BitBtn1.Enabled:=True;
   Exit;
  end;
  SingleHostAddr:=htonl(SingleHostAddr);
  First_Addr:=First_IpAddress(SingleHostAddr);
  Second_Addr:=SECOND_IPADDRESS(SingleHostAddr);
  Third_Addr:=THIRD_IPADDRESS(SingleHostAddr);
  Fourth_Addr:=FOURTH_IPADDRESS(SingleHostAddr);
  HostIP:=IntToStr(Fourth_Addr)+'.'+IntToStr(Third_Addr)+'.'+
            IntToStr(Second_Addr)+'.'+IntToStr(First_Addr);
  TreeView1.Items.Clear;
  TreeView1.Font.Color:=clBlue;
  //OpenNode:=TreeView1.Items.Add(nil,HostIP);
  TreeView1.Items.Add(nil,HostIP);
  wVersionRequested:=MakeWord(1,1);
  nSockErr:=WSAStartup(wVersionRequested,WSAData);
  if nSockErr<>0 then
    //初始化失败,退出程序
    begin
      ShowMessage('找不到所需要的DLL文件!');
      Halt;
    end;
   //此程序是否支持ws1.1版本
   if (LOBYTE(WSAData.wVersion)<>1) or (HIBYTE(WSAData.wVersion)<>1) then
     begin
       ShowMessage('无法使用版本1.1');
       WSACleanup();
       Halt;
     end;
  SendMessage(Single_Edit,IPM_GETADDRESS,0,longint(@SingleHostAddr));
  SingleHostAddr:=htonl(SingleHostAddr);
  First_Addr:=First_IpAddress(SingleHostAddr);
  Second_Addr:=SECOND_IPADDRESS(SingleHostAddr);
  Third_Addr:=THIRD_IPADDRESS(SingleHostAddr);
  Fourth_Addr:=FOURTH_IPADDRESS(SingleHostAddr);
  HostIP:=IntToStr(Fourth_Addr)+'.'+IntToStr(Third_Addr)+'.'+
            IntToStr(Second_Addr)+'.'+IntToStr(First_Addr);
   TreeView1.Items.Clear;
   OpenNode:=TreeView1.Items.Add(nil,HostIP);
    //测试端口是否打开
    //创建套接字
   sClntSock:=Socket(AF_INET,SOCK_STREAM,0);
   addr.sin_family:=AF_INET;
   //addr.sin_addr.S_addr:=inet_addr(pchar(Edit2.Text));
   addr.sin_addr.S_addr:=SingleHostAddr;
   for i_port:=0 to CheckListBox1.Items.Count-1 do
     begin
     if CheckListBox1.Checked[i_port] then
       begin
        //创建套接字
       { sClntSock:=Socket(AF_INET,SOCK_STREAM,0);
        addr.sin_family:=AF_INET;
        //addr.sin_addr.S_addr:=inet_addr(pchar(Edit2.Text));
        addr.sin_addr.S_addr:=SingleHostAddr;
       // showmessage(inttostr(addr.sin_addr.s_addr));}
        addr.sin_port:=htons(StrToInt(CheckListBox1.Items.Strings[i_port]));
        if Connect(sClntSock,addr,sizeof(addr))<>0 then
          begin
             TimeOut.tv_sec:=2;
             TimeOut.tv_usec:=0;
             //TreeView1.Font.Color:=clRed;
             TreeView1.Items.AddChild(OpenNode,CheckListBox1.Items.Strings[i_port]+' 端口未打开');
          end
        else
          begin
             TimeOut.tv_sec:=2;
             TimeOut.tv_usec:=0; 
             //TreeView1.Font.Color:=clGreen;
             //OpenNode:=TreeView1.Items.Add(nil,HostIP);
             TreeView1.Items.AddChild(OpenNode,CheckListBox1.Items.Strings[i_port]+' 端口已打开');
          end;
     end;
   end;
   CloseSocket(sClntSock);
   BitBtn1.Enabled:=True;
end;


//BitBtn2实现的功能是IP段的端口扫描
//目前程序还有bug,当Host不通时,会导致程序不响应
procedure TForm1.BitBtn2Click(Sender: TObject);
var
  ip:integer;
  wVersionRequested:Word;
  WSAData:TWSAData;
  sClntSock:TSocket;
  Addr:SockAddr_in;
  TimeOut:TimeVal;
  nSockErr:integer;
  i_port:integer;
  OpenNode:TTreeNode;
  First_Addr:Word;
  Second_Addr:Word;
  Third_Addr:Word;
  Fourth_Addr:Word;
  HostIP:string;
  CurIP:integer;
begin
  BitBtn2.Enabled:=False;
  TreeView1.Items.Clear;
  TreeView1.Font.Color:=clBlue;
  wVersionRequested:=MakeWord(1,1);
  nSockErr:=WSAStartup(wVersionRequested,WSAData);
  if nSockErr<>0 then
    //初始化失败,退出程序
    begin
      ShowMessage('找不到所需要的DLL文件!');
      Halt;
    end;
   //此程序是否支持ws1.1版本
   if (LOBYTE(WSAData.wVersion)<>1) or (HIBYTE(WSAData.wVersion)<>1) then
     begin
       ShowMessage('无法使用版本1.1');
       WSACleanup();
       Halt;
     end; 
  SendMessage(ipEdit,IPM_GETADDRESS,0,longint(@ipAddress));
  SendMessage(ipEdit1,IPM_GETADDRESS,0,longint(@endip));
  if (ipAddress=0) or (endip=0)  then
     begin
      ShowMessage('IP初始化有错误,请重新输入!');
      BitBtn2.Enabled:=True;
      SendMessage(IpEdit,IPM_SETFOCUS,0,0);
      Exit;
    end;
    sClntSock:=Socket(AF_INET,SOCK_STREAM,0);
    addr.sin_family:=AF_INET;
  for ip:=ipAddress to  endip do
     begin
      CurIP:=ip;
      First_Addr:=First_IpAddress(CurIP);
      Second_Addr:=SECOND_IPADDRESS(CurIP);
      Third_Addr:=THIRD_IPADDRESS(CurIP);
      Fourth_Addr:=FOURTH_IPADDRESS(CurIP);
      HostIP:=IntToStr(First_Addr)+'.'+IntToStr(Second_Addr)+'.'+
           IntToStr(Third_Addr) +'.'+IntToStr(Fourth_Addr);
      OpenNode:=TreeView1.Items.Add(nil,HostIP);
      {sClntSock:=Socket(AF_INET,SOCK_STREAM,0);
      addr.sin_family:=AF_INET;}
      //addr.sin_addr.S_addr:=inet_addr(pchar(Edit2.Text));
      addr.sin_addr.S_addr:=inet_addr(pchar(HostIp));
      // showmessage(inttostr(addr.sin_addr.s_addr));
      for i_port:=0 to CheckListBox1.Items.Count-1 do
       if CheckListBox1.Checked[i_port] then
       begin
        //创建套接字
        {sClntSock:=Socket(AF_INET,SOCK_STREAM,0);
        addr.sin_family:=AF_INET;
        //addr.sin_addr.S_addr:=inet_addr(pchar(Edit2.Text));
        addr.sin_addr.S_addr:=inet_addr(pchar(HostIp));
       // showmessage(inttostr(addr.sin_addr.s_addr));}
        addr.sin_port:=htons(StrToInt(CheckListBox1.Items.Strings[i_port]));
        if Connect(sClntSock,addr,sizeof(addr))<>0 then
          begin
             //ShowMessage('Error here');
             TimeOut.tv_sec:=2;
             TimeOut.tv_usec:=0;
             TreeView1.Items.AddChild(OpenNode,CheckListBox1.Items.Strings[i_port]+' 端口未打开');
          end
        else
          begin
             TimeOut.tv_sec:=2;
             TimeOut.tv_usec:=0;
             //TreeView1.Font.Color:=clBlue;
             TreeView1.Items.AddChild(OpenNode,CheckListBox1.Items.Strings[i_port]+' 端口已打开');
          end;
       end;
  end;
  CloseSocket(sClntSock);
  BitBtn2.Enabled:=True;  
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  with TForm2.Create(Form2) do
    begin
       Show;
       Refresh;
       Sleep(3000);
       Free;
    end;
end;

end.


unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, jpeg, ExtCtrls;

type
  TForm2 = class(TForm)
    Image1: TImage;
    StaticText1: TStaticText;
    Label1: TLabel;
    Label2: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

end.

地主 发表时间: 09/21 22:35

回复: sainthero [sainthero]   论坛用户   登录
功能是能够实现加入端口号
单个主机扫描
多个主机扫描
是不是在作多个主机扫描的时候一定要用多线程


B1层 发表时间: 09/21 22:38

回复: ricky [ricky]   版主   登录
多线程一定要的,要不谁愿意等那么长时间啊

B2层 发表时间: 09/21 23:42

论坛: 原创软件

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号