Skip to content

Instantly share code, notes, and snippets.

@m7x
Last active November 29, 2017 23:28
Show Gist options
  • Save m7x/f938e8d3dd1231b433f475b717589986 to your computer and use it in GitHub Desktop.
Save m7x/f938e8d3dd1231b433f475b717589986 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import argparse
import subprocess
import sys
import netifaces as ni
def run():
try:
print("Listening on all TCP ports")
subprocess.Popen("iptables -A PREROUTING -t nat -i "+args.interface+" -p tcp --dport 1:65535 -j DNAT --to-destination "+ args.address+":"+args.port,shell=True).wait()
subprocess.Popen("ncat -lkp "+args.port,shell=True).wait()
except KeyboardInterrupt:
print("\nKilling listener and removing iptables rule")
subprocess.Popen("iptables -D PREROUTING -t nat -i "+args.interface+" -p tcp --dport 1:65535 -j DNAT --to-destination "+ args.address+":"+args.port,shell=True).wait()
sys.exit()
except Exception, e:
print("Error: "+str(e))
def getIP(interface):
ni.ifaddresses(interface)
ip = ni.ifaddresses(interface)[2][0]['addr']
return ip
if __name__ == "__main__" :
parser = argparse.ArgumentParser()
parser.add_argument("-i","--interface",help="Specify interface (Default eth0)")
parser.add_argument("-a","--address",help="Specify IP address")
parser.add_argument("-p","--port",help="Specify port to redirect traffic (Default: 9999)")
args = parser.parse_args()
if (args.interface is None):
args.interface="eth0"
if (args.port is None):
args.port = "9999"
if (args.address is None):
try:
args.address = getIP(args.interface)
except KeyError:
print("Interface "+args.interface+" is without IP address")
sys.exit()
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment