Created
May 16, 2018 10:11
Star
You must be signed in to star a gist
synping which works on BSD (macOS) as well (fix ip_len byteorder issue)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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