I want to know how can I verify that a packet I received from the sr1() function in Scapy contains a TCP layer, in order to do some treatment on the TCP flags.
You have two options, the in operator is one.
>>> TCP in pkt
True
>>> if TCP in pkt:
... # Handle TCP Flags
Packet objects in Scapy also have a function called haslayer().
>>> pkt = IP()/TCP()
>>> pkt.haslayer(TCP)
1
>>> pkt2 = IP()/UDP()
>>> pkt2.haslayer(TCP)
0
>>> Packet.haslayer.__doc__
'true if self has a layer that is an instance of cls. Superseded by "cls in self" syntax.'
The accepted answer may not give the wanted result in cases of encapsulation. Normally you should check the protocol at particular place using the method layers():
>>> pkt1 = IP()/TCP()
>>> pkt1.layers()
[scapy.layers.inet.IP, scapy.layers.inet.TCP]
>>> pkt1.layers()[1] == TCP
True
>>> pkt2 = IP()/UDP()
>>> pkt2.layers()[1] == TCP
False
Demonstration how the answer by RyPeck may give unwanted result:
>>> pkt3 = IP()/IP()/TCP()
>>> pkt3.layers()[1] == TCP
False
>>> pkt3.haslayer(TCP)
True
Related
Torch claim that EmbeddingBag with mode="sum" is equivalent to Embedding followed by torch.sum(dim=1), but how can I implement it in detail? Let's say we have
"EE = nn.EmbeddingBag(n, m, mode="sum", sparse=True)", how can we replace the "nn.EmbeddingBag" by "nn.Embeeding" and "torch.sum" equivalently? Many thanks
Consider the following example where all four implementations yield the same result:
nn.EmbeddingBag:
>>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')
>>> embedding_sum(input, torch.zeros(1).long())
nn.functional.embedding_bag:
>>> F.embedding_bag(input, embedding_sum.weight, torch.zeros(1).long(), mode='sum')
nn.Embedding:
>>> embedding = nn.Embedding(10, 3)
>>> embedding.weight = embedding_sum.weight
>>> embedding(input).sum(0)
nn.functional.embedding:
>>> F.embedding(input, embedding_sum.weight).sum(0)
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.
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
I'd like to add a variable called foo to the weights of a Conv2D layer to keep track of some book keeping.
This is my attempt:
kernels = model.get_layer(name='conv2d_1').get_weights()
kernels.foobar = 4
Note that kernels is of type list.
However, the previous code produce the following error:
AttributeError: 'list' object has no attribute 'foobar'
Any idea?
You can't use kernels.__setattr__('foobar', 4) / setattr(kernels, 'foobar', 4) in the way that you want, so you can't set an arbitrary attribute like you can with a custom class.
Perhaps in this case you do a very basic subclass on list:
class Kernels(list):
def __setattr__(self, attr, x):
# Make sure you aren't overriding some method of lists,
# such as `.append()`
if attr in self.__dir__():
raise ValueError('%s is already a method.')
super().__setattr__(attr, x)
>>> k = Kernels([1, 2, 3])
>>> k.foobar = 4
>>> k
[1, 2, 3]
>>> k.foobar
4
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()