Does scapy works on macOS without problems? - scapy

I have the following output on macOS. It keeps on printing dots. Does anybody know how to check what is wrong?
>>> a, b = sr(IP(dst="www.target.com")/TCP(sport=[RandShort()]*10))
Begin emission:
.........Finished sending 10 packets.
......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Related

Scapy show packet content after sniffing it - UnicodeEncodeError

I want to sniff ProfiNet Connect Messages and print them to console every time i receive them. Herefore i used the Scapy sniff method. Every time a Connect-Frame is received the following error is displayed. The Connect-Message is in correct format and is visible in Wireshark. The sniff-method aborts every time such a frame is received. If i export the frame with wireshark and read the .pcap-file with scapy the frame is correctly displayed. Do you have any suggestions? I'm currently using Python 3.8.10 and scapy version 2.4.5rc1.dev205. Thanks!
UnicodeEncodeError: 'latin-1' codec can't encode character '\u0797' in position 595: ordinal not in range(256)
def update_connect_load(pkt):
pkt.show2()
def stopFilter(x):
return False
sniff(
filter=f"ether src {mac_address}",
store=0,
count=-1,
prn=update_connect_load,
iface=iface,
stop_filter=stopFilter,
)`
Tried encoding the paket in the prn-function, but that didn't work. Also tried parsing the packet with DceRpc() but that also didn't work.
Just update the Python Version to 3.9 and everything works!

Python Error Handling while base decoding

I tried to do the following :
I have read a code in base64 via QR Code and then I converted it.
If I get an error while I do the convert, I will write a error variable to 1 and then continue without exiting the program.
I don't find a solution for me. Did anyone has an idea how I can handle it?
I tried it with the Python Try Command but I didn't get it working or I have done something wrong.
here is a snip of my code:
secure = base64.b64decode(secure_base).decode("utf-8", "ignore")
number = base64.b64decode(number_base).decode("utf-8", "ignore")
start = int(base64.b64decode(start_base).decode("utf-8", "ignore"))
end = int(base64.b64decode(end_base).decode("utf-8", "ignore"))
thanks a lot.
You can use the try and Except in python in the following manner.
try:
"""some intelligent program here, which some times may FOOBAR"""
except Exception as e:
error_recieved = e
"""Do whatever you want here incase of an error"""
Remember that the program in try skips to except just after the line in which the error/exception occured.

How to extract the payload of a packet using Pyshark

I am trying to read the payload of all packets in a .pcap file using Pyshark. I am able to open and read the file, access the packets and their other information but I am not able to find the correct attribute/method to use to access the payload of a packet. Any suggestions ? Is there any other way to read packet payloads in .pcap files using python for windows 10 ?
(I tried using Scapy instead of Pyshark, but apparently there is some issue with running Scapy on Windows, it does not work on my system as well)
I found these lines in different code snippets of pyshark projects on the Internet and on StackOverflow. I tried them but none of them work :
import pyshark
cap = pyshark.FileCapture('file.pcap')
pkt = cap[1]
#for other information
print(pkt.tcp.flags_ack) #this works
print(pkt.tcp.flags_syn) #this works
print(pkt.tcp.flags_fin) #this works
#for payload
print(pkt.tcp.data) #does not work, AttributeError
print(pkt.tcp.payload) #does not work, AttributeError
print(pkt.data.data) #does not work, AttributeError
This code will print the value associated with the field name tcp.payload.
capture = pyshark.FileCapture(pcap_file, display_filter='tcp')
for packet in capture:
field_names = packet.tcp._all_fields
field_values = packet.tcp._all_fields.values()
for field_name in field_names:
for field_value in field_values:
if field_name == 'tcp.payload':
print(f'{field_name} -- {field_value}')
# outputs
tcp.payload -- \xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7AP\xc2\xb7\xc2\xb7\xc2\xb7
tcp.payload -- 0x00001e2c
tcp.payload -- 113977858
...
In order to use that API you have to pass appropriate parameter into constructor of 'FileCapture' class:
import pyshark
cap = pyshark.FileCapture('file.pcap', include_raw=True, use_json=True)
pkt = cap[1]
print(pkt.data.data) # Will work
'include_raw' is the key here, but 'use_json' is needed when when 'include_raw' is used.
dir cap[].
This one will give you all accessible attributes related to your capture., look there if there is the payload option.

Python 3 - PYSerial send and receive HEX

I am trying to send and receive hex commands to and from a device.
The device sends the following hex data: \x02x82x36xFFxFFx01xB5x03
I am using the follow simple code:
import serial
port = serial.Serial('COM1', baudrate=19200, timeout=3.0)
while True:
rcv = port.readline()
print (rcv)
The output of rcv is The following python code displays the output as \x028236????01;5\x03'
Help. I am new to python and I am sure i am making a simple mistake but its driving me crazy.
Thanks
The output you are getting is the printable representation of the bytes received. Each byte is encoding an (extended) ASCII character (printable or not). Printable ones are printed as they are (in your case they are 8236, 01;5). And the others are printed as hex code \x02, \x03 (which are codes 02 and 03 respectively) and so on. If you open some ASCII table you will see that the character 2 is represented as 0x82, and the same about rest of the codes you are getting.

Read console output realtime in lua

How can I manage to periodically read the output of a script while it is running?
In the case of youtube-dl, it sends download information (progress/speed/eta) about the video being downloaded to the terminal.
With the following code I am able to capture the total result of the scripts output (on linux) to a temporary file:
tmpFile = io.open("/tmp/My_Temp.tmp", "w+")
f = io.popen("youtube-dl http://www.youtube.com/watch?v=UIqwUx_0gJI", 'r')
tmpFile:write(f:read("*all"))
Instead of waiting for the script to complete and writing all the data at the end, I would like able to capture "snapshots" of the latest information that youtube-dl has sent to the terminal.
My overall goal is to capture the download information in order to design a progress bar using Iup.
If there are more intelligent ways of capturing download information I will be happy to take advice as well.
Regardless, if it is possible to use io.popen(), os.execute(), or other tools in such a way I would still like to know how to capture the real time console output.
This works fine both on Windows and Linux. Lines are displayed in real-time.
local pipe = io.popen'ping google.com'
for line in pipe:lines() do
print(line)
end
pipe:close()
UPD :
If previous code didn't work try the following (as dualed suggested):
local pipe = io.popen'youtube-dl with parameters'
repeat
local c = pipe:read(1)
if c then
-- Do something with the char received
io.write(c) io.flush()
end
until not c
pipe:close()

Resources