Text File turn into some Gibberish language

Viewed 306

this is tcpdump originally in pcap, changed into txt. Input:- sip.txt

Ôò¡          Ü     ªkã_¹¦ R  R   hIÿkRT 4V E`D]  @9°Ã'ö%1æËÄÞ÷0ûðSIP/2.0 403 Forbidden
Via: SIP/2.0/UDP XXX.XX.XX.X:57079;branch=94tsjam66cmay5bpswyfta0nebw34zhfctjuuge2baevikbk03opf15t6wvovnb82mjih3v;received=IP;rport=57079
From: "IP" <sip:IP@IP>;tag=0c26cd11
To: <sip:XXXXXX@XXX.XX.XX.XXX>;tag=as3a5a21bf
Call-ID: 88c218486f66a6aa214d483d988dfa9c
CSeq: 2 INVITE
Server: Asterisk
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, PRACK, MESSAGE
Supported: replaces, timer
Reason: Q.850;cause=21
Content-Length: 0

My code

import re
from collections import defaultdict
import io

with io.open('file location','rb',encoding='utf-8') as f:

    text = f.readlines()
result = []
blocks = text.split('\n\n\n')

# print(blocks)

print(len(blocks))
IP_add_dict_list = defaultdict(list)
IP_add_dict_set = defaultdict(set)

for block in blocks:
    if ("CSeq: 1 INVITE" or "CSeq: 1 INVITE") in block:
        caller = r";received=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3};"
        x = re.search(caller, block)
        callerIP = x.group()
        callerIP = callerIP[10:-1]
        to = r"To: <sip:(\d|[+]|(0-9))*@"
        y = re.search(to, block)
        toSip = y.group()
        toSip = toSip[9:-1]
        call = (callerIP, toSip)
        result.append(call)
        # print(callerIP, toSip)
        IP_add_dict_list[callerIP].append(toSip)
        IP_add_dict_set[callerIP].add(toSip)
print(IP_add_dict_list)
print("---------------")
print(IP_add_dict_set)

My Output:- error

with io.open('file location', 'rb', encoding='utf-8') as f:
ValueError: binary mode doesn't take an encoding argument
1 Answers

You said in comments that the command you used to save the output was

tcpdump -i eth0 -s 1500 udp port 5060 -w sip.txt

tcpdump -w saves the captured packets in a binary format described e.g. here and here. The name of the file doesn't affect this. The description in the tcpdump man page says:

-w
Write the raw packets to file rather than parsing and printing them out.

It the -w option is not set, tcpdump instead parses the packets and prints them out in text. If you want to save this output, you can just redirect the output to a file, e.g. with tcpdump -l ... > dump.txt or tcpdump -l ... >> dump.txt, where the -l options tells it to make stdout line buffered, making sure the output gets actually written even if tcpdump gets terminated with e.g. Ctrl-C.

Using both -w and an output redirection probably will not do much, as with -w, there will be little to no output on standard output.

Related