I am looking for a way to accurately get the URL data from the tcp->{data} value using NetPacket. What I have right now seems to work but the data I am getting is sometimes incomplete. Any help on making this more accurate? What I need to do is quickly detect executable, pdf or other downloads so my other app can use the URL to retrieve the binary, analyze it and then if something is out of the ordinary perform further actions.
... my $object = Net::Pcap::open_live($dev, 1024, 1, 0, \$err); ... sub token_data { my $data = shift; my @items = split (/\n|\r\n|\r/,$data); my $item; my $host = ''; my $uri = ''; foreach $item (@items) { if ($item =~ /GET\s+(.*)\s+HTTP.*/) { $uri= $1; } if ($item =~ /Host:\s+(.*)/) { $host = $1; } if ($item =~ /User-Agent:\s+(.*)/) { $agent = $1; $agent =~ s/\(//g; $agent =~ s/\)//g; } } return "$host,$uri,$agent"; } sub process_packet { my($user_data, $header, $pkt) = @_; my $ether_data = NetPacket::Ethernet::strip($pkt); my $ip = NetPacket::IP->decode($ether_data); my $tcp = NetPacket::TCP->decode(ip_strip(eth_strip($pkt))); if ($tcp->{data} =~ /GET|POST|get|post/) { if ($tcp->{data} =~ /.*Host:\s+(.*)/) { my $result = token_data ($tcp->{data}); print "DEBG::$ip->{'src_ip'},$ip->{'dest_ip'},$tcp->{'de +st_port'},$result\n"; } } }

In reply to libpcap, netpacket and decoding http data by gk2010

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.