VoIP and Hacking | Consulenza Documentazione

tshark e cli tools di wireshark

by admin on Jun.28, 2010, under Hacking, Linux, Networking, Sicurezza, Tools

Wireshark, che una volta si chiamava Ethereal, è un software piuttosto noto in generale a molti utilizzatori Linux, cui magari sfugge tuttavia la presenza dei suoi tools a riga di comando, talvolta presenti nell’omonimo package, oppure in un package supplementare, a seconda della distribuzione utilizzata.

Nel tarball dei sorgenti, invece è tutto compreso.

Un buon motivo per usare i tools di tipo CLI che accompagnano Wireshark è la possibilità di effettuare una veloce analisi tramite il “postprocessing” dei dati di una precedente cattura, in modo più flessibile di quanto possa offrire l’interfaccia grafica, che rimane estremamente potente e interattiva, ma statica, con le sue varie e pur numerose funzionalità.

Un altro buon motivo può essere rappresentato dalla necessità di dover implementare, nella network analisys, qualche forma di automazione.

I tools CLI di Wireshark sono:

  • tshark
  • dumpcap
  • capinfos
  • editcap
  • mergecap
  • rawshark

In particolare, tShark è la versione CLI di Wireshark.
Consente di catturare i pacchetti in transito nella rete, o di leggerli da un capture file (in formato libpcap) precedentemente salvato, restituendoli tramite qualche forma di decodifica sullo standard output o riscrivendoli a sua volta su di un file.

In qualche modo può essere assimilato a tcpdump, ma al contrario di quest’ultimo è statefull anziché stateless, e in possesso di molte più possibilità di decodificare i vari protocolli.

Fa uso di dumpcap come capture engine.

Sue opzioni standard sono: -D, -i, -c, -n, -l, -f, -R, -s, -w, -r

  • l’opzione “-r” si riferisce alla lettura del file specificato di seguito.
  • l’opzione “-w” si riferisce invece alla scrittura del file specificato di seguito.
  • l’opzione “-n” riguarda il name resolving.
  • riguarda invece i time stamps l’opzione “-t”
  • l’opzione “-d” sta per “decodifica come” ad esempio “-d tcp.port==8080,http”
  • determinate preferenze si possono impostare con “-o preferenza:valore”
  • riguardano i formats di output le opzioni “-V” o “-T “
  • un output verbose è rappresentato da “-V”,
  • un dump esadecimale da “-x”
  • l’opzione “-T” rappresenta il formato dell’ output che si ottiene dalla visualizzazione dei pacchetti decodofificati come dati pdml (Packet Details Markup Language), un formato XML-based per definire dettagli di un pacchetto così codificato. Tale informazione è equivalente ai “packet details” emessi col flag “-V”.
  • l’opzione “-T fields ( -E separatore  -e campo1 -e campo2 …)” da la possibilità di rappresentare nell’output il valore di diversi campi, aggiunti ognuno con l’opzione “-e”.
  • l’opzione “-z” da l’opportunità di collezionare dati statistici. quella relativa ad esempio alla gerarchia protocollare:
# tshark -r file1.cap -qz io,phs

===================================================================
Protocol Hierarchy Statistics
Filter: frame

frame                                    frames:48346 bytes:24412056
  sll                                    frames:48346 bytes:24412056
    ip                                   frames:48346 bytes:24412056
      pim                                frames:846 bytes:59220
      igmp                               frames:413 bytes:18172
      udp                                frames:941 bytes:168438
        nbns                             frames:16 bytes:1504
        dns                              frames:924 bytes:166888
        edonkey                          frames:1 bytes:46
      icmp                               frames:26 bytes:2750
      tcp                                frames:46120 bytes:24163476
        http                             frames:5852 bytes:4262144
          data-text-lines                frames:377 bytes:307599
          media                          frames:85 bytes:69035
          image-gif                      frames:398 bytes:235810
          png                            frames:46 bytes:39668
          xml                            frames:9 bytes:6218
          image-jfif                     frames:18 bytes:20952
          http                           frames:1 bytes:1508
        tcp.segments                     frames:1847 bytes:1234511
          http                           frames:1655 bytes:1072216
            xml                          frames:22 bytes:8898
            data-text-lines              frames:651 bytes:407351
            media                        frames:133 bytes:78611
            image-jfif                   frames:381 bytes:283116
            png                          frames:207 bytes:162102
            image-gif                    frames:147 bytes:78577
          ssl                            frames:192 bytes:162295
        ssl                              frames:436 bytes:448695
===================================================================

oppure all’ i/o, come in:

# tshark -r file1.cap -qz io,stat,3600,udp,tcp
===================================================================
IO Statistics
Interval: 3600.000 secs
Column #0: udp
Column #1: tcp
                      |   Column #0    |   Column #1
Time                  |frames|  bytes  |frames|  bytes
000.000-3600.000         16      3043    128     51717
3600.000-7200.000        10      1933     74     20602
7200.000-10800.000       28      3877     92     15638
10800.000-14400.000      16      2549     91     15696
14400.000-18000.000      14      2715    109     29237
18000.000-21600.000     436     75223  23228  12366165
21600.000-25200.000     438     81124  22399  11664505
===================================================================

oppure alle conversazioni, ad esempio:

# tshark -r file1.cap -qz conv,tcp
================================================================================
TCP Conversations
Filter:
                                               |       <-      | |       ->      | |     Total     |
                                               | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |
151.65.46.165:37204  <-> 195.22.202.56:http       389    579321     238     19430     627    598751
151.65.46.165:56783  <-> 72.14.234.136:http       298    404759     249     33993     547    438752
151.65.46.165:41561  <-> 72.14.234.190:http       241    321264     243     30537     484    351801
151.65.46.165:60972  <-> 195.22.202.56:http       280    415705     196     15387     476    431092
151.65.46.165:52278  <-> 72.14.234.91:http        214    286784     217     27003     431    313787
151.65.46.165:58117  <-> 72.14.234.91:http        212    281050     191     25303     403    306353
151.65.46.165:54341  <-> 213.92.45.30:http        220    324591     155     11960     375    336551
151.65.46.165:58018  <-> 151.1.245.98:http        162    240200     144     10987     306    251187
151.65.46.165:58399  <-> 72.14.234.190:http       141    189237     139     17636     280    206873
151.65.46.165:35913  <-> 213.92.45.30:http        177    259203     103      9799     280    269002
[...]
41.141.96.70:wafs    <-> 151.65.46.165:telnet       1        56       1        76       2       132
151.65.21.171:13411  <-> 151.65.46.165:netbios-ssn  1        56       1        64       2       120
151.65.21.171:13410  <-> 151.65.46.165:microsoft-ds 1        56       1        64       2       120
188.18.132.199:ds-user <-> 151.65.46.165:telnet     1        56       1        72       2       128
92.240.68.153:46003  <-> 151.65.46.165:http         1        56       1        76       2       132
62.193.237.53:36506  <-> 151.65.46.165:socks        1        56       1        64       2       120
41.196.73.132:ibm-diradm <-> 151.65.46.165:telnet   1        56       1        76       2       132
95.7.122.81:magbind  <-> 151.65.46.165:telnet       1        56       1        76       2       132
41.141.96.70:avinstalldisc <-> 151.65.46.165:telnet 1        56       1        76       2       132
151.65.46.165:35176  <-> 74.52.6.250:http           0         0       1        84       1        84

Dumpcap è pure esso un tool per il “network traffic dump”. Consente di catturare i pacchetti in transito nella rete, e li può registrare su di un file.
può venire usata in maniera indipendente, possedendo delle opzioni simili a quelle di tshark. È inoltre molto rapido, prevedendo solamente un trasferimento network->disco.
al pari di tcpdump, è invece stateless, cosicché i traces possano essere perennemente in esecuzione.
possiede una feature “ring buffer” (-b) estremamente utile, ad esempio:

# dumpcap -i 5 -s0 -b filesize:16384 files:1024 -w ring.cap
File: ring_00001_20100628132124.cap
[...]

che fa in modo che dumpcap venga eseguito in “multifiles” mode. In “multiple files” mode, dumpcap sarà in grado di scrivere differenti capture files.
Quando il primo file “capture” raggiunge la dimensione massima, dumpcap passerà a scrivere sul file successivo, e così via, qui per un massimo di 1024 files, dopodiché continuerà riutilizzando i vecchi filenames.

capinfos è un tool che visualizza un summario relativo ad un tracefile, tutte le info, come in:

# capinfos file1.cap
File name: file1.cap
File type: Wireshark/tcpdump/... - libpcap
File encapsulation: Linux cooked-mode capture
Number of packets: 48346
File size: 25185616 bytes
Data size: 24412056 bytes
Capture duration: 25146 seconds
Start time: Sat Jun 26 11:42:22 2010
End time: Sat Jun 26 18:41:28 2010
Data byte rate: 970.83 bytes/sec
Data bit rate: 7766.62 bits/sec
Average packet size: 504.94 bytes
Average packet rate: 1.92 packets/sec

oppure info specifiche:

# capinfos -aec file1.cap
File name: file1.cap
Number of packets: 48346
Start time: Sat Jun 26 11:42:22 2010
End time: Sat Jun 26 18:41:28 2010

che restituisce solamente start-time, end-time, e conteggio dei pacchetti.

editcap è invece una utility che può essere usata per selezionare pacchetti in un capture file

selezionandoli ad esempio in base a frame ranges o time ranges:

# editcap -r file1.cap tmp.cap 1-1000 2001-3000
Add_Selected: 1-1000
Inclusive ... 1, 1000
Add_Selected: 2001-3000
Inclusive ... 2001, 3000


# editcap -A "2010-06-26 15:00:00" -B "2010-06-17 15:59:59" file1.cap tmp.cap
# capinfos -ae tmp.cap
File name: tmp.cap
Start time: Sat Jun 26 15:00:21 2010
End time: Sat Jun 26 15:59:50 2010

spezzandoli in tronconi (chunks)

5000 pacchetti per file:

# editcap -c 5000 file1.cap tmp.cap
# ls -lrt tmp*.cap
-rw-r--r-- 1 root root  1318141 2010-06-28 18:43 tmp_00009_20100626182204.cap
-rw-r--r-- 1 root root  2431831 2010-06-28 18:43 tmp_00008_20100626181159.cap
-rw-r--r-- 1 root root  2926934 2010-06-28 18:43 tmp_00007_20100626175818.cap
-rw-r--r-- 1 root root  3134674 2010-06-28 18:43 tmp_00006_20100626174953.cap
-rw-r--r-- 1 root root  2424101 2010-06-28 18:43 tmp_00005_20100626173744.cap
-rw-r--r-- 1 root root  2976707 2010-06-28 18:43 tmp_00004_20100626173145.cap
-rw-r--r-- 1 root root  2685178 2010-06-28 18:43 tmp_00003_20100626171603.cap
-rw-r--r-- 1 root root  2911973 2010-06-28 18:43 tmp_00002_20100626170853.cap
-rw-r--r-- 1 root root  2704739 2010-06-28 18:43 tmp_00001_20100626170410.cap
-rw-r--r-- 1 root root  1671554 2010-06-28 18:43 tmp_00000_20100626114222.cap

un’ora di traffico per file:

# editcap -i 3600 file1.cap tmp.cap

è usato anche per modificare i pacchetti in un capture file
ad esempio cambiando lo snaplen:

# editcap -s 96 file1.cap tmp.cap

modificando i timetamps

# editcap -t 3600 file1.cap tmp.cap


# capinfos -ae file1.cap
File name: file1.cap
Start time: Sat Jun 26 11:42:22 2010
End time: Sat Jun 26 18:41:28 2010
# capinfos -ae tmp.cap
File name: tmp.cap
Start time: Sat Jun 26 12:42:22 2010
End time: Sat Jun 26 19:41:28 2010

cambiare formato di output:

# editcap -F ngsniffer file1.cap tmp.cap
# editcap -F
editcap: option requires an argument -- 'F'
editcap: The available capture file types for "F":
    libpcap - Wireshark/tcpdump/... - libpcap
    nseclibpcap - Wireshark - nanosecond libpcap
    modlibpcap - Modified tcpdump - libpcap
    nokialibpcap - Nokia tcpdump - libpcap
    rh6_1libpcap - RedHat 6.1 tcpdump - libpcap
    suse6_3libpcap - SuSE 6.3 tcpdump - libpcap
    5views - Accellent 5Views capture
    dct2000 - Catapult DCT2000 trace (.out format)
    nettl - HP-UX nettl trace
    netmon1 - Microsoft NetMon 1.x
    netmon2 - Microsoft NetMon 2.x
    ngsniffer - NA Sniffer (DOS)
    ngwsniffer_1_1 - NA Sniffer (Windows) 1.1
    ngwsniffer_2_0 - NA Sniffer (Windows) 2.00x
    niobserverv9 - Network Instruments Observer (V9)
    lanalyzer - Novell LANalyzer
    snoop - Sun snoop
    rf5 - Tektronix K12xx 32-bit .rf5 format
    visual - Visual Networks traffic capture
    k12text - K12 text file
    commview - TamoSoft CommView
    pcapng - Wireshark - pcapng (experimental)
    btsnoop - Symbian OS btsnoop

mergecap è un tool dedicato al merge di capture files:

in base ai timestamps:

# mergecap -w out.cap in-1.cap in-2.cap

oppure semplicemente appendendo ciascun file

# mergecap -a -w out.cap in-1.cap in-2.cap

anche il tool rawshark effettua il dump ed analizza i dati libpcap grezzi, ma a differenza di tshark, rawshark non fa alcuna assunzione sull’ incapsulamento dell’ input che gli viene passato.

Il tool tshark, comunque, per completezza, si rivela sicuramente quello più interessante, inoltre combinato ancora con altri tools a linea di comando, tipici del mondo unix, ancora più flessibile

Alcuni esempi, utilizzando il command piping

solamente i primi 3 http response:

# tshark -r file1.cap -R http.response -c3
16 94.044442 63.245.209.93 -> 151.65.46.165 HTTP HTTP/1.1 302 Found (text/html)
19 94.240137 63.245.209.93 -> 151.65.46.165 HTTP HTTP/1.1 302 Found (text/html)
36 94.381166 195.22.202.32 -> 151.65.46.165 HTTP/XML HTTP/1.0 200 OK

solamente i primi 3 http response code:

# tshark -r file1.cap -R http.response -T fields -e http.response.code -c3
302
302
200

conteggio dei vari http response code in una tabella ordinata:

# tshark -r file1.cap -R http.response -T fields -e http.response.code  | sort | uniq -c
   2440 200
     97 204
    165 301
     81 302
      1 303
    904 304
      5 400
     48 404

Altri esempi:

# tshark -r file1.cap -R http.request -T fields -e http.host -e http.request.uri -c3
fxfeeds.mozilla.com	/en-US/firefox/headlines.xml
fxfeeds.mozilla.com	/firefox/headlines.xml
newsrss.bbc.co.uk	/rss/newsonline_world_edition/front_page/rss.xml

# tshark -r file1.cap -R http.request -T fields -e http.host -e http.request.uri | sed -e 's/?.*$//' | sed -e 's#^(.*)t(.*)$#http://12#' | sort | uniq -c
[...]
      1 http://www.youtube.com/v/ArEYSyBHFUM&hl=it_IT&fs=1&
      1 http://www.youtube.com/v/CoSL_qayMCc&hl=it_IT&fs=1&
      1 http://www.youtube.com/v/nxEkKEpqvSg&hl=it_IT&fs=1&

Come si può facilmente dedurre dall’esame delle conversazioni tcp presenti nell’esempio fatto all’inizio, molte sono evidentemente relative a scansioni effettuate dall’esterno, “rumore di fondo” ormai onnipresente per ogni utente internet.

Gia in passato avevo affrontato il problema di geolocalizzare l’origine di tali probes.
tshark, in sinergia con altri tools può svolgere il compito in modo ancora più diretto dei metodi adottati in tali occasioni

A conferma di ciò, si può utilizzare preventivamente un filtro con tcpdump che isoli in un nuovo files capture solo i tentativi di connessione tcp effettuati sull’interfaccia esterna.

# tcpdump -ntr file1.cap 'dst host 151.65.46.165 and tcp[13] = 0×02′ -w probes.cap

operando con tshark su questo file intermedio si ottengono facilmente informazioni come i probes distribuiti per porta:

# tshark -r  probes.cap -T fields -e tcp.dstport |sort -n |uniq -c
      1 22
      8 23
      3 53
      1 80
     29 135
     17 139
     18 445
      1 1080
      1 2967
      1 3389
      1 12000
      3 12496

o gli stessi suddivisi e conteggiati per nazione di origine:

# tshark -r  probes.cap -T fields -e ip.geoip.src_country |sort |uniq -c
      6 Australia
      5 China
      1 Egypt
      1 France
     58 Italy
      1 Latvia
      2 Morocco
      2 Qatar
      1 Russian Federation
      2 Turkey
      5 United States
Send post as PDF to PDF | PDF Creator | PDF Converter
:, , , , , , ,

Comments are closed.

Cerchi qualcosa in particolare?

Usa il form qui sotto per cercare nel sito:

Blogroll!

Alcuni links...

Archives

Tutte le entries, in ordine cronologio...