Advertisement
Guest User

ftp-honeypot.py

a guest
Dec 22nd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import socket
  4. from thread import start_new_thread
  5. from datetime import datetime
  6.  
  7. users = {}
  8. HOST = ''
  9. PORT = 42069
  10. log_file = open('log.log', 'a+')
  11. def init_user_conf():
  12. f = open('users.conf', 'r')
  13. user_conf_lines = f.read().split('\n')
  14. for user_conf_line in user_conf_lines:
  15. split_line = user_conf_line.split(':')
  16. if len(split_line) >= 2:
  17. username = split_line[0]
  18. password = split_line[1]
  19. users[username] = password
  20.  
  21.  
  22. def log_message(msg):
  23. print msg
  24. log_file.write(msg + '\n')
  25.  
  26.  
  27. def init_server_conf():
  28. f = open('server.conf', 'r')
  29. server_conf_lines = f.read().split('\n')
  30. for server_conf_line in server_conf_lines:
  31. split_line = server_conf_line.split('=')
  32. if len(split_line) >= 2:
  33. conf_variable = split_line[0]
  34. conf_variable_value = split_line[1]
  35. if conf_variable == 'PORT':
  36. PORT = int(conf_variable_value)
  37. elif conf_variable == 'HOST':
  38. if conf_variable_value == 'DEFAULT':
  39. pass
  40. else:
  41. HOST = str(conf_variable_value)
  42.  
  43. def clientThread(conn, connip):
  44. currentDir = "/"
  45. isLoggedIn = False
  46. isRecivingPassword = False
  47. user_to_login = ""
  48. log_msg = ""
  49. while True:
  50. conn_data = conn.recv(1024)
  51. if isLoggedIn == False and conn_data.startswith('USER'):
  52. user_to_login = conn_data[5:]
  53. conn.sendall('331 Please specify the password.\n')
  54. isRecivingPassword = True
  55. elif isLoggedIn == True:
  56. if conn_data.startswith('pwd'):
  57. conn.sendall("257 " + '"' + currentDir + '"' + " is the current directory" + '\n')
  58. elif isRecivingPassword == True:
  59. #print conn_data[5:]
  60. if conn_data.startswith('PASS'):
  61. user_to_login = user_to_login.replace('\n', '').replace('\r', '')
  62. password = conn_data[5:].replace('\n', '').replace('\r', '')
  63. if user_to_login in users.keys() and not(user_to_login == '*'):
  64. if users[user_to_login] == password:
  65. conn.sendall('230 Login successful.\n')
  66. log_msg = 'Login from IP: ' + connip + ' with username:' + user_to_login + " and password:" + password + " SUCCESSFUL."
  67. elif users[user_to_login] == '*':
  68. conn.sendall('230 Login successful.\n')
  69. log_msg = 'Login from IP: ' + connip + ' with username:' + user_to_login + " and password:" + password + " SUCCESSFUL."
  70. else:
  71. conn.sendall('530 Incorrect Login.\n')
  72. log_msg = 'Login from IP: ' + connip + ' with username:' + user_to_login + ' and password:' + password + ' FAILED.'
  73. elif '*' in users.keys():
  74. if users['*'] == password:
  75. conn.sendall('230 Login successful.\n')
  76. log_msg = 'Login from IP: ' + connip + ' with username:' + user_to_login + " and password:" + password + " SUCCESSFUL."
  77. else:
  78. conn.sendall('530 Incorrect Login.\n')
  79. log_msg = 'Login from IP: ' + connip + ' with username:' + user_to_login + ' and password:' + password + ' FAILED.'
  80. else:
  81. log_msg = 'Login from IP: ' + connip + ' with username:' + user_to_login + ' and password:' + password + ' FAILED.'
  82. conn.sendall('530 Incorrect Login.\n')
  83.  
  84. if not(log_msg == ''):
  85. log_message(log_msg)
  86. log_msg = ''
  87. isRecivingPassword = False
  88.  
  89.  
  90.  
  91. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  92.  
  93.  
  94.  
  95. def init_ftp_server():
  96. s.bind((HOST, PORT))
  97.  
  98. s.listen(50)
  99. print "FTP Honeypot running."
  100. while 1:
  101. conn, addr = s.accept()
  102. print "client logged in from IP:" + str(addr[0]) + ":" + str(addr[1])
  103. conn.sendall("220 (vsFTPd 3.0.3)\n")
  104. start_new_thread(clientThread, (conn, str(addr[0]),))
  105.  
  106.  
  107.  
  108. def getDateTime():
  109. now = datetime.now()
  110. currentDateTime = str(now.day) + "/" + str(now.month) + "/" + str(now.year)
  111. return currentDateTime
  112.  
  113.  
  114. if __name__ == '__main__':
  115. """
  116. log_file.write('Starting logging, Date (DD/MM/YY): ' + getDateTime() + "\n")
  117. print "configuring server settings..."
  118. try:
  119. init_server_conf()
  120. except Exception as e:
  121. print "FAILED: " + str(e)
  122. print "configuring FTP users..."
  123. try:
  124. init_user_conf()
  125. except Exception as e:
  126. print "FAILED: " + str(e)
  127. #print str(users)
  128. """
  129. users = {'admin': 'admin123'}
  130. REAL_HOST = HOST
  131. if REAL_HOST == '':
  132. REAL_HOST = '*'
  133. print "Starting FTP Honeypot on: " + REAL_HOST + ":" + str(PORT) + "..."
  134. try:
  135. init_ftp_server()
  136. except Exception as e:
  137. print "FAILED: " + str(e)
  138. # print users['admin']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement