Extracting TCP data with Scapy - python

I'm currently trying to extract the payload of a TCP packet (which is just a single letter) with Scapy, but it keeps giving me a NoneType exception after going through the first two packets.
from scapy.all import *
import base64
capture = rdpcap('Data.pcapng') # pcap file
output = open('output.bin','wb') # save dumped data to output.bin
for packet in capture:
# if packet is ICMP and type is 8 (echo request)
if packet[TCP].src == '172.16.139.128':
output.write(packet[TCP].payload) # write packet data to output.bin
The exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/packet.py", line 372, in __getattr__
fld, v = self.getfield_and_val(attr)
TypeError: cannot unpack non-iterable NoneType object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "exploit.py", line 10, in <module>
if packet[TCP].src == '172.16.139.128':
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/packet.py", line 374, in __getattr__
return self.payload.__getattr__(attr)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/packet.py", line 372, in __getattr__
fld, v = self.getfield_and_val(attr)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/packet.py", line 1600, in getfield_and_val
raise AttributeError(attr)
AttributeError: src
Does anyone know what's causing this? It should be outputting more.

The IP source address is in the IP payload, not in TCP's. Hence, the error. So if you want to check whether the packet comes from 172.16.139.128 and has a TCP payload, the appropriate test would be:
if IP in packet and packet[IP].src == '172.16.139.128' and TCP in packet:
output.write(packet[TCP].payload)

Related

How to fix - TypeError: can't set attributes of built-in/extension type 'set'

I'm relatively new to python and I'm trying to make a simple GUI chat with python. It is programmed to ask for a nickname when a client joins the server. All works fine until the part where I enter the nickname. When I enter the nickname I get these errors from the server and client respectively, I'll provide the tracebacks as well.
ConnectionResetError: [Errno 104] Connection reset by peer
(From server)
traceback:
Traceback (most recent call last):
File "server.py", line 51, in <module>
receive()
File "server.py", line 44, in receive
broadcast(f"{nickname} entered to the chat!\n".encode('utf-8'))
File "server.py", line 17, in broadcast
client.send(message)
TypeError: can't set attributes of built-in/extension type 'set'
(from client)
traceback:
Traceback (most recent call last):
File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 94, in <module>
client = Client(HOST, PORT)
File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 22, in __init__
set.gui_done = False
The code for the server and client are also linked.
server -> https://pastebin.com/0W7Cw9Cu
client-> https://pastebin.com/FES2UNc1
What I have tried:
I tried googling for answers and I can't say I didn't get any, but I didn't understand how to implement those solutions for my issue. These are the links I referred to
Python handling socket.error: [Errno 104] Connection reset by peer
python can't set attributes of built-in/extension type 'object'
I think the error message points us to the problem pretty precisely.
Traceback (most recent call last):
File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 94, in <module>
client = Client(HOST, PORT)
File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 22, in __init__
set.gui_done = False
TypeError: can't set attributes of built-in/extension type 'set' (from client)
On line 22 of client.py, you try to assign set.gui_done = False; you probably meant self.gui_done.
Your problem is this line
set.gui_done = False
set is a protected keyword in python. It's reserved for the set data type. With this line youre telling the interpreter to change the gui_done attribute of the in-built set type to false which is not allowed. That's why you're getting a TypeError. If it wasn't an in-built type you'd still get an AttributeError because set doesn't have a gui_done attribute. You probably meant to use self instead of set

Python `Error in atexit._run_exitfuncs`

I am trying to use kafka MultiProcessConsumer but I am getting following error. It seems like error is related to multithreading in python
Here is the code which I am using.
simple.py
from kafka import SimpleProducer, SimpleClient, SimpleConsumer, MultiProcessConsumer
# To consume messages
client = SimpleClient('localhost:9092')
consumer = MultiProcessConsumer(client, "my-group", "testing_topic", num_procs=3)
for message in consumer:
# message is raw byte string -- decode if necessary!
# e.g., for unicode: `message.decode('utf-8')`
print(message)
client.close()
Error while running above code.
$ python simple.py
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/var/users/ec2-user/.pyenv/versions/3.6.0/lib/python3.6/multiprocessing/managers.py", line 749, in _callmethod
conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/users/ec2-user/.pyenv/versions/3.6.0/lib/python3.6/multiprocessing/connection.py", line 614, in SocketClient
s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory

Timeout error with PyVISA -- communicating with Agilent 34970A throug RS232 (USB)

This is my first time i try to use Pyvisa, in order to communicate with an Agilent 34970A, using a RS232 connection(using an USB port).
This is what's happening to me, inserting the basic first lines:
IN: import visa
IN: rm=visa.ResourceManager()
IN: print rm.list_resources()
(u'ASRL4::INSTR',)
IN: inst=rm.open_resource("ASRL4::INSTR")
IN: print inst.query("*IDN?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda2\lib\site-packages\pyvisa-1.8-py2.7.egg\pyvisa\resources\messagebased.py", line 407, in query
return self.read()
File "C:\Anaconda2\lib\site-packages\pyvisa-1.8-py2.7.egg\pyvisa\resources\messagebased.py", line 332, in read
message = self.read_raw().decode(enco)
File "C:\Anaconda2\lib\site-packages\pyvisa-1.8-py2.7.egg\pyvisa\resources\messagebased.py", line 306, in read_raw
chunk, status = self.visalib.read(self.session, size)
File "C:\Anaconda2\lib\site-packages\pyvisa-1.8-py2.7.egg\pyvisa\ctwrapper\functions.py", line 1582, in read
ret = library.viRead(session, buffer, count, byref(return_count))
File "C:\Anaconda2\lib\site-packages\pyvisa-1.8-py2.7.egg\pyvisa\ctwrapper\highlevel.py", line 188, in _return_handler
raise errors.VisaIOError(ret_value)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
This timeout error happens everytime i try to read a value. I imposed also a larger timeout but nothing, i waited some minutes in vain. Any idea for this problem?
You need to match up your baud rate. Either on your 34970A or in the attributes of pyVisa.
The default that comes with visa is 9600.
https://buildmedia.readthedocs.org/media/pdf/pyvisa/master/pyvisa.pdf
here's an example if you wanted to change it to 115200, the highest baud on the 34970A.
inst = rm.open_resource('ASRL4::INSTR')
inst.baud_rate = 115200
Try changing the RS232 setting protocol to be XON/XOFF.

Packet sending with scapy

I tried to send icmp packet with scapy without success,
to understand where and what is the problem. I wrote some code in python that sends icmp packet.
Of course the code is running successfully without any problem.
also in scapy the problem is present,
My internal ip is 10.0.0.8 in 10.0.0.0/24 network
the code with error in scapy for example is :
send(IP(dst="10.0.0.138")/ICMP())
send(IP(dst="8.8.8.8")/ICMP())
the error message is:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\site-packages\scapy\sendrecv.py", line 251, in send
__gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop, count=count,verbose=verbose, realtime=realtime)
File "C:\Python26\lib\site-packages\scapy\sendrecv.py", line 234, in __gen_send
s.send(p)
File "C:\Python26\lib\site-packages\scapy\arch\pcapdnet.py", line 237, in send
ifs = dnet.eth(iff)
File "dnet.pyx", line 112, in dnet.eth.__init__
OSError: Result too large
Note: when I try to run:
send(IP(dst="10.0.0.8")/ICMP())
or
send(IP(dst="127.0.0.1")/ICMP())
the packet is sent successfully!
you must start scapy with sudo scapy

Using TFTPY in python causing time out

I was trying to transfer a log file using tftp. For client side I am using TFTPY module in python. But it is showing below mentioned error messages.
WARNING:tftpy:Timeout waiting for traffic, retrying...
ERROR:tftpy:Timed-out waiting for traffic
WARNING:tftpy:resending last packet
WARNING:tftpy:Resending packet RRQ packet: filename = /opt/TDK/logs//55194186195512_Console.log mode = octet on sessions <tftpy.TftpStates.TftpStateSentRRQ object at 0xb70a0f8c>
Traceback (most recent call last):
File "callConsoleLogTransfer.py", line 30, in <module>
consoleLogTransfer(IP,Port,logTransferPort,fileName,localFilePath)
File "/file/consoleLogTransfer.py", line 68, in consoleLogTransfer
client.download( remoteFile, localFile, timeout=20 )
File "/usr/local/lib/python2.7/dist-packages/tftpy/TftpClient.py", line 52, in download
self.context.start()
File "/usr/local/lib/python2.7/dist-packages/tftpy/TftpContexts.py", line 378, in start
self.state.resendLast()
File "/usr/local/lib/python2.7/dist-packages/tftpy/TftpStates.py", line 203, in resendLast
(self.context.host, self.context.tidport))
TypeError: an integer is required

Categories

Resources