Advertisement
Guest User

Untitled

a guest
Aug 16th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import sys,socket,getopt,threading,subprocess
  2. l=False;c=False;u=False;e="";t="";d="";p=0
  3. def RC(C):
  4.  C=C.rstrip()
  5.  try:O=subprocess.check_output(C,stderr=subprocess.STDOUT,shell=True)
  6.  except:O="exe fail\n"
  7.  return O
  8. def CH(CS):
  9.  global u,e,c
  10.  if len(d):
  11.   fb=""
  12.   while True:
  13.    da=CS.recv(1024)
  14.    if not da:
  15.     break
  16.    else:
  17.     fb+=da
  18.   try:
  19.    file_descriptor=open(d, "wb")
  20.    file_descriptor.write(fb)
  21.    file_descriptor.close()
  22.    CS.send("Pass %s\n" % d)
  23.   except:
  24.    CS.send("Fail %s\n" % d)
  25.  if len(e):
  26.   op=RC(e)
  27.   CS.send(op)
  28.  if c:
  29.   while True:
  30.    CS.send(": ")
  31.    cb=""
  32.    while "\n" not in cb:
  33.     cb += CS.recv(1024)
  34.    r=RC(cb)
  35.    CS.send(r)
  36. def SL():
  37.  global t,p
  38.  if not len(t):
  39.   t="0.0.0.0"
  40.  S=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  41.  S.bind((t, p))
  42.  S.listen(5)
  43.  while True:
  44.   client_socket, addr=S.accept()
  45.   client_thread=threading.Thread(target=CH, args=(client_socket,))
  46.   client_thread.start()
  47. def sn(bu):
  48.  cl=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  49.  try:
  50.   cl.connect((t,p))
  51.   if len(bu):
  52.    cl.send(bu)
  53.   while True:
  54.    recv_len=1
  55.    re=""
  56.    while recv_len:
  57.     da=cl.recv(4096)
  58.     recv_len=len(da)
  59.     re+=da
  60.     if recv_len<4096:
  61.      break
  62.    print re,
  63.    bu=raw_input("")
  64.    bu+="\n"
  65.    cl.send(bu)
  66.  except:
  67.   print "Excpt"
  68.   cl.close()
  69. def use():
  70.  print "Use: nc.py -t target_host -p port"
  71.  print "-l listen"
  72.  print "-e exec=file"
  73.  print "-c command"
  74.  print "-u upload=path"
  75.  sys.exit(0)
  76. def main():
  77.  global l,p,e,c,d,t
  78.  if not len(sys.argv[1:]):
  79.   use()
  80.  try:
  81.   opts, args=getopt.getopt(sys.argv[1:], "hle:t:p:cu:")
  82.  except getopt.GetoptError as err:
  83.   print str(err)
  84.   use()
  85.  for o, a in opts:
  86.   if o in ("-h"):use()
  87.   elif o in ("-l"):l=True
  88.   elif o in ("-e"):e=a
  89.   elif o in ("-c"):c=True
  90.   elif o in ("-u"):d=a
  91.   elif o in ("-t"):t=a
  92.   elif o in ("-p"):p=int(a)
  93.   else: assert False, "bad option"
  94.  if not l and len(t) and p > 0:
  95.   bu=sys.stdin.read()
  96.   sn(bu)
  97.  if l:
  98.   SL()
  99. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement