Scapy Ethernet packet from byte string loses information of top layers - byte

I am trying to build scapy Ether packet from raw string.
packet = packets[4] # this is the packet I get from pcap file
str_packet = str(packet) # I get string form from here
packet2 = Ether(str_packet) # I try to make packet2 from the string
packet.show()
packet2.show()
Outputs here shows that Ether packet2 is not formed well.
Any idea, How can I get packet2 from raw string ?
###[ Ethernet ]###
dst = 80:e6:50:14:3d:52
src = 2a:74:02:9b:85:64
type = IPv4
###[ IP ]###
version = 4
ihl = 5
tos = 0x0
len = 52
id = 44178
flags =
frag = 0
ttl = 88
proto = tcp
chksum = 0x5503
src = 157.240.13.35
dst = 172.20.10.7
\options \
###[ TCP ]###
sport = https
dport = 60643
seq = 905248884
ack = 938762494
dataofs = 8
reserved = 0
flags = A
window = 113
chksum = 0x43e9
urgptr = 0
options = [('NOP', None), ('NOP', None), ('Timestamp', (2596765797, 886096700))]
###[ Raw ]###
load = "b'\\x80\\xe6P\\x14=R*t\\x02\\x9b\\x85d\\x08\\x00E\\x00\\x004\\xac\\x92\\x00\\x00X\\x06U\\x03\\x9d\\xf0\\r#\\xac\\x14\\n\\x07\\x01\\xbb\\xec\\xe35\\xf5\\x00t7\\xf4`\\xfe\\x80\\x10\\x00qC\\xe9\\x00\\x00\\x01\\x01\\x08\\n\\x9a\\xc7\\x80e4\\xd0\\xc3<'"

At line 2, use raw instead of str. I extracted some frame from a pcap file as you did and here is what I get:
>>> str(p)
WARNING: Calling str(pkt) on Python 3 makes no sense!
"b'\\xf4\\xca\\xe5Cu\\x10\\x00!\\xcc\\xd3px\\x08\\x00E\\x00\\x004\\xbe\\x05#\\x00#\\x06\\xfb\\xc4\\xc0\\xa8\\x01\\x0f6\\xbf\\x88\\x83\\x96N\\x01\\xbb5M\\x94r\\xd0\\xc7\\xd9\\xae\\x80\\x10\\x01\\xf5\\x81 \\x00\\x00\\x01\\x01\\x08\\np\\xaf\\xdf\\xc4a\\xcf=\\x84'"
>>> raw(p)
b'\xf4\xca\xe5Cu\x10\x00!\xcc\xd3px\x08\x00E\x00\x004\xbe\x05#\x00#\x06\xfb\xc4\xc0\xa8\x01\x0f6\xbf\x88\x83\x96N\x01\xbb5M\x94r\xd0\xc7\xd9\xae\x80\x10\x01\xf5\x81 \x00\x00\x01\x01\x08\np\xaf\xdf\xc4a\xcf=\x84'
>>> Ether(raw(p)).show()
###[ Ethernet ]###
dst= f4:ca:e5:43:75:10
src= 00:21:cc:d3:70:78
type= IPv4
###[ IP ]###
version= 4
ihl= 5
tos= 0x0
len= 52
id= 48645
flags= DF
frag= 0
ttl= 64
proto= tcp
chksum= 0xfbc4
src= 192.168.1.15
dst= 54.191.136.131
\options\
###[ TCP ]###
sport= 38478
dport= https
seq= 894276722
ack= 3502758318
dataofs= 8
reserved= 0
flags= A
window= 501
chksum= 0x8120
urgptr= 0
options= [('NOP', None), ('NOP', None), ('Timestamp', (1890574276, 1640971652))]

Related

Check and cast error in Omnet++ TSN. Unable to transmit UDP packets

I am trying to send a UDP packet from Omnett ++ TSN Device to a standard Host through a TSN switch that is connected to a Router.
However, I get the following check_and_cast error:-
check_and_cast(): Cannot cast(inet::physicallayer::signal*)app[0]-0 to type 'inet::physicallayer::EthernetSignalBase *' in module (inet::EthernetMac) of router.eth[0].mac
My omnetpp.ini udp app setup is as follows.
extends = omnetpptsnnetworksample
#Source application
*.tsnDevice1.numApps = 1
*.tsnDevice1.app[0].typename = "UdpSourceApp"
*.tsnDevice1.app[0].source.packetLength = 10B
*.tsnDevice1.app[0].source.productionInterval = 1ms
*.tsnDevice1.app[0].io.destAddress = "ue[0]"
*.tsnDevice1.app[0].io.destPort = 1000
*.tsnDevice1.app[0].source.clockModule = "^.^.clock"
#Sink application
*.standardHost[*].numApps = 1
*.standardHost[*].app[*].typename = "UdpSinkApp"
*.standardHost[*].app[*].io.localPort = 1000
Where did I go wrong?
TsnDevice and TsnSwitch have LayeredEthernetInterface by default, but StandardHost has EthernetInterface. The two interfaces are not compatible (not sure if they should be or not). So by setting standardHost's ethernet interface type to LayeredEthernetInterface, it should work:
*.standardHost[*].eth[*].typename = "LayeredEthernetInterface"

How to send UDP packet in a wireless network from multi-host simultaneously in INET/Veins_inet

Greeting all,
I am trying to edit Veins_inet (square example)to the following scenario:
node#1,2 send UDP packet to node#0 at time 15s and stop sending at 18s.
but when I do that the UDP packet not sent simultaneously!
How can I make the two node target node#0 simultaneously?
here part of .ini file
# UDPBasicApp
MyScenario.node[1..2].numUdpApps = 2
MyScenario.node[1..2].udpApp[0].typename = "UDPBasicApp"
MyScenario.node[1..2].udpApp[0].destAddresses = "224.0.0.1"
MyScenario.node[1..2].udpApp[0].multicastInterface = "wlan0"
MyScenario.node[1..2].udpApp[0].joinLocalMulticastGroups = true
#MyScenario.node[1..2].udpApp[0].localPort = 9001
MyScenario.node[1..2].udpApp[0].destPort = 9001
MyScenario.node[1..2].udpApp[0].messageLength = 100B
MyScenario.node[1..2].udpApp[0].startTime = uniform(0s, 5s)
MyScenario.node[1..2].udpApp[0].sendInterval = 5s #uniform(0.01s, 0.05s)
#atttack
MyScenario.node[1..2].udpApp[1].typename = "UDPBasicApp"
#MyScenario.node[1..2].udpApp[1].destAddresses = "224.0.0.1"
MyScenario.node[1..2].udpApp[1].multicastInterface = "wlan0"
MyScenario.node[1..2].udpApp[1].joinLocalMulticastGroups = true
# MyScenario.node[1..2].udpApp[1].localPort = 9001
MyScenario.node[1..2].udpApp[1].destAddresses = "node[0]"
MyScenario.node[1..2].udpApp[1].destPort = 9001
MyScenario.node[1..2].udpApp[1].messageLength = 100B
MyScenario.node[1..2].udpApp[1].startTime = 15s #uniform(0s, 5s)
MyScenario.node[1..2].udpApp[1].stopTime = 18s
MyScenario.node[1..2].udpApp[1].sendInterval = 0.01s #uniform(0.01s, 0.05s)
MyScenario.node[1..2].udpApp[1].packetName = "attack"
Any help will be very appreciated to help me to move forward in my project

Make IPv6 UDP socket on windows to receive from any interface

I want to have an IPv6 UDP socket that can receive broadcast/multicast messages from any local interface using Link-Local addresses.
In Linux it is enough to bind it to in6addr_any, but in Windows you will not receive any multicast until you join a multicast group using setsockopt() + IPV6_JOIN_GROUP. The problem that an interface index must be provided during this option. But this is inconvenient. Is there a way to receive multicast from any interface in Windows?
UPD: I use destination address ff02::1 (All Nodes Address)
For IPv4, the index of the network interface is the IP address; for IPv6 the index of the network interface is returned by the method socket.getaddrinfo.
The code below shows how to listen to multicast on all network interfaces:
from socket import AF_INET6, AF_INET
import socket
import struct
# Bugfix for Python 3.6 for Windows ... missing IPPROTO_IPV6 constant
if not hasattr(socket, 'IPPROTO_IPV6'):
socket.IPPROTO_IPV6 = 41
multicast_address = {
AF_INET: ["224.0.1.187"],
AF_INET6: ["FF00::FD"]
}
multicast_port = 5683
addr_info = socket.getaddrinfo('', None) # get all ip
for addr in addr_info:
family = addr[0]
local_address = addr[4][0]
sock = socket.socket(family, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((local_address, multicast_port))
if family == AF_INET:
for multicast_group in multicast_address[family]:
sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(multicast_group) + socket.inet_aton(local_address)
)
elif family == AF_INET6:
for multicast_group in multicast_address[family]:
ipv6mr_interface = struct.pack('i', addr[4][3])
ipv6_mreq = socket.inet_pton(socket.AF_INET6, multicast_group) + ipv6mr_interface
sock.setsockopt(
socket.IPPROTO_IPV6,
socket.IPV6_JOIN_GROUP,
ipv6_mreq
)
# _transport, _protocol = await loop.create_datagram_endpoint(
# lambda: protocol_factory(), sock=sock)

wvdial, pppd and default route metric

I have this in wvdial.conf:
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0
Modem Type = USB Modem
Phone = *99#
New PPD = yes
ISDN = 0
Username = foo
Init1 = ATZ
Password = foo
Modem = /dev/ttyUSB1
Baud = 9600
Stupid Mode = 0
#Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CFUN=1
Init4 = AT+CGDCONT=1,"ip","internet"
Auto Reconnect = on
(PIN disabled)
This is working for 4G USB Dongle Huawei E3131 # T-mobile.pl.
And now I have 2 questions:
1 How to translate this config to /etc/conf.d/net for connect as net.ppp1 (Gentoo) (I would connecting without wvdial)
2 After connecting I get default route with metric 0, How to change metric to say 100? (I'm interested in setting metric for both wvdial & pppd way)

Create a UDP ping packet to fetch mumble(murmur) status

I'd like to send a udp packet to a mumble server using ruby to get status information about how many users are currently connected.
The documentation states there is a way using a UDP packet: http://mumble.sourceforge.net/Protocol#UDP_Ping_packet
However I don't know how to formulate that with ruby and thus get no reply from the server.
require 'socket'
sock = UDPSocket.new
sock.connect("99.99.99.99", 66666)
sock.send("00", 0)
p sock.recvfrom(1) # this does not return
sock.close
How do I format data of the udp packet?
This should work to generate your ping packet:
def ping(identifier)
v = identifier
a = []
while v > 256 # extract bytes from the identifier
a << v % 256
v = v / 256
end
a << v % 256
prefix = [0] * (8-a.length) # pad the identifier
([0,0,0,0] + prefix + a).pack("C*") # pack the message as bytes
end
usage:
# random 8 byte number as a message identifier - compare this to any packet
# received to ensure you're receiving the correct response.
identifier = rand(256**8)
sock.send ping(identifier), 0
# you should get a response here if the mumble server is
# accessible and responding to pings.
sock.recvfrom(1)

Resources