in reply to Re^16: p0fq.pl and pack
in thread p0fq.pl and pack?

There's no such thing as a string in C. Strings are just arrays of characters treated specially. So that means you need to convert the array of chars into a string yourself.

foreach (qw( genre detail link tos )) { $data->{$_} = pack('c*', $data->{$_}); $data->{$_} =~ s/\x00.*//; }

Better yet, you can tell Concert::Binary::C whether a certain field should be treated as a string or as an array.

$c->tag('p0f_response.genre', Format => 'String'); $c->tag('p0f_response.detail', Format => 'String'); $c->tag('p0f_response.link', Format => 'String'); $c->tag('p0f_response.tos', Format => 'String');

Replies are listed 'Best First'.
Re^18: p0fq.pl and pack
by macli (Beadle) on Feb 26, 2007 at 04:22 UTC
    $c->tag did it, thanks :)
    it is quite annoying, that the script only works on Mac running Yellow Dog Linux, not on PC or my another OS X machine, the hex dump is like this:
    src ip:0xc0a80101 dst ip:0xc0a80102 query: 0x0000 : ED AC EF 0D 01 00 00 00 78 56 34 12 01 01 A8 C0 : ........x +V4..... 0x0010 : 02 01 A8 C0 00 00 BB 01 : ........ response: 0x0000 : ED AC EF 0D 78 56 34 12 02 00 00 00 00 00 00 00 : ....xV4.. +....... 0x0010 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0020 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0030 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0040 : 00 00 00 00 00 FF 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0050 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0060 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0070 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... 0x0080 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ......... +....... This connection is not (no longer?) in the cache.

    The pack did pack src, dst ip into query , but response always says no connection, I did adjust the bigendian to little endian on PC. Is Perl really not good at this low level packet handling things?

      Is Perl really not good at this low level packet handling things?

      Any other languages would have the same problem. The reason it's hard it's because p0f uses an undocumented communication format that varies from machine to machine.

      I did adjust the bigendian to little endian on PC.

      I thought that if you didn't specify one, it would use the machine's native byte order. Are you sure you need to specify the ByteOrder and alignments?

      The pack did pack src, dst ip into query , but response always says no connection

      IP addresses are usually big endian (i.e. in the same order as in dotted form) on all systems.

      01 01 A8 C0
      should be
      C0 A8 01 01
      (192.168.1.1)

      Use tag to mark the appropriate fields as big endian. That means you should have:

      my $c = Convert::Binary::C->new(); eval { $c->parse_file("p0f-query.h"); 1 } or die("Unable to parse p0f-query.h: $@\n"); $c->tag('p0f_query.src_ad', ByteOrder => 'BigEndian'); $c->tag('p0f_query.dst_ad', ByteOrder => 'BigEndian'); $c->tag('p0f_response.genre', Format => 'String'); $c->tag('p0f_response.detail', Format => 'String'); $c->tag('p0f_response.link', Format => 'String'); $c->tag('p0f_response.tos', Format => 'String');

      The port might need to be big endian too.

        ah, you rock!
        $c->tag('p0f_query.src_ad', ByteOrder => 'BigEndian'); $c->tag('p0f_query.dst_ad', ByteOrder => 'BigEndian');
        works for my PC.

        I just commented ByteOrder, the native system endaness is used as you mentioned. but I have to specify Alignment, strange.

        It still does not work for OS X, I guess I need to adjust some other parameters.