How do I extract all the features from a pcap file using Wireshark? - wireshark

Hi its my first time using this, I have a PCAP file and I need help to extract all the features from this. When I open it in wireshark and export it as .csv all i get is the default columns which aren't useful.
Can anyone guide me to how I will go about doing this? I've seen some people mention TShark but haven't been successful in finding something useful which provides a tutorial on how to do this.

Tshark can do that using the following instruction:
tshark -r (path of pcap file) -E separator=, -E header=y -T fields -e field1 -e field2... > name.csv
The above code will read a pcap file, extract the features (fields) and output a CSV file.
-E header= y: if you want to have the name of features in the output CSV files.
-e field: The feature to be extraced.
The list of the fields/features can be long to be mentioned here. You can select the features from this tshark link https://www.wireshark.org/docs/dfref/#section_t, based on the protocol.
You can also use the list in this link https://medium.com/hacker-toolbelt/wireshark-filters-list-983c49468a45.
Example: Extract IP address (source and destination), Protocol, MAC address (source and destination) from a PCAP file and output a CSV file.
tshark -r Data\myfile.pcap -E header=y -E separator=, -T fields -e ip.src -e ip.dst -e ip.proto -e eth.src -e eth.dst > Data/myfile.csv
myfile.pcap: name of pcap file in Data folder.
myfile.csv: name of csv file, you don't have to creat this before running the code as it will be autimaitcally created in Data folder. you can select the location you want.

Related

Converting a pcap file to csv: Tshark displays multiple src,dst IP addresses in a single line for some packets

I want to convert a pcap file to csv/tsv with "Tshark" where each line corresponds to a packet and have the following format:
timestamp src_ip dst_ip protocol
I use this command:
tshark -r <file_name.pcap> -T fields -e frame.time_epoch -e ip.src -e ip.dst -e ip.proto
However, in the displayed output I see some lines to have two src,dst IP addresses and protocol number like this:
1525794300.3842 92.153.107.1,203.46.108.229 203.46.108.229,85.50.172.78 1,1
While for the rest, each line has one src IP, one dst IP and one protocol like the following:
1525794300.3843 185.61.46.124 163.218.99.213 6
Is there any reason that tshark is displaying multiple src and dst ip addresses in a single line? Can we do something so tshark does not do this?
Thanks!
The reason tshark displays multiple src and dst IP addresses as well as multiple protocol numbers is because there are multiple IP headers in the packet. In this case, it's ICMP packet carrying information about another ICMP packet, perhaps a "Time to live exceeded in transit" or some other such error. If you open the file in Wireshark or run tshark -r <file_name.pcap> -Y "icmp", you will be able to see this for yourself.
If you're only interested in the first (outer) IP src and dst addresses and protocol number, then you can limit the output to the first occurrence of each field as follows:
tshark -r <file_name.pcap> -T fields -E occurrence=f -e frame.time_epoch -e ip.src -e ip.dst -e ip.proto
Alternatively, you can specify columns while limiting the field occurrences this way:
On *nix:
tshark -r <file_name.pcap> -o 'gui.column.format:"Epoch Time","%Cus:frame.time_epoch","Src","%Cus:ip.src:1","Dst","%Cus:ip.dst:1","Proto","%Cus:ip.proto:1"'
On Windows:
tshark.exe -r <file_name.pcap> -o "gui.column.format:\"Epoch Time\",\"%Cus:frame.time_epoch\",\"Src\",\"%Cus:ip.src:1\",\"Dst\",\"%Cus:ip.dst:1\",\"Proto\",\"%Cus:ip.proto:1\""
Run tshark -G column-formats for additional help with the column formats.

grep all binary file in a folder to show only a specific world followed by numbers

