From charlesreid1

 
(22 intermediate revisions by the same user not shown)
Line 29: Line 29:
Pick out which ones you want to listen to.
Pick out which ones you want to listen to.


=Usage=


You will need to run tcpdump as sudo.


==Unfiltered Packet Capture==
=Basic Usage=
 
You may need to run tcpdump as sudo to access certain information from the hardware.
 
Tcpdump options can vary from platform to platform (e.g. mac vs linux) but this guide will cover some universal usage.


The simplest way to use tcpdump is to do an unfiltered packet capture - no filters on packets, so everything is captured.  
The simplest way to use tcpdump is to do an unfiltered packet capture - no filters on packets, so everything is captured.  


To do this, specify which device you want to listen to with the <code>-i</code> flag. Also specify an output file with the <code>-w</code> flag:
The bare minimum you'll have to specify is a network interface. You may want to specify a file, too.
 
==The -i and -w flags==
 
To specify a network device you want to listen to, use the <code>-i</code> flag (for interface). Also specify an output file with the <code>-w</code> flag:


<pre>
<pre>
Line 45: Line 51:
<code>-w</code> prevents your computer from having a meltdown trying to print every single packet in a busy place.
<code>-w</code> prevents your computer from having a meltdown trying to print every single packet in a busy place.


You can also monitor multiple interfaces by specifying a list: <code>-i en0,en1</code>
You can monitor multiple interfaces by specifying a list: <code>-i en0,en1</code>
 
If you are using wireless, you'll need to use additional commands to control the channel your wireless card is listening to.
 
 
==Controlling Output==
 
To control output, you can have tcpdump create a new pcap file every N seconds, or every N megabytes.
 
===G flag===
 
Use the G flag to create a new pcap file every N seconds:


==Wireless Packet Capture==
<pre>
-G [seconds]
</pre>


If you want to capture wireless packets, you need to know a bit more about a few things.
If you use the G flag without the C flag (see below), you specify new filenames with strftime date/time format when you pass the filename to the -w flag.
 
This command makes a new pcap file every 100 seconds:
 
<pre>
tcpdump -G 100 -w filename_%H-%M-%S.pcap
</pre>


First is channels.
===C flag===


The 802.11 protocol allocates 12 channels for wireless (in the US), and your wireless card can only listen to one channel at a time. To listen to twelve channels, you need twelve wireless cards - or you need to hop from channel to channel with your single wireless card.
The C flag sets the maximum pcap file size, in millions of bytes. New files will have a common name with an incrementing number at the end. From the man page:


If it is critical to capture all traffic, you will want to use multiple wireless cards - if you're hopping from channel 5 to channel 6, and traffic shows up on channel 4, you won't see it.
<pre>
-C


Second is monitor mode. If your wireless card is not in monitor mode, your wireless card will be throwing away any packets that are not intended for itself, meaning you'll only be creating a pcap file of your own traffic.
Before writing a raw packet to a savefile, check whether the file is currently larger than file_size and, if so, close the current savefile and open a new one. Savefiles after the first savefile will have the name specified with the -w flag, with a number after it, starting at 1 and continuing upward. The units of file_size are millions of bytes (1,000,000 bytes, not 1,048,576 bytes).
</pre>


===Monitor Mode in Mac===
===W flag===


To put the wireless card into monitor mode, you can use the <code>-I</code> flag with tcpdump (and the <code>-n</code> flag to make things more readable):
The W flag will limit the number of output files, so that tcpdump will begin to overwrite the first file once it has finished writing to the Nth file:


<pre>
<pre>
sudo tcpdump -In -i en1 -w save.pcap
-W    Used  in  conjunction  with the -C option, this will limit the
          number of files created to the  specified  number,  and  begin
          overwriting  files from the beginning, thus creating a 'rotat-
          ing' buffer.  In addition, it will name the files with  enough
          leading  0s  to  support the maximum number of files, allowing
          them to sort correctly.
 
          Used in conjunction with the -G option, this  will  limit  the
          number  of  rotated  dump files that get created, exiting with
          status 0 when reaching the limit. If used with -C as well, the
          behavior will result in cyclical files per timeslice.
</pre>
</pre>


Alternatively, you can use the <code>airport</code> utility, located at:
=Wireless Tcpdump=
 
More instructions on capturing wireless packets with Tcpdump: [[Tcpdump/Wireless]]
 
[[Tcpdump/Wireless/Linux]]
 
[[Tcpdump/Wireless/Mac]]
 
=More Flags=
 
==Faster Packet Capture==
 
To minimize overhead processing packets and maximize the number of packets captured, you can turn off host name resolution with the <code>-n</code> flag. This also makes things slightly more readable.


<pre>
<pre>
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport.
tcpdump -I -n -i wlan1 -w output_pcap_file.pcap
</pre>
</pre>


conveniently symlinked to <code>/usr/local/bin</code>:
==Writing Packets To File==
 
If you want to force tcpdump to write every packet to the output file as it is received, rather than waiting until its input buffer is full, you can use the U flag. Note that this will be slower and should only be done when traffic is light - otherwise excessive disk writes will bog things down.
 
From the man page:


<pre>
<pre>
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport
      -U    If the -w option is not specified, make the printed packet output `
              `packet-buffered''; i.e., as the description of the contents of  each  packet  is
              printed, it will be written to the standard output, rather than, when not writing
              to a terminal, being written only when the output buffer fills.
 
              If the -w option is specified, make the saved raw packet output
              ``packet-buffered''; i.e., as each packet is saved, it will be written
              to the output file, rather than being written only when the output
              buffer fills.
 
              The -U flag will not be supported if tcpdump was built with an
              older version of libpcap that lacks the pcap_dump_flush() function.
</pre>
</pre>


===Channel-Hopping on Mac===
=Analysis=
 
You can also use tcpdump to analyze a pcap file.
 
==Reading Packets==


Once you've put the card into monitor mode, you can use airport to listen to a single channel thusly:
To read packet data, run tcpdump with the <code>-r</code> flag (for read):


<pre>
<pre>
sudo airport en1 sniff 1
$ tcpdump -r output.pcap
</pre>
</pre>


or you can scan all channels for nearby routers, which only does a once-through sweep of the channels:
==Counting Packets==
 
Not sure if this will work:


<pre>
<pre>
sudo airport en1 -s
$ tcpdump -n -r output.pcap | wc -l
</pre>
</pre>


===Monitor Mode in Linux===
This will give you a count of the total number of packets in the pcap file.
 
==Parsing Information==
 
You can parse information by column using the cut utility.
 
<pre>
$ tcpdump -n -r output.pcap
</pre>


Put your card into monitor mode with these steps:
The output has the fields:


<pre>
<pre>
iwconfig # list all devices
[timestamp] [network protocol] [source IP] . [source port] > [destination IP] . [destination port]
ifconfig wlan1 down # assuming wlan1 is wireless
iwconfig wlan1 mode monitor # put into monitor mode
ifconfig wlan1 up # bring wlan1 online
</pre>
</pre>


===Channel-Hopping on Linux===


Once you've put the card into monitor mode, you can run <code>airodump-ng</code>, which will automatically channel-hop unless you specify a specific channel.
 
[https://www.sans.org/reading-room/whitepapers/protocols/analyzing-network-traffic-basic-linux-tools-34037]
 
 
{{NetworkingFlag}}

Latest revision as of 22:31, 30 June 2016

Installing

Linux

tcpdump should come with your distro, but if it doesn't, use aptitude or your package manager to install:

apt-get install tcpdump

Once you've done that, you can list your network devices:

iwconfig

Pick out which ones you want to listen to.

Mac

tcpdump comes with Mac. Man page for tcpdump: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/tcpdump.1.html

List your network devices:

ifconfig

Pick out which ones you want to listen to.


Basic Usage

You may need to run tcpdump as sudo to access certain information from the hardware.

Tcpdump options can vary from platform to platform (e.g. mac vs linux) but this guide will cover some universal usage.

The simplest way to use tcpdump is to do an unfiltered packet capture - no filters on packets, so everything is captured.

The bare minimum you'll have to specify is a network interface. You may want to specify a file, too.

The -i and -w flags

To specify a network device you want to listen to, use the -i flag (for interface). Also specify an output file with the -w flag:

tcpdump -i en0 -w output_file.pcap

-w prevents your computer from having a meltdown trying to print every single packet in a busy place.

You can monitor multiple interfaces by specifying a list: -i en0,en1

If you are using wireless, you'll need to use additional commands to control the channel your wireless card is listening to.


Controlling Output

To control output, you can have tcpdump create a new pcap file every N seconds, or every N megabytes.

G flag

Use the G flag to create a new pcap file every N seconds:

-G [seconds]

If you use the G flag without the C flag (see below), you specify new filenames with strftime date/time format when you pass the filename to the -w flag.

This command makes a new pcap file every 100 seconds:

tcpdump -G 100 -w filename_%H-%M-%S.pcap

C flag

The C flag sets the maximum pcap file size, in millions of bytes. New files will have a common name with an incrementing number at the end. From the man page:

-C

Before writing a raw packet to a savefile, check whether the file is currently larger than file_size and, if so, close the current savefile and open a new one. Savefiles after the first savefile will have the name specified with the -w flag, with a number after it, starting at 1 and continuing upward. The units of file_size are millions of bytes (1,000,000 bytes, not 1,048,576 bytes).

W flag

The W flag will limit the number of output files, so that tcpdump will begin to overwrite the first file once it has finished writing to the Nth file:

-W     Used  in  conjunction  with the -C option, this will limit the
          number of files created to the  specified  number,  and  begin
          overwriting  files from the beginning, thus creating a 'rotat-
          ing' buffer.  In addition, it will name the files with  enough
          leading  0s  to  support the maximum number of files, allowing
          them to sort correctly.

          Used in conjunction with the -G option, this  will  limit  the
          number  of  rotated  dump files that get created, exiting with
          status 0 when reaching the limit. If used with -C as well, the
          behavior will result in cyclical files per timeslice.

Wireless Tcpdump

More instructions on capturing wireless packets with Tcpdump: Tcpdump/Wireless

Tcpdump/Wireless/Linux

Tcpdump/Wireless/Mac

More Flags

Faster Packet Capture

To minimize overhead processing packets and maximize the number of packets captured, you can turn off host name resolution with the -n flag. This also makes things slightly more readable.

tcpdump -I -n -i wlan1 -w output_pcap_file.pcap

Writing Packets To File

If you want to force tcpdump to write every packet to the output file as it is received, rather than waiting until its input buffer is full, you can use the U flag. Note that this will be slower and should only be done when traffic is light - otherwise excessive disk writes will bog things down.

From the man page:

       -U     If the -w option is not specified, make the printed packet output `
              `packet-buffered''; i.e., as the description of the contents of  each  packet  is
              printed, it will be written to the standard output, rather than, when not writing 
              to a terminal, being written only when the output buffer fills.

              If the -w option is specified, make the saved raw packet output 
              ``packet-buffered''; i.e., as each packet is saved, it will be written 
              to the output file, rather than being written only when the output 
              buffer fills.

              The -U flag will not be supported if tcpdump was built with an 
              older version of libpcap that lacks the pcap_dump_flush() function.

Analysis

You can also use tcpdump to analyze a pcap file.

Reading Packets

To read packet data, run tcpdump with the -r flag (for read):

$ tcpdump -r output.pcap

Counting Packets

Not sure if this will work:

$ tcpdump -n -r output.pcap | wc -l 

This will give you a count of the total number of packets in the pcap file.

Parsing Information

You can parse information by column using the cut utility.

$ tcpdump -n -r output.pcap

The output has the fields:

[timestamp] [network protocol] [source IP] . [source port] > [destination IP] . [destination port]


[1]