Scapy extract TCP window size - scapy

I wanted to know how to extract the TCP window size using Scapy
For example to get the IP packet payload I use the following code
def packethandler(pkts):
with codecs.open("Dropcam.csv", mode='w', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
for pkts in pkt:
data = (len(pkts))
data2 = (len(pkts.payload))
ether_len.append(data)
IP_len.append(data2)
ethernet_header_min = min(ether_len) # min ethernet frame length
ethernet_header_max = max(ether_len) # max ethernet frame length
````

You can use this syntax:
pkt[TCP].window

Related

How do I convert a CAN message to the bytearray format needed by UDP sendto()? [duplicate]

I made UDP socket connection between two Linux machines and can send for example b"Hello, World!" easily. But now I need to send the below CAN frame
from can import Message
send_msg = Message(data=[1, 2, 3, 4, 5])
So if I print send_msg it shows:
Timestamp: 0.000000 ID: 00000000 X DLC: 5 01 02 03 04 05
I want to get this printed on the receiving end. The sending and receiving end codes I am using are below:
Sending:
import socket
UDP_IP = "10.140.189.249"
UDP_PORT = 5005
from can import Message
send_msg = Message(data=[1, 2, 3, 4, 5])
print(send_msg)
MESSAGE = send_msg
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
Here I know MESSAGE = send_msg is wrong.
Receiving
import socket
UDP_IP = "0.0.0.0"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
rec_msg, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("received message: %s" % rec_msg)
Please advise
As the physical layer of an UDP connection and a CAN connection are substantially different, you cannot send the CAN frames over UDP. What is of course possible is to send the payload of the CAN frame and assemble the CAN message on the receiving side: I.e. on the sending side:
sock.sendto(b“12345“, (UDP_IP, UDP_PORT))
And on the receiving side:
msg = Message(data=bytearray(recv_msg))
Most likely you do not only want to transfer the data of the CAN frame, but also the ids and other fields.
Another possibility would be to pickle the Message object on the sending side and unpickle it on the receiving side using pickle.dumps and pickle.loads
All features of the CAN bus like arbitration, error-frames etc. cannot be mimicked on a UDP connection.

How to avoid MPU9250 FIFO overflow?

I'm now working with mpu9250 for my master thesis, but I'm quite new to hardware device. My goal is to collect the data at the sample rate and postprocess these data via allan deviation. So I just want to get the data from 512 bytes fifo, but when I'm trying to read out the data, it always overflows which annoying me a lot.
I know the method is to set the reading speed faster than the fifo writing speed, so I remove the time sleep in my loop, but it seems overflows sooner than before. I'm a little bit confused, because the reading speed depends on the writing speed.
#!/usr/bin/env python3
import os
import time
import sys
import struct
import csv
from datetime import datetime
from mpu9250_FIFO import MPU9250
from navio.leds import Led
def main():
# Initialize MPU9250
mpu = MPU9250()
mpu.bus_open(max_speed_hz=10000000) # Hz
sampleRate = 50
sampleRateDiv = int(1000 / sampleRate - 1)
mpu.initialize(sample_rate_div=sampleRateDiv, low_pass_filter_gt=0x01, low_pass_filter_a=0x01)
mpu.set_acc_scale(0x08) # +/-4G
mpu.set_gyro_scale(0x08) # +/-500dps
# Enable FIFO to collect data
mpu.enableFIFO(True)
# Test connection:
if mpu.testConnection():
print("Connection working.")
else:
print("Connection to one of the sensors is faulty.")
now = datetime.now().strftime('%d-%m-%Y_%H:%M:%S')
fname = '/home/pi/Python/meas_data/mpu9250_data_' + now + r'.txt'
with open(fname, 'w', 1) as data_imu:
# write headers to file
data_imu.write('Sample Rate: %d Hz\n' % sampleRate)
data_imu.write('mpu_accel_1, mpu_accel_2, mpu_accel_3, mpu_gyro_1, mpu_gyro_2, mpu_gyro_3, temp\n')
#print("----1----")
# Main loop
while True:
mpudata_a, mpudata_g, mpudata_temp = mpu.getFIFOData()
#print("----2----")
data = mpudata_a + mpudata_g + mpudata_temp
# print(data)
data_imu.write(str(data).replace("[", "").replace("]", "") + "\n")
# FIFO READ AND WRITE
# FIFO_R_W REGISTER:
# This register is used to read and write data from the FIFO buffer. Data is written to the FIFO
# in order of register number (from lowest to highest). If all the FIFO enable flags are enabled
# and all External Sensor Data registers (Registers 73 to 96) are associated with a Slave device,
# the contents of registers 59 through 96 will be written in order that the Sample Rate.
# The contents of the sensor data registers (Registers 59 to 96) are written into the FIFO buffer
# when their corresponding FIFO enable flags are set to 1 in FIFO_EN (Register 35). An additional
# flag for the sensor data registers associated with I2C Slave 3 can be found in I2C_MST_CTRL
# (Register 46).
# If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is automatically set to 1.
# This bit is located in INT_STATUS (Register 58). When the FIFO buffer has overflowed, the
# oldest data will be lost and new data will be written to the FIFO.
# If the FIFO buffer is empty, reading this register will return the last byte that was previously
# read from the FIFO until new data is available. The user should check FIFO_COUNT to ensure that
# the FIFO is not read when empty.
# -----------------------------------------------------------------------------------------------
def getFIFOData(self):
while True:
mpu_int_status = self.getIntStatus()
INT_FIFO_OFLOW_BIT = 0x10 # the 4th bit is set to 1 when FIFO buffer is overflowed
fifoCount = self.getFIFOCount()
#print("fifoCount:%d" % fifoCount)
if fifoCount < 14:
continue
# check for overflow (this shouldn't happen)
elif (mpu_int_status & 0x10 == 0x10) or fifoCount>=512:
print("fifoCount is: %d" % fifoCount)
print("FIFO is overflow!")
# print("Reset FIFO...")
# self.resetFIFO()
break
else:
packet_count = int(fifoCount/14)
# print("packet count:%d" % packet_count)
for i in range(0, packet_count):
response = self.ReadRegs(self.__MPUREG_FIFO_R_W, 14)
# print(response)
# Get Accelerometer values
for i in range(0, 3):
data = self.byte_to_float(response[i * 2:i * 2 + 2])
self.accelerometer_data[i] = self.G_SI * data / self.acc_divider
# print(self.accelerometer_data)
# Get temperature
i = 3
temp = self.byte_to_float(response[i * 2:i * 2 + 2])
self.temperature[i - 3] = (temp / 333.87) + 21.0
# print(self.temperature)
# Get gyroscope values
for i in range(4, 7):
data = self.byte_to_float(response[i * 2:i * 2 + 2])
self.gyroscope_data[i - 4] = (self.PI / 180) * data / self.gyro_divider
# print(self.gyroscope_data)
return self.accelerometer_data, self.gyroscope_data, self.temperature
def getIntStatus(self):
return self.ReadReg(self.__MPUREG_INT_STATUS)
def getFIFOCount(self):
response = self.ReadRegs(self.__MPUREG_FIFO_COUNTH, 2)
fifoCount = self.byte_to_float(response)
return fifoCount
def resetFIFO(self):
self.WriteReg(self.__MPUREG_USER_CTRL, 0x00)
self.WriteReg(self.__MPUREG_USER_CTRL, 0x04) # reset fifo mode
self.WriteReg(self.__MPUREG_USER_CTRL, 0x40) # FIFO_EN
print('fifo is reset!')
def enableFIFO(self, flag):
self.WriteReg(self.__MPUREG_FIFO_EN, 0x00)
if flag:
self.resetFIFO()
self.WriteReg(self.__MPUREG_FIFO_EN, 0xF8)
print('fifo is enabled!')
I hope to get a constant fifo reading without overflow.

Using Python to read a nonfixed number of bytes through Serial

I am trying to read a nonfixed number of bytes from the Serial port. I can use read to read a fixed number of bytes, putting in a larger number than the actual number of bytes present, will cause it to wait forever. Putting in a smaller number than the actual number of bytes present will crop some of the characters out. Using read_all, the output seems to be blank. Readline() is yielding syntax error. I ran out of ideas, any ideas? Here is my code:
import time
import datetime
import serial
import os
# public variables
sensors = [] # list of sensor readings
wait = True
sensor_count = 10 # the zero based count of sensors
def pwr_solenoid(solenoid0=0, solenoid1=0, solenoid2=0, solenoid3=0):
# Defaults are for low signal values
# compile output
output = '9{solenoid0}{solenoid1}{solenoid2}{solenoid3}' \
.format(solenoid0=solenoid0, solenoid1=solenoid1, solenoid2=solenoid2, solenoid3=solenoid3).encode()
with serial.Serial('/dev/ttyACM0', baudrate=9600) as ser:
print("created connection to '/dev/ttyACM0'\n")
print("going to send:\t'{}'".format(output))
ser.write(output)
ser.reset_output_buffer()
# for testing to console
print("Value sent to the uC:\t'{}'".format(output.decode()))
# ***** READ from UART *****
#ser.in_waiting >= 12:
raw = ser.read(16)
print("printing raw value of ser.read(16)\n")
print(raw)
val = str(ser.read_all().decode()) # (3).decode()[2:])
#printing val
print("\n printing val using read_all \n")
print(val)
val1 = raw.decode()
#printing val of raw.decode()
print("\n pringting val of raw.decode() \n")
print(val1)
# print("printing value of ser.readline()\n"
# serreadlin = ser.readline() This line generates an error cannot use readline()
# print(serreadlin)
# print("printing val\n")
# print(val)
# exit()
You can use:
import serial
import time
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200, timeout=1)
while True:
val = ser.readline().strip()
if(val):
print (val)

Python IP logger taking in, and outputting data in csv format

Hello I'm looking for some help with a project I recently took up to help check pc connectivity in my workplace. I am fairly new to python and programming in general so a large portion of this may be wrong. I'm trying to create a simple IP logger that will ping the destination address and return a value to determine the connection state of the machine. It will take the data from a CSV file (excell.xlsx) and splice the IP address from the information provided in cell style format, then output the connection state with a simple "connected" or "disconnected" after relisting the input file. Here's what I've come up with so far:
import csv, platform, subprocess, os as sys
#Defines filepath locations (Adjustment may be required)
in_path = sys.os.path['C:\Users\User\Documents\IpLog\log1.xlsx']
out_path = sys.os.path['C:\Users\User\Documents\IpLog\ip_log.xlsx']
try:
ip = 0
reference = out_path
host = '?'
line_count = 0
with open(in_path, dialect='excel', delimeter=',', newline='') as csvfile:
for row in csvfile:
if line_count == 0:
ip_inp = csv.reader(in_path, dialect='excel') #adjust filename as needed.
ip = ip_inp[ip_inp.index('10.21')]
ip = ip[5::]
line_count += 1
for line in ip_inp:
def ping(ip):
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""
# Option for the number of packets as a function of
param = '-n' if platform.system().lower() == 'windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', ip]
return subprocess.call(command) == 0
if subprocess.call(command) == 0:
status = subprocess.call(command)
else:
status = 1
if status == 0:
if line_count == 0:
net_state = 'Connected'
with open(out_path, delimiter=',') as csvfile2:
print(csvfile)
csv.writer(csvfile2, dialect='excel')
print(net_state)
else:
net_state = 'Disconnected'
with open(out_path, delimiter=',') as csvfile2:
print(csvfile)
csv.writer(csvfile2, dialect='excel')
print(net_state)
except IOError:
print('Error opening the input file please check file input name and file location and try again.')
finally:
raise SystemExit('Operation Completed. Results have been written to (%s)' % out_path)
Right now the error I keep running into is truncation in my global file path variables. Can anyone tell me why this may be happening?

Why conversion works on the idle but not executing the program?

I'm playing with scapy library in python3 (https://github.com/phaethon/scapy) and sniffing the packets on the net, I get these format of packets, here are some examples:
b'\xe8t\xe6,\xf1:0\x10\xb3\x18\x8b\xfd\x08\x00E\x00\x00b\xe7\xb1#\x00#\x06\xf2j\xc0\xa8\x01h\xd8:\xc6.\xd2B\x01\xbb\xa1\x9a\xbf\x1e\x0b"q\xc1\x80\x18\x00\xfe^\x95\x00\x00\x01\x01\x08\n\x00CX6)\x818P\x17\x03\x03\x00)\x00\x00\x00\x00\x00\x00\x00\x0f\x8d\xdc\x86`:\xe9\x8f=\xec\x89u~\xdf\x7f\x1aN\x85\xf4ESt\xa0oT\xdba\xfan\xc7\xb8\x01\xf8\t'
or
b'\xe8t\xe6,\xf1:0\x10\xb3\x18\x8b\xfd\x08\x00E\x00\x01\x03\xe3\x9e#\x00#\x06\xda\xbe\xc0\xa8\x01h\xc0|\xf9\n\xcc6\x01\xbbDo\x9a\xe4N\xaf\x11~\x80\x18\x05\xa4\xc6x\x00\x00\x01\x01\x08\n\x00CY\xed`\xa8Vb\x17\x03\x03\x00\xca\x00\x00\x00\x00\x00\x00\x004;wn,\x13\x8d9\x17G\xee/\x1c\x15\x12,\xc0\xde\xf4\xab\xa5emvZ\xf5\x1f\x94\x8a\xcc4g>#\xb9\xf2dYs\xeb<l;\x8b\x8a\xccT\n\xacUT\x049\xcbp\x1c#\x87z\\[8\xde\xd7I\x14\xfd\x94\x81\x13x`\x9eH\xd8\xbc)lc\x0fE\xfc\x99F\xb0c\xb9v\x86\x1c\xc4\xec\x14\xb6\x00\x1e\x8f\xd5\x08H/\xa4\xea\xf4~2\x90\xc7\xabJ\xb9\x9f\xf9\x06\xc6\xe5$\xe4(\xab#\xd8\xf4<\x7f\xfa\xe6Y_\x80\x14\xb2H\x10\x1cT\xdf\x1f\x0f\xbb\xce\xf8\xbe q\xf3=qQ\xd2a!e\x8a\xbf9Y\x7f\xb8\xe5Bo\xd8\r\xcb\xcf\x01H\xf8x\x90\x98\xfc>$\xef>\x80;\xa9\xf9\x7f\x9f\xc5?V\xecH:\x7f\xfeb\xa5E\xab'
The weird thing, is that if I try to convert them to idle3, typing:
> from scapy.all import *
> text = b'blablablabytesstring'
> text2 = Ether(text)
> text2
And it works, it correctly converts.
But if I do the same thing, but writing a simple script, like:
from scapy.all import *
text = b'sameblablablabytesstringasidle3'
text2 = Ether(text)
print (text2)
It doesn't converts, and it prints the same bytes string.
Why? How can I solve this?
--UPDATE--
This is the output of text2.show()
###[ Ethernet ]###
dst = e8:74:e6:2c:f1:3a
src = 30:10:b3:18:8b:fd
type = 0x800
###[ IP ]###
version = 4
ihl = 5
tos = 0x0
len = 259
id = 24485
flags = DF
frag = 0
ttl = 64
proto = tcp
chksum = 0x5eb8
src = 192.168.1.104
dst = 192.124.249.10
\options \
###[ TCP ]###
sport = 51172
dport = https
seq = 3204149667
ack = 3154139287
dataofs = 8
reserved = 0
flags = PA
window = 1444
chksum = 0xb3ea
urgptr = 0
options = [('NOP', None), ('NOP', None), ('Timestamp', (4182786, 1620720294))]
###[ Raw ]###
load = b'\x17\x03\x03\x00\xca\x00\x00\x00\x00\x00\x00\x01\xba\xc1\xa0H\xa2\xb3\t\x02:P>\x8b\xeb\xaa\xbd\x83H\x05\x1f(\x02\x80\x9e\x96\xa5\xd9\xf7\xf4\x07&s\xfd6\xb9 \x00\x8c\xcf\xd7\xe4\x04pQ\x992"$\x07R\x02\xb4\x97\xad!\xafB\xc8I\xa6\xe6\x18\xc7x\x16\x9a$c\xc4\x05}\xffl\xe7\x89\x93\xa4\x9a\xf1\x90\x8d\xde"\xe0\xb2\x06\x14(Yk\xf441\xbf\xfa?\xd4puE\x97\xcd\xb1\xc2\xfaaa\x14{\xd4f\x80\xb9\x81keO\x90\x13\x9c\x01\xaa\xe4}\xab{\xd8\x9a\xb3\xe3\x88\xcd:5\xe1\xa9\xd6:\x06l_\xed\xb3\x96=\xcc\x0f?c\xf7\xad\xed\xde\xca||\xd6\xbc\xa4\x99u\xff\xbcI\xe2\x8cQ\xe3\x87\x9f\x18(\rp\x04u\xad\r\xa55\xbe`q\x1b=`5\'\xbc\xf2\x0b\xb9V\x1ecp'
--UPDATE2--
(from idle3)
>>> from scapy.all import *
WARNING: No route found for IPv6 destination :: (no default route?). This affects only IPv6
>>> text = b"0\x10\xb3\x18\x8b\xfd\xe8t\xe6,\xf1:\x08\x00E\xa0\x004a'#\x004\x06\x90\xa2\x173{\x1b\xc0\xa8\x01d\x00P\xec\xe6\x01\x81%|\xfcY#8\x80\x10\x03\xd4(\xbe\x00\x00\x01\x01\x08\nx\x93UR\x00\x00\xd7$"
>>> text2 = Ether(text)
>>> text2
<Ether dst=e8:74:e6:2c:f1:3a src=30:10:b3:18:8b:fd type=0x800 |<IP version=4 ihl=5 tos=0x0 len=98 id=59313 flags=DF frag=0 ttl=64 proto=tcp chksum=0xf26a src=192.168.1.104 dst=216.58.198.46 options=[] |<TCP sport=53826 dport=https seq=2711273246 ack=186806721 dataofs=8 reserved=0 flags=PA window=254 chksum=0x5e95 urgptr=0 options=[('NOP', None), ('NOP', None), ('Timestamp', (4413494, 696334416))] |<Raw load=b'\x17\x03\x03\x00)\x00\x00\x00\x00\x00\x00\x00\x0f\x8d\xdc\x86`:\xe9\x8f=\xec\x89u~\xdf\x7f\x1aN\x85\xf4ESt\xa0oT\xdba\xfan\xc7\xb8\x01\xf8\t' |>>>>
--UPDATE3-- (print from program.py)
Ether / IP / TCP 192.168.1.104:53826 > 216.58.198.46:https PA / Raw
Can you try text2.show() instead of print(text2) and see how that goes?
from scapy.all import *
text = b'\xe8t\xe6,\xf1:0\x10\xb3\x18\x8b\xfd\x08\x00E\x00\x00b\xe7\xb1#\x00#\x06\xf2j\xc0\xa8\x01h\xd8:\xc6.\xd2B\x01\xbb\xa1\x9a\xbf\x1e\x0b"q\xc1\x80\x18\x00\xfe^\x95\x00\x00\x01\x01\x08\n\x00CX6)\x818P\x17\x03\x03\x00)\x00\x00\x00\x00\x00\x00\x00\x0f\x8d\xdc\x86`:\xe9\x8f=\xec\x89u~\xdf\x7f\x1aN\x85\xf4ESt\xa0oT\xdba\xfan\xc7\xb8\x01\xf8\t'
text2 = Ether(text)
text2.show()

Resources