Página 1 de 2

Sniffer con tcpdump

NotaPublicado: 2013-11-20 11:44 @530
por BigBear
Hola, estaba probando este código que sirve como sniffer que es muy viejo.
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2.  
  3. $LIMIT = shift || 5000;
  4.  
  5. $|=1;
  6. open (STDIN,"/usr/sbin/tcpdump -lnx -s 1024 dst port 80 |");
  7. while (<>) {
  8.     if (/^\S/) {
  9.         last unless $LIMIT--;
  10.         while ($packet=~/(GET|POST|WWW-Authenticate|Authorization).+/g)  {
  11.             print "$client -> $host\t$&\n";
  12.         }
  13.         undef $client; undef $host; undef $packet;
  14.         ($client,$host) = /(\d+\.\d+\.\d+\.\d+).+ > (\d+\.\d+\.\d+\.\d+)/
  15.             if /P \d+:\d+\((\d+)\)/ && $1 > 0;
  16.     }
  17.     next unless $client && $host;
  18.     s/\s+//;
  19.     s/([0-9a-f]{2})\s?/chr(hex($1))/eg;
  20.     tr/\x1F-\x7E\r\n//cd;
  21.     $packet .= $_;
  22. }
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4

El tema es que solo muestra esto a pesar donde navegue por Internet:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
  1. doddy@doddy-desktop:~/Escritorio/test$ sudo perl sniffer.pl
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on eth0, link-type EN10MB (Ethernet), capture size 1024 bytes
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


Me parece raro y lo necesito usar con tcpdump porque siempre viene por defecto. ¿ Alguien sabe si cambiaron los comandos de tcpdump o si el código ya es viejo y no sirve?

Todo esto está basado de acá: http://stein.cshl.org/~lstein/talks/WWW6/sniffer/

Re: Sniffer con tcpdump

NotaPublicado: 2013-11-20 14:10 @632
por explorer
Sí, el formato ha cambiado un poco.

La siguiente versión funciona con un tcpdump moderno.
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2.  
  3. $LIMIT = shift || 5000;
  4.  
  5. $|=1;
  6. open (STDIN,"/usr/sbin/tcpdump -lnx -s 1024 dst port 80 |");
  7. while (<>) {
  8.     if (/^\S/) {
  9.         last unless $LIMIT--;
  10.         while ($packet=~/(GET|POST|WWW-Authenticate|Authorization).+/g)  {
  11.             print "$client -> $host\t$&\n";
  12.         }
  13.         undef $client; undef $host; undef $packet;
  14.         ($client,$host) = /IP (\d+\.\d+\.\d+\.\d+).+ > (\d+\.\d+\.\d+\.\d+)/;
  15.     }
  16.     next unless $client && $host;
  17.     s/\s+//g;
  18.     s/0x[abcdef\d]+://i;
  19.     s/([0-9a-f]{2})/chr(hex($1))/eg;  
  20.     tr/\x1F-\x7E\r\n//cd;
  21.     $packet .= $_;
  22. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Re: Sniffer con tcpdump

NotaPublicado: 2013-11-20 15:02 @668
por BigBear
Me sorprendió, funciona perfecto. El problema es que no sé cómo funciona el fixup.pl para decodificar la información que está en la página que te mostré. Me parece raro no sé cuándo se usa. ¿Cómo uso el fixup.pl con tu versión? Con eso me refiero a cuándo la uso, porque no entiendo cómo se combina.

P.D.: Last modified: Mon Jul 21 14:55:16 EDT 1997 , esta técnica es muy vieja :D

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-11 14:34 @649
por BigBear
explorer, tengo una duda. Quiero escribir esto mismo pero de otra forma:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. undef $client; undef $host; undef $packet;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Intenté con "my" pero después falla el código.

¿ De qué otra forma se logra lo mismo ?

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-11 15:12 @675
por explorer
Si el objetivo es indefinir esas variables, no conozco otra forma.

Ahora bien... a lo mejor el problema es que tengas que indefinirlas... ;)

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-11 15:34 @690
por BigBear
Ok, mira, el código quedó así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!usr/bin/perl
  2. #DH Sniffer 0.3
  3. #(C) Doddy Hackman 2013
  4.  
  5. use CGI;
  6. use threads;
  7.  
  8. $| = 1;
  9.  
  10. my $hilo_get = threads->new( \&sniffer_get );
  11.  
  12. #my $hilo_post = threads->new( \&sniffer_post );
  13.  
  14. if ( $ARGV[0] =~ /g/ ) {
  15.     $hilo_get->join;
  16. }
  17.  
  18. #$hilo_post->join;
  19.  
  20. sub sniffer_get {
  21.  
  22.     open( STDIN, "/usr/sbin/tcpdump -lnx -s 1024 dst port 80 |" );
  23.  
  24.     while (<>) {
  25.  
  26.         if (/^\S/) {
  27.  
  28.             while ( $contenido =~ /(GET|POST|WWW-Authenticate|Authorization).+/g ) {
  29.                 print "\n[+] $ip = $name " . uri_unescape($&);
  30.                 savefile( "logs", "\n[+] $ip = $name " . uri_unescape($&) );
  31.             }
  32.  
  33.             undef $ip;
  34.             undef $name;
  35.             undef $contenido;
  36.  
  37.             ( $ip, $name ) = /IP (\d+\.\d+\.\d+\.\d+).+ > (\d+\.\d+\.\d+\.\d+)/;
  38.  
  39.         }
  40.  
  41.         s/\s+//g;
  42.         s/0x[abcdef\d]+://i;
  43.         s/([0-9a-f]{2})/chr(hex($1))/eg;
  44.         tr/\x1F-\x7E\r\n//cd;
  45.  
  46.         $contenido .= $_;
  47.  
  48.     }
  49. }
  50.  
  51. # The End ?
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


Desde que implementé el tema de los threads funciona mal. Mira lo que devuelve:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
Can't open -g: No existe el fichero o el directorio at test.pl line 23.
doddy@doddy-desktop:~/Escritorio/HackingToolz/Warfactory IX/DH Sniffer$ tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 1024 bytes
1 packets captured
1 packets received by filter
0 packets dropped by kernel
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Al parecer el error está acá:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. while (<>) {
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


¿ Cómo arreglo el código ?

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-12 07:15 @343
por explorer
El problema está en que Perl trata a los argumentos como si fueran nombres de archivos que tiene que procesar.

Así, cuando pones '-g' después del nombre del programa, Perl lo intenta abrir como si fuera un archivo, para luego ser procesado línea a línea con el while(<>). Y claro: ese argumento no es un archivo, sino un parámetro de funcionamiento.

Una solución rápida es la de "sacar" el parámetro de la lista de argumentos, es decir, de @ARGV.

Inserta la línea siguiente delante de la 15:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1.     shift;     # saca el argumento '-g' de @ARGV
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Otra cosa... ¿para qué necesitas usar threads? No veo nada en el código, que lo requiera.

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-12 08:30 @396
por BigBear
Corregí el código pero sigue devolviendo mal el resultado. Te muestro el código completo:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!usr/bin/perl
  2. #DH Sniffer 0.3
  3. #(C) Doddy Hackman 2013
  4. #Credits :
  5. #Based on :
  6. #http://stackoverflow.com/questions/4777042/can-i-use-tcpdump-to-get-http-requests-response-header-and-response-body
  7. #http://www.perlmonks.org/?node_id=656590
  8. #http://stein.cshl.org/~lstein/talks/WWW6/sniffer/
  9. #http://perlenespanol.com/foro/post36051.html
  10. #Thanks to : Lincoln D. Stein , paulz and Explorer
  11.  
  12. use CGI;
  13. use threads;
  14.  
  15. $| = 1;
  16.  
  17. my $hilo_get  = threads->new( \&sniffer_get );
  18. my $hilo_post = threads->new( \&sniffer_post );
  19.  
  20. my $con = shift;
  21.  
  22. print "con $con";
  23.  
  24. if ( $con =~ /g/ ) {
  25.     $hilo_get->join;
  26. }
  27. if ( $con =~ /p/ ) {
  28.     $hilo_post->join;
  29. }
  30.  
  31. sub sniffer_get {
  32.  
  33.     open( STDIN, "/usr/sbin/tcpdump -lnx -s 1024 dst port 80 |" );
  34.  
  35.     while (<>) {
  36.  
  37.         if (/^\S/) {
  38.  
  39.             while ( $contenido =~ /(GET|POST|WWW-Authenticate|Authorization).+/g ) {
  40.                 print "\n[+] $ip = $name " . uri_unescape($&);
  41.                 savefile( "logs", "\n[+] $ip = $name " . uri_unescape($&) );
  42.             }
  43.  
  44.             undef $ip;
  45.             undef $name;
  46.             undef $contenido;
  47.  
  48.             ( $ip, $name ) = /IP (\d+\.\d+\.\d+\.\d+).+ > (\d+\.\d+\.\d+\.\d+)/;
  49.  
  50.         }
  51.  
  52.         s/\s+//g;
  53.         s/0x[abcdef\d]+://i;
  54.         s/([0-9a-f]{2})/chr(hex($1))/eg;
  55.         tr/\x1F-\x7E\r\n//cd;
  56.  
  57.         $contenido .= $_;
  58.  
  59.     }
  60. }
  61.  
  62. sub sniffer_post {
  63.  
  64.     open( STDIN, "tcpdump -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' |" );
  65.     while (<>) {
  66.  
  67.         if (/^\S/) {
  68.  
  69.             my $code = $_;
  70.  
  71.             $buscando = CGI->new($code);
  72.  
  73.             my @params = $buscando->param;
  74.  
  75.             foreach $par (@params) {
  76.  
  77.                 if ( $par =~ /\./ ) {
  78.                     next;
  79.                 }
  80.                 else {
  81.                     my $dataf = $buscando->param($par);
  82.                     print "\n[+] $par " . " : " . $dataf;
  83.                     savefile( "logs", "\n[+] $par " . " : " . $dataf );
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90. sub savefile {
  91.     open( SAVE, ">>" . $_[0] );
  92.     print SAVE $_[1];
  93.     close SAVE;
  94. }
  95.  
  96. # The End ?
  97.  
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-12 12:47 @574
por explorer
Yo no veo nada raro, pero tampoco lo he probado. ¿Sigue saliendo el error de que intenta abrir el archivo que no es?

Re: Sniffer con tcpdump

NotaPublicado: 2014-01-12 13:12 @592
por BigBear
Sigue dando lo mismo que la primera vez, por eso te mostré el código para ver cuál era el error.