Unit1.pas
上传用户:ionicmat
上传日期:2007-01-05
资源大小:22k
文件大小:5k
源码类别:

网络截获/分析

开发平台:

Delphi

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. Author:       Jagad (don@indo.net.id)
  3. Description:  Packet Sniffer for Windows 95
  4.               Uses Snowing sofware (PACKET32.DLL and ZPACKET.VXD) made by
  5.               Sang-Eun Han (seh@brabo1.korea.ac.kr). His source code available
  6.               from http://widecomm.korea.ac.kr/~seh).
  7.               Some updates by francois.piette@pophost.eunet.be (see history)
  8. History:
  9. May 17, 1999 FPiette Selected first adapter on startup
  10.                      Adapted for MacAddr as PChar
  11.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  12. unit Unit1;
  13. interface
  14. uses
  15.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  16.   StdCtrls, Ingusclass, Protohdr, IngusPacket;
  17. type
  18.   TForm1 = class(TForm)
  19.     ComboBox1: TComboBox;
  20.     Memo1: TMemo;
  21.     Button1: TButton;
  22.     Button2: TButton;
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure FormDestroy(Sender: TObject);
  25.     procedure Button1Click(Sender: TObject);
  26.     procedure Button2Click(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.     sIngus: TIngusSniffer;
  30.   public
  31.     { Public declarations }
  32.     procedure OnParsePacketHandle( nPacketSeq: Longint; uBuffer: PChar;
  33.                                    nRecvBytes: integer; sPacket: TIngusPacketBase );
  34.     procedure OnAfterGetAdapterDesc(bStatus: Boolean; sAdapterDesc: string);
  35.     procedure OnAfterGetMacAddress(bStatus: Boolean; pMacAddr: PChar);
  36.   end;
  37. var
  38.   Form1: TForm1;
  39. implementation
  40. {$R *.DFM}
  41. procedure TForm1.FormCreate(Sender: TObject);
  42. begin
  43.   sIngus := TIngusSniffer.Create;
  44.   sIngus.OnParsePacket := OnParsePacketHandle;
  45.   sIngus.OnAfterGetAdapterDesc := OnAfterGetAdapterDesc;
  46.   sIngus.OnAfterGetMacAddress := OnAfterGetMacAddress;
  47.   ComboBox1.Items.Assign(sIngus.AdapterNameList);
  48.   ComboBox1.ItemIndex := 0;       // Fp May 17, 1999
  49. end;
  50. procedure TForm1.FormDestroy(Sender: TObject);
  51. begin
  52.   sIngus.Free;
  53. end;
  54. procedure TForm1.OnParsePacketHandle( nPacketSeq: Longint; uBuffer: PChar;
  55.                                       nRecvBytes: integer; sPacket:
  56.                                       TIngusPacketBase );
  57. var
  58.   sMacAddr: string;
  59.   //pIPHdr: PIP_RHDR;
  60.   sIpPacket: TIngusIPPacket;
  61.   sIcmpPacket: TIngusICMPPacket;
  62.   sTCPPacket: TIngusTCPPacket;
  63.   nSrcPort, nDestPort: integer;
  64. begin
  65.   if sPacket.EthernetProtocol <> PROTO_IP then exit;
  66.   sIPPacket := TIngusIPPacket(sPacket);
  67.   // FPiette May 17, 1999
  68.   sMacAddr := Format( '**Mac address: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X',
  69.                       [ UCHAR(sIngus.MacAddr[0]), UCHAR(sIngus.MacAddr[1]),
  70.                         UCHAR(sIngus.MacAddr[2]), UCHAR(sIngus.MacAddr[3]),
  71.                         UCHAR(sIngus.MacAddr[4]), UCHAR(sIngus.MacAddr[5]) ] );
  72.   Memo1.Lines.Add('');
  73.   Memo1.Lines.Add(IntToStr(nPacketSeq));
  74.   Memo1.Lines.Add(sMacAddr);
  75.   Memo1.Lines.Add(Format('Source: %u.%u.%u.%u', [ UCHAR(sIPPacket.IPSourceAddr^),
  76.                                                   UCHAR((sIPPacket.IPSourceAddr+1)^),
  77.                                                   UCHAR((sIPPacket.IPSourceAddr+2)^),
  78.                                                   UCHAR((sIPPacket.IPSourceAddr+3)^) ]));
  79.   Memo1.Lines.Add(Format('Destination: %u.%u.%u.%u', [ UCHAR((sIPPacket.IPDestAddr)^),
  80.                                                        UCHAR((sIPPacket.IPDestAddr+1)^),
  81.                                                        UCHAR((sIPPacket.IPDestAddr+2)^),
  82.                                                        UCHAR((sIPPacket.IPDestAddr+3)^) ]));
  83.   //pIPHdr := PIP_RHDR(sPacket.Data);
  84.   //case pIPHdr^.Protocol of
  85.   case sIPPacket.IPProtocol of
  86.   1: begin
  87.        //ICMP
  88.        sIcmpPacket := TIngusICMPPacket(sPacket);
  89.        Memo1.Lines.Add('ICMP');
  90.      end;
  91.   6: begin
  92.        //TCP
  93.        sTCPPacket := TIngusTCPPacket(sPacket);
  94.        Memo1.Lines.Add('TCP');
  95.        nSrcPort := sTCPPacket.SourcePort;
  96.        nDestPort := sTCPPacket.DestPort;
  97.        Memo1.Lines.Add('PS: '+IntToStr(nSrcPort));
  98.        Memo1.Lines.Add('DS: '+IntToStr(nDestPort));
  99.      end;
  100.   end;
  101.   case sPacket.PacketDirection of
  102.   pdInput:
  103.     begin
  104.       Memo1.Lines.Add('Input');
  105.     end;
  106.   pdOutput:
  107.     begin
  108.       Memo1.Lines.Add('Output');
  109.     end;
  110.   end;
  111. end;
  112. procedure TForm1.OnAfterGetAdapterDesc(bStatus: Boolean; sAdapterDesc: string);
  113. begin
  114.   Memo1.Lines.Add('***Adapter Desc: '+sAdapterDesc);
  115. end;
  116. procedure TForm1.OnAfterGetMacAddress(bStatus: Boolean; pMacAddr: PChar);
  117. begin
  118.   Memo1.Lines.Add(Format( '***MAC Address: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X',
  119.                           [ UCHAR(pMacAddr^), UCHAR((pMacAddr+1)^), UCHAR((pMacAddr+2)^),
  120.                             UCHAR((pMacAddr+3)^), UCHAR((pMacAddr+4)^), UCHAR((pMacAddr+5)^) ] ));
  121. end;
  122. procedure TForm1.Button1Click(Sender: TObject);
  123. begin
  124.   //Start
  125.   Memo1.Lines.Clear;
  126.   sIngus.StartSnoop(ComboBox1.ItemIndex);
  127. end;
  128. procedure TForm1.Button2Click(Sender: TObject);
  129. begin
  130.   //Stop
  131.   sIngus.StopSnoop;
  132. end;
  133. end.