Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import subprocess
  4. import sys
  5. import os
  6. import threading
  7. import random
  8.  
  9. def stdinreader():
  10. while True:
  11. try:
  12. l = sys.stdin.readline()
  13. except KeyboardInterrupt:
  14. break
  15. if not l:
  16. break
  17. yield l
  18.  
  19. class Proxy:
  20. def __init__(self, blacklist, phrases, args):
  21. self.blacklist = blacklist
  22. self.phrases = phrases
  23. try:
  24. self.chld = subprocess.Popen(args, stdin=subprocess.PIPE)
  25. except OSError:
  26. print "Error starting the haas process."
  27. sys.exit(1)
  28.  
  29. def is_allowed(self, l):
  30. return reduce(lambda x,y: x and y, [x not in l for x in self.blacklist], True)
  31.  
  32. def listen(self):
  33. for line in stdinreader():
  34. if self.chld.poll() is None:
  35. if self.is_allowed(line):
  36. self.chld.communicate(line)
  37. else:
  38. print random.choice(self.phrases)
  39. break
  40. else:
  41. return
  42. self.chld.terminate()
  43.  
  44. def run(self):
  45. t = threading.Thread(target=self.listen)
  46. t.daemon = True
  47. t.start()
  48. self.chld.wait()
  49.  
  50. if __name__ == '__main__':
  51. bl = ['shell','unix','system','cd','argv']
  52. phrases = ['Nice try...', 'Nope.', 'What are you trying to do?!?', "Sorry, that's not implemented!"]
  53. ex = os.path.join(os.path.dirname(os.path.realpath(__file__)), "haas")
  54. if len(sys.argv) == 2:
  55. ex = sys.argv[1]
  56. proxy = Proxy(bl, phrases, [ex])
  57. proxy.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement