By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

[ADVANCED] NET:PcapUtils problem driving me nuts

P: 4
Hi all,

When I execute the following code, it's captures all the data going trough my network card, that goes fine. But then i want to print the headers of the packets, see the if-statement:

Expand|Select|Wrap|Line Numbers
  1. if (index($packet,"GET" gt 0)) {
  2.         $start = (index($packet,"GET"));
  3.         $end = (index($packet,"Content-Length: "));
  4.  
  5. } else {
  6.         $start = (index($packet,"POST"));
  7.         $end = length($packet);
  8. }
  9.  
When the HTTP POST method is used, it has to print the whole header, including the POST variables. So the $end is just the lenght of the whole packet. When the GET method is used, I only want to have the header, until "Content-Length" or so. But, when i execute this, it will only print the GET requests. When i swap the POST en GET in the if-statement, it will only display the POST requests. Like so:

Expand|Select|Wrap|Line Numbers
  1. if (index($packet,"POST" gt 0)) {
  2.         $start = (index($packet,"POST"));
  3.         $end = length($packet);
  4.  
  5. } else {
  6.         $start = (index($packet,"GET"));
  7.         $end = (index($packet,"Content-Length: "));
  8.  
  9. }
  10.  
I don't get this behavior, and it's driving me crazy, because I've tried alot of things..

Is there anyone who can help me out? Thanks in advance!!!



The whole script:
[/code]


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Net::PcapUtils;
  4. $,=' ';
  5. my $start;
  6. my $end;
  7.  
  8. my $error = Net::PcapUtils::loop(\&print_packet);
  9. die $error if $error;
  10.  
  11. sub print_packet {
  12.  
  13. my($user_data, $header, $packet) = @_;    
  14.  
  15. if (index($packet,"HTTP") gt 0){
  16.  
  17. if (index($packet,"GET" gt 0)) {
  18.         $start = (index($packet,"GET"));
  19.         $end = (index($packet,"Content-Length: "));
  20.  
  21. } else {
  22.         $start = (index($packet,"POST"));
  23.         $end = length($packet);
  24. }
  25.  
  26. print substr ($packet, $start,($end-$start))."\n";
  27.  
  28. }
  29. }
  30.  
Oct 1 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
do you want to use ">" instead of "gt"?
Oct 1 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.