使用Perl编写协议分析脚本
创建时间:2005-07-31 更新时间:2005-08-07
文章属性:原创
文章提交:r00t (i_am_jojo_at_msn.com)
1、软件环境:Windows、ActiveState Perl 5.8.6、Winpcap 3.1 Beta;
2、所需Perl 模块:Net::Pcap、Net::PcapUtils、NetPacket
   >ppm install NetPacket
   >ppm install http://www.bribes.org/perl/ppm/Net-Pcap.ppd
   >ppm install http://www.bribes.org/perl/ppm/Net-PcapUtils.ppd
其中Net::Pcap是Winpcap的接口,Net::PcapUtils提供常用的抓包函数,NetPacket用来解析各种协议结构;
3、仅分析ICMP和TCP协议结构,且把Header结构用文本表格的方式打印下来;
4、提供源地址、目的地址、源端口、目的端口组合的过滤方式;
5、如果需要Dump ARP协议的数据包,则需要按照额外的模块,NetPacket当前不支持ARP协议的解析;
6、可使用perl2exe工具将其转化到exe文件格式;
(0)、C:/Perl/scripts/iSniffer>packetDump.pl -h
#Please set the width of CMD window to 100 #为了格式化显示请将窗口长度设置到100
  > C:/Perl/scripts/iSniffer/packetDump.pl [hvd:p:i:s:t:u::x:y:z:]
    -h    print this help
    -v    print more information    #显示更多的信息
    -d    choice device, [1,2,3...] #指定设备编号
    -p    1->icmp, 6->tcp           #分析哪种协议,ICMP或TCP
    -i    icmp type                 #-p 1前提下指定ICMP的Type
             0     Echo Reply
             3     Destination Unreachable
             4     Source Quench
             5     Redirect
             8     Echo
            11     Time Exceeded
            12     Parameter Problem
            13     Timestamp
            14     Timestamp Reply
            17     Address Mask Request
            18     Address Mask Reply
            30     Traceroute
            37     Domain Name Request
    -s    x.x.x.x, source ip       #指定源地址
    -t    x.x.x.x, dest ip         #指定目的地址
    -u    x.x.x.x, source/dest ip  #源地址或目的地址均可
    -x    source port              #指定源端口
    -y    dest port                #指定目的端口
    -z    source/dest port         #源端口或目的端口均可
                v1.0, by shanleiguang@he.chinamobile.com
(1)、>packetDump.plC:/Perl/scripts/iSniffer>packetDump.pl
+-----------------------------------------------------------------------------------+
    | Supported Devices                                                                 |
    +---+------+------------------------------------------------------------------------+
    | 1 | dev  | /Device/NPF_GenericNdisWanAdapter                                      |
    +---+------+------------------------------------------------------------------------+
    |   | desc | Generic NdisWan adapter                                                |
    +---+------+------------------------------------------------------------------------+
    | 2 | dev  | /Device/NPF_{6A06FB50-D0BC-4908-A502-90322DC74B78}                     |
    +---+------+------------------------------------------------------------------------+
    |   | desc | Intel(R) PRO/100 VE Network Connection (Microsoft's Packet Scheduler)  |
    +---+------+------------------------------------------------------------------------+
    | 3 | dev  | /Device/NPF_{762D2D02-BA2C-46E1-9C54-396D8B79055F}                     |
    +---+------+------------------------------------------------------------------------+
    |   | desc | WAN (PPP/SLIP) Interface                                               |
    +---+------+------------------------------------------------------------------------+
Which device u want to sniff? [1,2,3] #选择希望Dump的设备,下一次可直接用-d来指配
(2)、C:/Perl/scripts/iSniffer>packetDump.pl -d 3 -p 1
2005/07/31 11:15:58, Sniffing on /Device/NPF_{762D2D02-BA2C-46E1-9C54-396D8B79055F}... ...
=No.1===========================================================================
    +------------------------------------------------+
    | IP Header                                      |
    +--------+------------+---------+----------------+
    | ver    | 4          | hlen    | 5              |
    +--------+------------+---------+----------------+
    | tos    | 0          | len     | 60             |
    +--------+------------+---------+----------------+
    | flags  | 0          | foffset | 0              |
    +--------+------------+---------+----------------+
    | id     | 50223      | ttl     | 128            |
    +--------+------------+---------+----------------+
    | src_ip | 60.6.41.89 | dest_ip | 64.233.189.104 |
    +--------+------------+---------+----------------+
    | proto  | 1          | cksum   | 4833           |
    +--------+------------+---------+----------------+
    +--------------------------------------------------------+
    | ICMP Message                                           |
    +------+------+-------+----------------------------------+
    | type | code | cksum | data                             |
    +------+------+-------+----------------------------------+
    | 8    | 0    | 17756 | abcdefghijklmnopqrstuvwabcdefghi |
    +------+------+-------+----------------------------------+
=No.2===========================================================================
    +------------------------------------------------+
    | IP Header                                      |
    +--------+----------------+---------+------------+
    | ver    | 4              | hlen    | 5          |
    +--------+----------------+---------+------------+
    | tos    | 0              | len     | 60         |
    +--------+----------------+---------+------------+
    | flags  | 0              | foffset | 0          |
    +--------+----------------+---------+------------+
    | id     | 50223          | ttl     | 242        |
    +--------+----------------+---------+------------+
    | src_ip | 64.233.189.104 | dest_ip | 60.6.41.89 |
    +--------+----------------+---------+------------+
    | proto  | 1              | cksum   | 41184      |
    +--------+----------------+---------+------------+
    +--------------------------------------------------------+
    | ICMP Message                                           |
    +------+------+-------+----------------------------------+
    | type | code | cksum | data                             |
    +------+------+-------+----------------------------------+
    | 0    | 0    | 19804 | abcdefghijklmnopqrstuvwabcdefghi |
    +------+------+-------+----------------------------------+
... ...
(3)、C:/Perl/scripts/iSniffer>packetDump.pl -d 3 -p 6 -u xxx.xxx.xxx.xxx -z 23
7、源代码
#!C:/Perl/bin/perl.exe
#By shanleiguang@he.chinamobile.com, 2005/07
#ActiveState Perl 5.8.6, Winpcap 3.1 beta
#ppm install NetPacket
#ppm install http://www.bribes.org/perl/ppm/Net-Pcap.ppd
#ppm install http://www.bribes.org/perl/ppm/Net-PcapUtils.ppd
use strict;
use Net::PcapUtils;
use NetPacket::Ethernet;
use NetPacket::IP;
use NetPacket::ICMP;
use NetPacket::TCP;
use Getopt::Std;
use POSIX qw(strftime);
my %opts;
getopts('hvd:p:i:s:t:u:x:y:z:', /%opts);
print_help() and exit if(defined $opts{'h'});
print_help() and exit if(defined $opts{'d'} and ($opts{'d'} !~ m/^/d+$/));
print_help() and exit if(defined $opts{'p'} and ($opts{'p'} !~ m/^/d+$/));
print_help() and exit if(defined $opts{'i'} and ($opts{'i'} !~ m/^/d+$/));
print_help() and exit if(defined $opts{'s'} and ($opts{'s'} !~ m/^/d+./d+./d+./d+$/));
print_help() and exit if(defined $opts{'t'} and ($opts{'t'} !~ m/^/d+./d+./d+./d+$/));
print_help() and exit if(defined $opts{'u'} and ($opts{'u'} !~ m/^/d+./d+./d+./d+$/));
print_help() and exit if(defined $opts{'x'} and ($opts{'x'} !~ m/^/d+$/));
print_help() and exit if(defined $opts{'y'} and ($opts{'y'} !~ m/^/d+$/));
print_help() and exit if(defined $opts{'z'} and ($opts{'z'} !~ m/^/d+$/));
$opts{'p'} = 6 if not defined($opts{'p'});
my $choice;
my %devices = get_supported_devices();
if(defined $opts{'d'}) {
    $choice = $opts{'d'};
} else {
    print_supported_devices();
   
    print "/nWhich device u want to sniff? [";
    print join ',', sort {$a <=> $b} (keys %devices) and print '] ';
   
    $choice = <STDIN>;
    chomp($choice);
}
die "Invalid Device!/n" if not defined($devices{$choice});
my $pkt_descriptor = Net::PcapUtils::open(
    FILTER  => 'ip',
    SNAPLEN => 1500,
    PROMISC => 1,
    DEV     => $devices{$choice}{'dev'},
);
die "Net::PcapUtils::open returned: $pkt_descriptor/n" if (!ref($pkt_descriptor));
print strftime "%Y/%m/%d %H:%M:%S, ", localtime;
print "Sniffing on $devices{$choice}{'dev'}... .../n";
my ($next_packet, %next_header);
my $packet_counter = 0;
while (($next_packet, %next_header) = Net::PcapUtils::next($pkt_descriptor)) {
    my ($ip_obj, $tcp_obj, $icmp_obj);
    $ip_obj = NetPacket::IP->decode(NetPacket::Ethernet::eth_strip($next_packet));
   
    next if (defined $opts{'s'} and ($ip_obj->{'src_ip'} ne $opts{'s'}));
    next if (defined $opts{'t'} and ($ip_obj->{'dest_ip'} ne $opts{'t'}));
    next if (defined $opts{'u'} and ($ip_obj->{'src_ip'} ne $opts{'u'})
        and ($ip_obj->{'dest_ip'} ne $opts{'u'}));
   
    next if ($ip_obj->{'proto'} != $opts{'p'});
if ($ip_obj->{'proto'} == 1) {
        $icmp_obj = NetPacket::ICMP->decode($ip_obj->{'data'});
        next if (defined $opts{'i'} and ($icmp_obj->{'type'} ne $opts{'i'}));
    }
   
    if ($ip_obj->{'proto'} == 6) {
        $tcp_obj = NetPacket::TCP->decode($ip_obj->{'data'});
        next if (defined $opts{'x'} and ($tcp_obj->{'src_port'} ne $opts{'x'}));
        next if (defined $opts{'y'} and ($tcp_obj->{'dest_port'} ne $opts{'y'}));
        next if (defined $opts{'z'} and ($tcp_obj->{'src_port'} ne $opts{'z'})
            and ($tcp_obj->{'dest_port'} ne $opts{'z'}));
    }
   
    $packet_counter++;
   
    print "=No.$packet_counter=", '=' x (80 - length("=No.$packet_counter=")), "/n";
    if($opts{'v'}) {
        print display_capinfo(/%next_header);
        print display_frame_hdr(NetPacket::Ethernet->decode($next_packet));
    }
    print display_ip_hdr($ip_obj);
    print display_icmp_msg($icmp_obj) if ($ip_obj->{'proto'} == 1);
    print display_tcp_hdr($tcp_obj) if ($ip_obj->{'proto'} == 6);
}
sub print_help {
    print <<HELP
   
  #Please set the width of CMD window to 100
  > $0 [hvd:p:i:s:t:u::x:y:z:]
    -h    print this help
    -v    print more information
    -d    choice device, [1,2,3...]
    -p    1->icmp, 6->tcp
    -i    icmp type
             0     Echo Reply
             3     Destination Unreachable
             4     Source Quench
             5     Redirect
             8     Echo
            11     Time Exceeded                      
            12     Parameter Problem
            13     Timestamp
            14     Timestamp Reply
            17     Address Mask Request
            18     Address Mask Reply
            30     Traceroute
            37     Domain Name Request
    -s    x.x.x.x, source ip
    -t    x.x.x.x, dest ip
    -u    x.x.x.x, source/dest ip
    -x    source port
    -y    dest port
    -z    source/dest port
                v1.0, by shanleiguang/@he.chinamobile.com
HELP
}
sub get_supported_devices {
    my ($error, %description, %devices);
    my $index = 0;
   
    foreach (Net::Pcap::findalldevs(/$error, /%description)) {
        die "Net::Pcap::finealldevs Error!/n" if defined $error;
        $index++;
        $devices{$index}{'dev'} = $_;
        $devices{$index}{'desc'} = $description{$_};
    }
   
    return %devices;
}
sub print_supported_devices {
    my ($error, %description);
    my (@indexes, @fields, @values);
    my $index = 0;
   
    foreach (Net::Pcap::findalldevs(/$error, /%description)) {
        die "Net::Pcap::finealldevs Error!/n" if defined $error;
        $index++;
        push @indexes, ($index, ' ');
        push @fields, ('dev', 'desc');
        push @values, ($_, $description{$_});
    }
print "/n", pretty_table('Supported Devices', (/@indexes, /@fields, /@values));
}
sub display_capinfo {
    my $capinfo = shift;
    my @capinfo;
    push @capinfo, [$_, $capinfo->{$_}] foreach (qw(tv_sec tv_usec len caplen));
    return pretty_table('Pcap Info', @capinfo);
}
sub display_frame_hdr {
    my $frame_obj = shift;
    my @eth_frame;
    push @eth_frame, [$_, $frame_obj->{$_}] foreach (qw(src_mac dest_mac type));
    return pretty_table('Ethernet Frame Header', @eth_frame);
}
sub display_ip_hdr {
    my $ip_obj = shift;
    my @ip_hdr;
    push @ip_hdr, [qw(ver tos flags id src_ip proto)];
    push @{$ip_hdr[1]}, $ip_obj->{$_} foreach (qw(ver tos flags id src_ip proto));
    push @ip_hdr, [qw(hlen len foffset ttl dest_ip cksum)];
    push @{$ip_hdr[3]}, $ip_obj->{$_} foreach (qw(hlen len foffset ttl dest_ip cksum));
    return pretty_table('IP Header', @ip_hdr);
}
sub display_icmp_msg {
    my $icmp_obj = shift;
    my @icmp_msg;
    $icmp_obj->{'data'} =~ s//W//g;
    push @icmp_msg, [$_, $icmp_obj->{$_}] foreach (qw(type code cksum data));
    return pretty_table('ICMP Message', @icmp_msg);
}
sub display_tcp_hdr {
    my $tcp_obj = shift;
    my @tcp_hdr;
    push @tcp_hdr, [qw(src_port seqnum hlen flags)];
    push @{$tcp_hdr[1]}, $tcp_obj->{$_} foreach (qw(src_port seqnum hlen flags));
    push @tcp_hdr, [qw(dest_port acknum reserved winsize)];
    push @{$tcp_hdr[3]}, $tcp_obj->{$_} foreach (qw(dest_port acknum reserved winsize));
    return pretty_table('TCP Header', @tcp_hdr);
    #my $data = unpack 'a*', $tcp_obj->{'data'};
    #print "$data/n";
}
sub display_udp_hdr {
    my $udp_obj = shift;
    my @udp_hdr;
    push @udp_hdr, [$_, $udp_obj->{$_}] foreach (qw(src_port dest_port cksum));
    return pretty_table('UDP Header', @udp_hdr);
}
sub pretty_table {
    # pretty_table($aString, @aList); @aList = ( [...], [...] );
    my ($title, @data) = @_;
    my @temp;
    my @maxLength;
    my $rowLength;
    my $indent = 4;
    my $theTable;
foreach my $col (0..$#{$data[0]}) { push @{$temp[$col]}, $_->[$col] foreach (@data); }
    $maxLength[$_] = length( (sort{length($b) <=> length($a)} @{$data[$_]} )[0]) + 2 foreach (0..$#data);
    $rowLength+= $maxLength[$_] foreach (0..$#{$temp[0]}); 
    $rowLength+= $#data;
$theTable = ' ' x $indent.'+'.'-' x $rowLength."+/n";
    $theTable.= ' ' x $indent.'| '.$title.' ' x ($rowLength - length($title) - 1)."|/n";
    foreach my $row (0..$#temp) {
        $theTable.= ' ' x $indent;
        $theTable.= '+'.'-' x $maxLength[$_] foreach (0.. $#{$temp[0]});
        $theTable.= "+/n";
        $theTable.= ' ' x $indent;
        $theTable.= '| '.@{$temp[$row]}[$_].' ' x ($maxLength[$_] - length(@{$temp[$row]}[$_]) - 1)
            foreach (0.. $#{$temp[0]});
        $theTable.= "|/n";
    }
    $theTable.= ' ' x $indent;
    $theTable.= '+'.'-' x $maxLength[$_] foreach (0.. $#{$temp[0]});
    $theTable.= "+/n";
   
    return $theTable;
}

<script type="text/javascript"></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

使用Perl编写协议分析脚本 z相关推荐

  1. 使用Perl编写协议分析脚本

    使用Perl编写协议分析脚本 创建时间:2005-07-31 更新时间:2005-08-07 文章属性:转载 文章提交:r00t (i_am_jojo_at_msn.com) 1.软件环境:Windo ...

  2. (转载)使用Perl编写协议分析脚本

    使用Perl编写协议分析脚本 创建时间:2005-07-31 更新时间:2005-08-07 文章属性:原创 文章提交:r00t (i_am_jojo_at_msn.com) 1.软件环境:Windo ...

  3. 网络协议分析与仿真课程设计报告:网络流量分析与协议模拟

    公众号:CS阿吉 网络协议分析与仿真课程设计报告  题  目:网络流量分析与协议模拟 专业名称:         网络工程 班    级: 学生姓名:           阿吉 学号(8位): 指导教 ...

  4. 如何编写一个shell脚本

    本文结合大量实例阐述如何编写一个shell脚本. 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而 ...

  5. loadrunner录制事件为0_利用LoadRunner编写Socket性能测试脚本简述

    >>>推荐阅读<<< 1.性能测试学习笔记-场景设计 2.性能测试的重要意义 3.性能分析流程及方法 4.应用系统性能调优之性能分析 一.概述 Loadrunner ...

  6. Linux下的网络协议分析工具-tcpdump快速入门手册

    TCPDUMP简介 在传统的网络分析和测试技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一.sniffer工具首先是为网络管理员和网络程序员进行网络分析而设计的.对于网络管理人员来说,使 ...

  7. arp协议分析python编程实现arp欺骗抓图片

    arp协议分析&python编程实现arp欺骗抓图片 序 学校tcp/ip协议分析课程老师布置的任务,要求分析一种网络协议并且研究安全问题并编程实现,于是我选择了研究arp协议,并且利用pyt ...

  8. 华中科技大学计算机与网络,华中科技大学计算机通信与网络实验报告-基于NS2的协议分析实验...

    华中科技大学计算机通信与网络实验报告-基于NS2的协议分析实验 (26页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 19.9 积分 实验二基于NS2的协 ...

  9. 干货|app自动化测试之Appium 原理 与 JsonWP 协议分析

    想要使用 Appium 进行测试,那么就一定要先了解Appium的原理.Appium 不仅能够实现移动端的 JSONWP,并且延伸到了 Selenium 的 JSONWP,它能够控制不同移动设备的行为 ...

最新文章

  1. 自带数据线的迷你数显充电宝,旅途必备
  2. 第七章子查询练习_SQL学习:复杂查询
  3. docker化你的java应用(上)
  4. java中redis原理_Redis字符串键的底层原理
  5. php做微信小程序登录,php(ThinkPHP)实现微信小程序的登录过程
  6. 基于事件驱动架构构建微服务第2部分:领域对象和业务规则
  7. Studio 一些使用
  8. KesionICMS智能建站系统v8源码
  9. ActiveMq工作笔记003---SpringBoot集成ActiveMq_随时更新
  10. [设计模式-结构型]适配器(Adapter)
  11. zabbix 客户端自定义端口监控
  12. 动画特效九:下拉刷新
  13. 错误、调试和测试(4)-文档测试
  14. MyBatis-Plus 分页查询以及自定义sql分页
  15. ac3168无线网卡驱动下载_70块的笔记本网卡,值不值得换
  16. 5G时代芯片之王——射频芯片
  17. 那些年我们在Python掉进的坑——清除不想要的字符
  18. Enterprise Architect安装使用
  19. java 解析uri_Uri详解之——Uri结构与代码提取
  20. PDPS软件:机器人焊枪工具自动选取功能介绍与使用方法

热门文章

  1. 基于Visual C++2010 与office2010开发办公自动化(2)-自动生成excel与word并打开
  2. 常用英语口语句型100句
  3. 吴军《数学之美》第二版阅读整理
  4. TikTok跨境出海:TikTok的8种变现方式?
  5. 中小企业掀起“减碳潮”,“上云”提高产品绿色竞争力
  6. autoHotkey — 连击/双击/重复 按键触发
  7. Atracsys FusionTrack 500 光学测量系统
  8. 瑞萨电子第一家Preferred Partner-武汉万象奥科
  9. echarts地图上的标签为图片_百度地图标记点中添加Echarts图表
  10. 有关JAVA考试中数据库的题_全国2018年4月自考互联网数据库考试真题