I spent several hours trying to figure out what I am doing wrong. Thanks for any help in advance.
I want to grep the string toze359485948584 from multi different binary file within a specific folder. The first part of the string stay the same but the 12 digits after the world toze could change.
When I use
grep -a -o -E -r 'toze' /my folder/
I get the output toze
but when I use
grep -a -o -E -r 'toze[0-9]' /my folder/
I get no output at all.
The word toze is the same in all other binary files within that folder but the 12 digits following it are different from file to file.
Example of file:
:?5o2g0?2?76=1?7?5 clasFSCl??˹?t0?l?Ah?Ob??9??$[??Te?J? ????C?'fھ???ӽ?Agj?(m?r??q[4 '?E??'黼}v?seUC?ؑFh??0?-?:??ꅜP?~0?zMANP1?p?????cBMac60:30:d4:2d:0d:c2???ɜm0SrNm9I4l6?5?5?=?4!3L2?2?5}3
6?636?5{1(1?/?.uDX3X3JWLHG7F?????cWMac60:30:d4:2b:ef:ab?????c
/U/]-?5?6m+?.?-?*?*a-4;6'.?-?0x*?.?,00?faic??˵?i0toze359485948584??˹?t#0!inst00008010-001348443E100026?????d:08seid0040E3FF32F48800180401178969456532CBE6122F11BB554?????n*0(srvn :??j?^<?`m4,G????##???180718064325Z?????d0tsid928C7F80C073CA01???ٚR? 0?NvMR1???????T0DGSTo8En?HC??G??]???Q???????s0
,?0M/540K21 clasNvMR??˹?t0instF5?l?Ah?Ob??9??$[??Te?J? ????C?'fھ???ӽ?Agj?(m?r??q[4 '?E??'黼}v?seUC?ؑFh??0?-?:?????l?0?bbcl1?
RiMcP?SYS?Hs9v>B|B?AC?#?A?=$;U<?;?>?C?9?:E9?4X<7?:6?9?5-4?4?68?8?355L5$2
Because the numbers are more than one you can try something like:
grep -a -o -E -r 'toze[0-9].' /my folder/
If you are ready to loop the files and manage them one by one you can simplify the work via:
strings $file|grep -a -o -E 'toze[0-9].'

Is there a way in Wireshark to save only the packets in the listing window?

I would like to know if there is a way to save only No., Time, Source, Destination, Length field in Wireshark? I do not need the contents, I want it in plain text as well. Is this possible? Thank you.
If those are your only columns configured in Wireshark, then after loading a capture file you achieve this as follows: File -> Export Packet Dissections -> as "Plain Text" file... ->, then in the Packet Format section, select only the "Packet summary line" (and optionally "Include column headings" if you wish) and deselect everything else. Choose a filename and click "Save".
If you have other columns displayed, you can either hide or remove them first or create a separate profile with only the columns of interest displayed.
You can also accomplish this with tshark, Wireshark's command-line companion. There are several ways to do this, so select a method that best fits your needs. Here are some examples:
Using specified Wireshark columns: (Note: -e _ws.col.No. doesn't work)
tshark -r file.pcap -T fields -e frame.number -e _ws.col.Time -e _ws.col.Source -e _ws.col.Destination -e _ws.col.Length > file.txt
Using named fields: (assumes IPv4 addresses in Source and Destination columns)
tshark -r file.pcap -T fields -e frame.number -e frame.time -e ip.src -e ip.dst -e frame.len > file.txt
Rely on Wireshark's configured columns:
tshark -r file.pcap > file.txt
Rely on Wireshark's configured columns for a particular profile:
tshark -r file.pcap -C profilename > file.txt
Using column option: (First on Windows, then on *nix)
tshark -r file.pcap -o "gui.column.format:\"No.\",\"%m\",\"Time\",\"%t\",\"Source\",\"%s\",\"Destination\",\"%d\",\"Length\",\"%L\"" > file.txt
tshark -r file.pcap -o 'gui.column.format:"No.","%m","Time","%t","Source","%s","Destination","%d","Length","%L"" > file.txt
(Run tshark -G column-formats for more column options.)

Tshark - Exporting values of all fields with same name within a list

With reference to the below S1AP pcap log snapshot
The highlighted field above is s1ap.iE_Extensions which contains a list of items. I need to use Tshark to extract the id: field (s1ap.id) from Item0 and Item1 along with some other fields.
My tshark command looks like below as of now -
tshark -r test.pcap -Y "s1ap.procedureCode == 9" -T fields -e frame.time -e s1ap.procedureCode -E header=y -E separator=, > pcap_to_csv.csv
need help with exapanding the command so as to get the above fields (s1ap.id) for each item in s1ap.iE_Extensions list included in the exported file

How do I use tshark to print request-response pairs from a pcap file?

Given a pcap file, I'm able to extract a lot of information from the reconstructed HTTP request and responses using the neat filters provided by Wireshark. I've also been able to split the pcap file into each TCP stream.
Trouble I'm running into now is that of all the cool filters I'm able to use with tshark, I can't find one that will let me print out full request/response bodies. I'm calling something like this:
tshark -r dump.pcap -R "tcp.stream==123 and http.request" -T fields -e http.request.uri
Is there some filter name I can pass to -e to get the request/response body? The closest I've come is to use the -V flag, but it also prints out a bunch of information I don't necessary want and want to avoid having to kludge out with a "dumb" filter.
If you are willing to switch to another tool, tcptrace can do this with the -e option. It also has an HTTP analysis extension (xHTTP option) that generates the HTTP request/repsonse pairs for each TCP stream.
Here is a usage example:
tcptrace --csv -xHTTP -f'port=80' -lten capturefile.pcap
--csv to format output as comma sperated variable
-xHTTP for HTTP request/response written to 'http.times' this also switches on -e to dump the TCP stream payloads, so you really don't need -e as well
-f'port=80' to filter out non-web traffic
-l for long output form
-t to give me progress indication
-n to turn off hostname resolution (much faster without this)
If you captured a pcap file, you can do the following to show all requests+responses.
filename="capture_file.pcap"
for stream in `tshark -r "$filename" -2 -R "tcp and (http.request or http.response)" -T fields -e tcp.stream | sort -n | uniq`; do
echo "==========BEGIN REQUEST=========="
tshark -q -r "$filename" -z follow,tcp,ascii,$stream;
echo "==========END REQUEST=========="
done;
I just made diyism answer a bit easier to understand (you don't need sudo, and multiline script is imo simple to look at)
This probably wasn't an option when the question was asked but newer versions of tshark can "follow" conversations.
tshark -nr dump.pcap -qz follow,tcp,ascii,123
I know this is a super old question. I'm just adding this for anyone that ends up here looking for a current solution.
I use this line to show last 10 seconds request body and response body(https://gist.github.com/diyism/eaa7297cbf2caff7b851):
sudo tshark -a duration:10 -w /tmp/input.pcap;for stream in `sudo tshark -r /tmp/input.pcap -R "tcp and (http.request or http.response) and !(ip.addr==192.168.0.241)" -T fields -e tcp.stream | sort -n | uniq`; do sudo tshark -q -r /tmp/input.pcap -z follow,tcp,ascii,$stream; done;sudo rm /tmp/input.pcap

Resources