Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Created May 16, 2018 10:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lambdalisue/f472723209426f4705de67a2cc20a378 to your computer and use it in GitHub Desktop.
synping which works on BSD (macOS) as well (fix ip_len byteorder issue)
import socket
import random
import platform
from dpkt.tcp import TCP, TH_SYN
print(platform.system())
if platform.system() in ('FreeBSD', 'Darwin'):
#
# > Before FreeBSD 10.0 packets received on raw IP sockets had the ip_hl sub-
# > tracted from the ip_len field.
# >
# > Before FreeBSD 11.0 packets received on raw IP sockets had the ip_len and
# > ip_off fields converted to host byte order. Packets written to raw IP
# > sockets were expected to have ip_len and ip_off in host byte order.
#
# https://www.freebsd.org/cgi/man.cgi?query=ip&sektion=4&manpath=freebsd-release-ports#end
from dpkt.ip import IP as BaseIP
class IP(BaseIP):
def pack_hdr(self):
self.len = socket.htons(self.len)
self.off = socket.htons(self.off)
try:
return super().pack_hdr()
finally:
self.len = socket.htons(self.len)
self.off = socket.htons(self.off)
else:
from dpkt.ip import IP
s = socket.socket(
socket.AF_INET,
socket.SOCK_RAW,
socket.IPPROTO_RAW,
)
# IPPROTO_RAW imply IP_HDRINCL in Linux but it is required
# in at least macOS High Sierra
s.setsockopt(
socket.IPPROTO_IP,
socket.IP_HDRINCL,
1,
)
PORT = 80
tcp = TCP(
sport=random.randint(2**10, 2**16),
dport=PORT,
flags=TH_SYN,
)
ip = IP(
p=socket.IPPROTO_TCP,
src=socket.inet_aton('10.10.0.113'),
dst=socket.inet_aton('10.10.0.1'),
data=tcp,
)
print(' '.join(format(b, '02x') for b in bytes(ip)))
s.sendto(bytes(ip), ('10.10.0.1', 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment