• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[开源]Delphi下WinPCap开发基础一个简单的HttpSniffer

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Delphi开发WinPcap程序一直是个门槛,这个代码之前在netexpert发过,但是问“如何用Delphi写网络协议分析?”、“Delphi怎么发送ARP包”这类问题的人还是大有人在。Delphi不能开发Sniffer?不能进行底层数据包收发?跨过这条门槛,一切易如反掌。

 

一直在考虑是否要把这几个代码拿出来,因为有些程序危害还比较大(比如ARP冲突攻击、SYN Flood那几个),留着也是留着还是拿出来开源得了。另外,虽然网上有个转换过的WinPcap头文件,但怎么用怎么不顺手。这里是通过Dev-C++写了一个DLL,通过调用这个DLL操作WinPcap库(代码都在里面,很简单)。

 

这是第一个程序,代码量很小,算是Sniffer的基础,源码见文章末尾。过两天把另外的几个也整理出来...

欢迎随便转载,希望能普及Delphi WinPcap开发
原文发表在:http://www.netexpert.cn/thread-20671-1-1.html

这次是Sniffer的,下一篇打算发布SYN Flood的(伪造数据包)。代码中有些地方注释不全,不过可以先到这里下载之前的版本:http://www.netexpert.cn/thread-20673-1-1.html


一个简单的Sniffer例子程序,抓取TCP 80端口的数据包,并过滤出POST和GET消息,如图:

 

打开网卡、设置过滤器,以及HTTP数据包处理部分的代码如下:

********************************************
 *
 *  打开网卡并设置过滤器,只要HTTP数据
 *
 ********************************************}
procedure TForm1.btn_OpenAdapterClick(Sender: TObject);
var
  DeviceName: String;
  iRet: Integer;
begin
  
if ComboBoxAdapterName.Text = '' then Exit;
  DeviceName :
= ComboBoxAdapterName.Text;

  
{ 打开网卡设备 }
  
if ( -1 = OpenAdapter(PChar(DeviceName), 100) ) then
  
begin
    Self.Caption :
= StrPas(GetPCapError());
    Exit;
  
end;

  
{ 设置过滤器,只要 TCP 80 端口的 (语法请参见libpcap或tcpdump的文档) }
  iRet :
= SetFilter('tcp port 80');
  
if (iRet <> 0then
  
begin
    
{ 可能是过滤器语法错误 }
    Self.Caption :
= Format('SetFilter Error = %d', [iRet]);
  
end;

  btn_OpenAdapter.Enabled :
= False;
  ComboBoxDevice.Enabled :
= False;

  
{ 开始抓包 }
  PacketLoop();
end;


{********************************************
 *
 *  数据包处理:从网卡上读一个数据包并分析
 *
 ********************************************
}
procedure TForm1.ProcessPacket();
var
  iRet: Integer;
  pkt_hdr: PPCap_pkthdr;
  pkt_data: PByte;
  len, tcplen: Cardinal;
  httpdata: PChar;
begin
  
{ 读取数据包 }
  iRet :
= ReadPacket(@pkt_hdr, @pkt_data);
  
{ 长度大于0表成功 }
  
if (iRet > 0then
    
begin
      len :
= pkt_hdr^.PacketLength;   // 数据包长度
      tcplen :
= (PTCPPacket(pkt_data)^.TCPhdr.LenRes and $F0) div 4// TCP长度
      
{ 计算负载大小 }
      len :
= len - (14+20+tcplen);
      
if len > 0 then
      
begin
        
{ 取得HTTP数据包内容 }
        httpdata :
= PChar(Cardinal(pkt_data)+14+20+tcplen);
        httpdata[len] :
= #0;        // 修正行尾
        
        
{ 检查字符串 GET }
        
if CheckBox_ShowGET.Checked then      // 过滤GET
        
if (httpdata[0= 'G')
          
and (httpdata[1= 'E')
          
and (httpdata[2= 'T')
          
and (httpdata[3= ' 'then
        
begin
          Memo1.Lines.Add(Format(
'[%s] Packet Size: %d', [FormatDateTime('h:mm:ss', time_t2DateTime(pkt_hdr^.DateTime)), pkt_hdr^.SliceLength]));
          Memo1.Lines.Add(StrPas(httpdata));
        
end;

        
{ POST }
        
if (httpdata[0= 'P')
          
and (httpdata[1= 'O')
          
and (httpdata[2= 'S')
          
and (httpdata[3= 'T'then
        
begin
          Memo1.Lines.Add(Format(
'[%s] Packet Size: %d', [FormatDateTime('h:mm:ss', time_t2DateTime(pkt_hdr^.DateTime)), pkt_hdr^.SliceLength]));
          Memo1.Lines.Add(StrPas(httpdata));

          
{ 检查是不是一个包就发完所有数据 }
          
if (pkt_hdr^.SliceLength >= 1514then
          
begin
            
// 继续读入下一个包
            ProcessPacket();
          
end;
          Memo1.Lines.Add(
'.'#13#10);
        
end;
      
end;
    
end;
end;

procedure TForm1.PacketLoop();
begin
  fRunning :
= True;

  
{ 循环抓取数据包并分析 }
  
while (fRunning) do
  
begin
    ProcessPacket();

    Application.ProcessMessages;
  
end;
end;

 


 程序用途,捕获HTTP的GET和POST消息,用于截获URL的访问以及表单的提交(比如只监听POST消息,可以抓到论坛登陆的帐号和密码)。例如抓到的输出如下:


POST /chklogin.do HTTP/1.1
Accept: image
/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: ht tp:
//ww w.webgame.com.cn/
Accept
-Language: zh-cn
User
-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; TheWorld)
Content
-Type: application/x-www-form-urlencoded
UA
-CPU: x86
Accept
-Encoding: gzip, deflate
Host: passport.webgame.com.cn
Content
-Length: 145
Connection: Keep
-Alive
Cache
-Control: no-cache
Cookie: __utma
=257446157.4392135229605150700.1219543473.1219543473.1219543473.1; __utmz=257446157.1219543473.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

username
=abcdefg&password=123456&method=login&login_md5=e10adc3949ba59abbe56e057f20f883e&method=login_pw&gameType=&forward=&image.x=58&image.y=14
.


第二行:POST /chklogin.do HTTP/1.1 可以看到,这里在验证登陆信息
一般用户名和密码都在Cookie中,找到Cookie这行:
Cookie: __utma=257446157.4392135229605150700.1219543473.1219543473.1219543473.1; __utmz=257446157.1219543473.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
username=abcdefg&password=123456&method=login&login_md5=e10adc3949ba59abbe56e057f20f883e&method=login_pw&gameType=&forward=&image.x=58&image.y=14
.
看到了,在 username=abcdefg&password=123456 这里清清楚楚的写着刚才提交的用户名是abcdefg,密码为123456(当然是我随便输的)。自己试试一下效果吧。

 

源码(Source)httpSniffer_src.rar
Linux版的源码(使用libpcap):httpSniffer_src4Linux.rar

可执行程序:httpSniffer_bin.rar


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Matlab输出显示发布时间:2022-07-18
下一篇:
MATLAB 的数据导入与导出 - CuriousZero发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap