Last active
July 18, 2024 22:29
-
-
Save staaldraad/f5a9c6acab980a62f981 to your computer and use it in GitHub Desktop.
Python script to create a Connect-Connect tunnel. For those times ncat/socat can't be put on the box and python is available..
This file contains hidden or 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
#!/usr/bin/python | |
""" | |
Python script to create a Connect-Connect tunnel. For those times ncat/socat can't be put on the box and python is available.. | |
Author: Etienne Stalmans <[email protected]> | |
Version: 1.0 (22_01_2015) | |
Usage: python pyforw.py <targetIP> <targetPort> <jumpbox> <jumpboxPort> | |
python pyforw.py 10.1.1.1 3389 179.0.0.100 8081 | |
""" | |
from socket import * | |
import sys | |
import multiprocessing as mp | |
import time | |
bufsize = 4096 # Modify to suit your needs | |
targetHost = sys.argv[1] | |
targetPort = int(sys.argv[2]) | |
listenHost = sys.argv[3] | |
listenPort = int(sys.argv[4]) | |
def listener(lsock,rsock): | |
while True: | |
try: | |
data = lsock.recv(bufsize) | |
if data: | |
forward(rsock,data) | |
except: | |
continue | |
def forward(sock,data): | |
sock.sendall(data) | |
def listen(host, port): | |
listenSocket = socket(AF_INET, SOCK_STREAM) | |
listenSocket.connect((host, port)) | |
otherSocket = socket(AF_INET, SOCK_STREAM) | |
otherSocket.connect((targetHost, targetPort)) | |
workers = [] | |
workers.append(mp.Process(target=listener, args=(listenSocket,otherSocket))) | |
workers.append(mp.Process(target=listener, args=(otherSocket,listenSocket))) | |
for p in workers: | |
p.daemon = True | |
p.start() | |
while True: | |
try: | |
time.sleep(10) | |
except: | |
break | |
listen(listenHost,listenPort) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment