Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. from twisted.conch import avatar, recvline
  2. from twisted.conch.interfaces import IConchUser, ISession
  3. from twisted.conch.ssh import factory, keys, session
  4. from twisted.conch.insults import insults
  5. from twisted.cred import portal, checkers
  6. from twisted.internet import reactor
  7. from zope.interface import implements
  8.  
  9. import subprocess
  10. import sys
  11.  
  12. class SysUser():
  13. def __init__(self):
  14. self.tunnel_route = []
  15.  
  16. self.aps = []
  17. self.aps.append(ProEntity(0,b'100.20.20.8',8009,'root','thisispass'))
  18. self.aps.append(ProEntity(1,b'55.20.10.8',1080,'mama','123456'))
  19. self.aps.append(ProEntity(2,b'22.22.22.22',5665,'toko','lydsas232'))
  20.  
  21. self.id_val = 10
  22.  
  23. class ProEntity():
  24. def __init__(self,pid,ip_address,port,user,password):
  25. self.pid = pid
  26. self.ip_address = ip_address
  27. self.port = port
  28. self.user = user
  29. self.password = password
  30.  
  31. def __repr__(self):
  32. #return(b'ID:'+self.pid +b'| IP:'+self.ip_address + b'| PORT:'+self.port)
  33. return('ID:%d| IP:%s| PORT:%d|USER:%s |PASS:%s\n' %
  34. (self.pid,self.ip_address,self.port,self.user,self.password))
  35.  
  36. class SSHDemoProtocol(recvline.HistoricRecvLine):
  37. def __init__(self, user):
  38. self.user = user
  39. self.data = SysUser()
  40.  
  41. clist = {
  42. b'add': b'Add a new proxy to database. format - add [ip_address]:[port]:[user]:[pass]',
  43. b'remove': b'Removes a proxy from database. format - remove [proxy_id] ',
  44. b'ctunnel': b'Specifies the current listed. format - ctunnel',
  45. b'tunnel': b'Allows to set the tunnel to a new route. use - tunnel [proxy_id]:...:[proxy_id]',
  46. b'list': b'Lists the existing proxy database. format - list',
  47. b'help': b'lists all commands. format - help',
  48. None: b" is an unknown command. type 'help' to get command list"
  49. }
  50.  
  51. def connectionMade(self):
  52. global all_users
  53.  
  54. all_users[self.user.username] = SysUser()
  55. recvline.HistoricRecvLine.connectionMade(self)
  56. self.terminal.write(b'Hey %s. This is setup interface for the proxy. type help to view available commands\n' % (self.user.username))
  57. self.terminal.nextLine()
  58.  
  59. def lineReceived(self, line):
  60. global all_users
  61.  
  62. user_data = all_users[self.user.username]
  63. command = line.split(b' ', 1)[0]
  64. if command in self.clist:
  65. #try:
  66. if command == b'add':
  67. self.add_p(line)
  68. if command == b'remove':
  69. self.remove(line)
  70. if command == b'ctunnel':
  71. self.ctunnel(line)
  72. if command == b'tunnel':
  73. self.tunnel(line)
  74. if command == b'list':
  75. self.list(line)
  76. if command == b'help':
  77. self.help(line)
  78. #except:
  79. # self.terminal.write(b"An error has accourd during the handling of your command. try again")
  80. # self.terminal.nextLine()
  81. elif command == b'':
  82. pass
  83. else:
  84. self.terminal.write(command + self.clist[None])
  85. self.terminal.nextLine()
  86.  
  87.  
  88. def add_p(self,line):
  89. try:
  90. indata = line.split(b' ', 1)[1]
  91. ip_address = indata.split(b':')[0]
  92. port = int(indata.split(b':')[1])
  93. user = indata.split(b':')[2]
  94. password = indata.split(b':')[3]
  95. except:
  96. self.terminal.write(b"command was incorrect - either port/ip/user/pass were wrong, or format was wrong")
  97. self.terminal.nextLine()
  98. return()
  99.  
  100. self.data.aps.append(ProEntity(self.data.id_val,ip_address,port,user,password))
  101. self.terminal.write(b"proxy was added with id %d" % (self.data.id_val))
  102. self.terminal.nextLine()
  103. self.data.id_val+=1
  104.  
  105. def remove(self,line):
  106. indata = line.split(b' ', 1)
  107. if len(indata) != 2:
  108. self.terminal.write(b"An incorrect amount of arguments has been specified")
  109. self.terminal.nextLine()
  110. return()
  111. try:
  112. pro_id = int(indata[1])
  113. except:
  114. self.terminal.write(indata[1] + b" isn't a recognized proxy server id in the system. try 'list'.")
  115. self.terminal.nextLine()
  116. return()
  117.  
  118. for pro in self.data.aps:
  119. if pro.pid == pro_id:
  120. self.data.aps.remove(pro)
  121. return()
  122.  
  123. self.terminal.write(indata[1] + b" isn't a recognized proxy server id in the system. try 'list'.")
  124. self.terminal.nextLine()
  125. return()
  126.  
  127.  
  128. def ctunnel(self,line):
  129. self.terminal.write(str.encode(str(self.data.tunnel_route)))
  130. self.terminal.nextLine()
  131.  
  132. def tunnel(self,line):
  133. self.data.tunnel_route = []
  134.  
  135. id_arr = line.split(b' ', 1)
  136. if len(id_arr) != 2:
  137. self.terminal.write(b"An incorrect amount of arguments has been specified")
  138. self.terminal.nextLine()
  139. return()
  140. id_arr = id_arr[1].split(b':')
  141.  
  142. for pro_id in id_arr:
  143. pro_id = int(pro_id)
  144. for pro in self.data.aps:
  145. if pro.pid == pro_id:
  146. self.data.tunnel_route.append(pro)
  147.  
  148. if len(id_arr)>len(self.data.tunnel_route):
  149. self.terminal.write(b"An unknown proxy ID has been specified")
  150. self.terminal.nextLine()
  151. self.data.tunnel_route = []
  152. return()
  153.  
  154. self.terminal.write(b"Tunnel is being created")
  155. self.terminal.nextLine()
  156.  
  157. self.do_connections(self.data.tunnel_route)
  158.  
  159. self.terminal.write(b"Tunnel is done! proxy in to %s to use the tunnel" %(self.data.tunnel_route[0].ip_address))
  160. self.terminal.nextLine()
  161.  
  162. def do_connections(self,tunnel_route):
  163.  
  164. HOST="www.example.org"
  165. # Ports are handled in ~/.ssh/config since we use OpenSSH
  166. COMMAND="uname -a"
  167.  
  168. ssh = subprocess.Popen(["ssh", "admin@reyysport.com", "-D 55557"],
  169. shell=False,
  170. stdout=subprocess.PIPE,
  171. stderr=subprocess.PIPE)
  172. result = ssh.stdout.readlines()
  173. if result == []:
  174. error = ssh.stderr.readlines()
  175. print >>sys.stderr, "ERROR: %s" % error
  176. else:
  177. print result
  178. #ssh -v -2 -C -D 55557 -L 55556:127.0.0.1:55556 -L 55555:127.0.0.1:55555 admin@reyysport.com
  179. #subprocess.call('ssh user1@middle.example.org -L 8001:localhost:8002 -t ssh -D 8002 user2@server.example.org')
  180. return()
  181.  
  182. def list(self,line):
  183. self.terminal.write(str.encode(str(self.data.aps)))
  184. self.terminal.nextLine()
  185.  
  186.  
  187. def help(self,line):
  188. for command in self.clist:
  189. if command is not None:
  190. self.terminal.write(command + b' : ' + self.clist[command])
  191. self.terminal.nextLine()
  192.  
  193. class SSHDemoAvatar(avatar.ConchUser):
  194. implements(ISession)
  195.  
  196.  
  197. def __init__(self, username):
  198. avatar.ConchUser.__init__(self)
  199. self.username = username
  200. self.channelLookup.update({'session': session.SSHSession})
  201.  
  202.  
  203. def openShell(self, protocol):
  204. serverProtocol = insults.ServerProtocol(SSHDemoProtocol, self)
  205. serverProtocol.makeConnection(protocol)
  206. protocol.makeConnection(session.wrapProtocol(serverProtocol))
  207.  
  208.  
  209. def getPty(self, terminal, windowSize, attrs):
  210. return None
  211.  
  212.  
  213. def execCommand(self, protocol, cmd):
  214. raise NotImplementedError()
  215.  
  216.  
  217. def closed(self):
  218. pass
  219.  
  220. def __repr__(self):
  221. return(self.username)
  222.  
  223. class SSHDemoRealm(object):
  224. implements(portal.IRealm)
  225.  
  226. def requestAvatar(self, avatarId, mind, *interfaces):
  227. if IConchUser in interfaces:
  228. return interfaces[0], SSHDemoAvatar(avatarId), lambda: None
  229. else:
  230. raise NotImplementedError("No supported interfaces found.")
  231.  
  232.  
  233. def getRSAKeys():
  234. with open('/home/idan/.ssh/id_rsa') as privateBlobFile:
  235. privateBlob = privateBlobFile.read()
  236. privateKey = keys.Key.fromString(data=privateBlob)
  237.  
  238.  
  239. with open('/home/idan/.ssh/id_rsa.pub') as publicBlobFile:
  240. publicBlob = publicBlobFile.read()
  241. publicKey = keys.Key.fromString(data=publicBlob)
  242.  
  243. return publicKey, privateKey
  244.  
  245. class ProxFactory(factory.SSHFactory):
  246. portal = portal.Portal(SSHDemoRealm())
  247.  
  248. if __name__ == "__main__":
  249. sshFactory = ProxFactory()
  250.  
  251. all_users = {}
  252. users = {'admin': 'aaa', 'guest': 'bbb'}
  253. sshFactory.portal.registerChecker(
  254. checkers.InMemoryUsernamePasswordDatabaseDontUse(**users))
  255. pubKey, privKey = getRSAKeys()
  256. sshFactory.publicKeys = {'ssh-rsa': pubKey}
  257. sshFactory.privateKeys = {'ssh-rsa': privKey}
  258. reactor.listenTCP(22222, sshFactory)
  259. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement