Skip to content

Instantly share code, notes, and snippets.

@ca0s
Created September 4, 2017 07:39
Show Gist options
  • Save ca0s/c016ac1f853ffdfaafffdb644db3bd4f to your computer and use it in GitHub Desktop.
Save ca0s/c016ac1f853ffdfaafffdb644db3bd4f to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
# ca0s @ ka0labs.net
# make AFL write to a FIFO file, relay it to your server's socket
import argparse
import socket
import select
import sys
import os
FAIL_COUNT_WARN = 5000
ROUND_COUNT_INFO = 10000
if __name__ == '__main__':
parser = argparse.ArgumentParser("AFL fifo -> socket relay")
parser.add_argument(
'source',
help='Source fifo'
)
parser.add_argument(
'sink',
help='Sink socket. Format must be type:args. Allowed types are "tcp", "udp" and "unix". tcp/udp argument is ip:port. unix argument is the local path where the socket is located'
)
args = parser.parse_args()
pipe_path = args.source
server_address = args.sink
stype, addr = server_address.lower().split(":", 1)
if stype in [ "udp", "tcp" ]:
addr, port = addr.split(":", 1)
port = int(port)
addr = (addr, port)
sfamily = socket.AF_INET
if stype == "udp":
sproto = socket.SOCK_DGRAM
else:
sproto = socket.SOCK_STREAM
elif stype == "unix":
sfamily = socket.AF_UNIX
sproto = socket.SOCK_STREAM
else:
print 'Unsupported socket protocol'
parser.print_help()
sys.exit(-1)
print 'Source: %s' % pipe_path
print 'Sink: %s' % str(addr)
count = 0
fails = 0
while True:
while True:
try:
fp = open(pipe_path, "rb")
break
except:
fails += 1
if fails % FAIL_COUNT_WARN == 0:
print 'Warning: opening the source pipe failed %s times' % fails
pass
m = fp.read()
fp.close()
sock = socket.socket(sfamily, sproto)
fails = 0
while True:
try:
sock.connect(addr)
break
except Exception as e:
#print e
fails += 1
if fails % FAIL_COUNT_WARN == 0:
print 'Warning: connecting to the sink socket failed %s times' % fails
pass
try:
sock.sendall(m)
except:
pass
#try:
# r = sock.recv(1024)
#except:
# pass
try:
sock.close()
except:
pass
count += 1
if count % ROUND_COUNT_INFO == 0:
print 'Rounds: %s' % count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment