RE: usable netcat in 99 lines of python 08-22-2015, 09:41 PM
#11
(08-22-2015, 08:53 PM)lola Wrote: Also don't ask me to share some of my code. I'm rewriting your atrocity, I'll post it when I'm done.
For someone who is bitching about dirtied up code and wanting to clean it, you sure are slow. There's no point in rewriting it, just making it cleaner will suffice.
Code:
import sys
import socket
import getopt
import threading
import subprocess
l = False
c = False
u = False
e = ""
t = ""
d = ""
p = 0
def RC(C):
C = C.rstrip()
try:
O = subprocess.check_output(C, stderr=subprocess.STDOUT, shell=True)
except:
O = "exe fail\n"
return O
def CH(CS):
global u,e,c
if len(d):
fb = ""
while True:
da = CS.recv(1024)
if not da:
break
else:
fb += da
try:
file_descriptor = open(d, "wb")
file_descriptor.write(fb)
filedescriptor.close()
CS.send("Pass %s\n" % d)
except:
CS.send("Fail %s\n" % d)
if len(e):
op=RC(e)
CS.send(op)
if c:
while True:
CS.send(": ")
cb = ""
while "\n" not in cb:
cb += CS.recv(1024)
r = RC(cb)
CS.send(r)
def SL():
global t,p
if not len(t):
t = "0.0.0.0"
S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
S.bind((t, p))
S.listen(5)
while True:
client_socket, addr = S.accept()
client_thread = threading.Thread(target=CH, args=(client_socket,))
client_thread.start()
def sn(bu):
cl = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
cl.connect((t,p))
if len(bu):
cl.send(bu)
while True:
recv_len=1
re = ""
while recv_len:
da = cl.recv(4096)
recv_len = len(da)
re += da
if recv_len < 4096:
break
print re,
bu=raw_input("")
bu+="\n"
cl.send(bu)
except:
print "Excpt"
cl.close()
def use():
print "Use: nc.py -t target_host -p port"
print "-l listen"
print "-e exec=file"
print "-c command"
print "-u upload=path"
sys.exit(0)
def main():
global l,p,e,c,d,t
if not len(sys.argv[1:]):
use()
try:
opts, args=getopt.getopt(sys.argv[1:], "hle:t:p:cu:")
except getopt.GetoptError as err:
print str(err)
use()
for o, a in opts:
if o in ("-h"):
use()
elif o in ("-l"):
l = True
elif o in ("-e"):
e = a
elif o in ("-c"):
c = True
elif o in ("-u"):
d = a
elif o in ("-t"):
t = a
elif o in ("-p"):
p = int(a)
else:
assert False, "bad option"
if not l and len(t) and p > 0:
bu = sys.stdin.read()
sn(bu)
if l:
SL()
main()![[Image: BXqGARG.png]](https://i.imgur.com/BXqGARG.png)










![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
