sunucu2.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python3
  2. from pyftpdlib.handlers import FTPHandler
  3. from pyftpdlib.servers import FTPServer
  4. from pyftpdlib.authorizers import DummyAuthorizer
  5. from pyftpdlib.log import logger
  6. import threading
  7. import os
  8. import time
  9. from random import randint
  10. #from dacid_dht import Dht
  11. import hashlib
  12. import subprocess
  13. from subprocess import check_output
  14. servisdir="./servisler"
  15. #HOST = "127.0.0.1"
  16. PORT = 2121
  17. dhtsunucu="10.45.2.129:1923"
  18. #dhtsunucu=None # 10.45.2.127:1923
  19. servis="/ftp-tarama/"
  20. def get_interface():
  21. for index, item in enumerate(os.listdir("/sys/class/net")):
  22. if "wg" in item:
  23. return item
  24. #f = os.popen('ifconfig '+get_interface()+' | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1')
  25. #HOST=f.read().strip('\n')
  26. HOST="192.168.100.3"
  27. # nmap ile port-tarama servisinin gerçeklenmesi
  28. def port_tarama(param,client_dir):
  29. """thread worker function"""
  30. updir=servisdir+servis+client_dir+"/"
  31. host=param.split(":")[0]
  32. port=param.split(":")[1]
  33. # portu tara- rapor oluştur
  34. subprocess.call(['python3','./ftpworm.py',"-H",host,"-p",port, "-o",updir+"raporT"])
  35. subprocess.call(['mv',updir+"raporT", updir+"rapor"])
  36. return
  37. # sunucunun kendini diğer düğümler tarafından bulunmasını sağlamak için DHT sunucusuna kayıt ettirmesi.
  38. def dht_kayit_islemi():
  39. while 1:
  40. bilgi=str(HOST)+":"+str(PORT)
  41. #sunucu dht ye kayıt edilecek.
  42. print ("sunucu dht ye kayıt edilecek.")
  43. #dht=Dht(bs=dhtsunucu)
  44. #if dht.sunucu_kayit(bilgi):
  45. # logger.info("Sunucu milisia-dht (%s) kaydı yapıldı.",(dht.sunucu))
  46. # 5dk da bir kendini dht suncusuna kayıt ettirecek.
  47. logger.info("Sunucu milisia-dht (%s) kaydı yapıldı.",(dhtsunucu))
  48. time.sleep(300)
  49. class MyHandler(FTPHandler):
  50. def istemci_hash(self,ip,port):
  51. ipport_str=str(ip)+"@"+str(port)
  52. hasho = hashlib.sha1()
  53. hasho.update(ipport_str.encode('utf-8'))
  54. ihash_id=hasho.hexdigest()
  55. return ihash_id
  56. def request_handle(self,file,client_id):
  57. print ("dosya:",file)
  58. istek=open(file,"r").read()
  59. t = threading.Thread(target=port_tarama, args=(istek,client_id,))
  60. t.start()
  61. def on_connect(self):
  62. print ("%s:%s connected" % (self.remote_ip, self.remote_port))
  63. #self.banner(str(randint(100, 1999)))
  64. def on_disconnect(self):
  65. # do something when client disconnects
  66. id=self.istemci_hash(self.remote_ip, self.remote_port)
  67. subprocess.call(['rm','-rf', "./servisler"+servis+id])
  68. pass
  69. def on_login(self, username):
  70. print ("%s:%s logined" % (self.remote_ip, self.remote_port))
  71. id=self.istemci_hash(self.remote_ip, self.remote_port)
  72. print (">>>>",id,"bağlantı dizini oluşturuldu")
  73. self.push(id+"\n")
  74. subprocess.call(['mkdir',"-p","./servisler"+servis+id])
  75. pass
  76. def on_logout(self, username):
  77. # do something when user logs out
  78. pass
  79. def on_file_sent(self, file):
  80. # do something when a file has been sent
  81. pass
  82. def on_file_received(self, file):
  83. # do something when a file has been received
  84. print ("%s:%s dosya_gonderdi" % (self.remote_ip, self.remote_port))
  85. client_id=self.istemci_hash(self.remote_ip, self.remote_port)
  86. self.request_handle(file,client_id)
  87. pass
  88. def on_incomplete_file_sent(self, file):
  89. # do something when a file is partially sent
  90. pass
  91. def on_incomplete_file_received(self, file):
  92. # remove partially uploaded files
  93. import os
  94. os.remove(file)
  95. def main():
  96. logger.info("sistem açıldı")
  97. # dht sunucu kayıt ettirme
  98. dhtkayit = threading.Thread(name='dht_kayit_islemi', target=dht_kayit_islemi)
  99. dhtkayit.setDaemon(True)
  100. dhtkayit.start()
  101. # ftp sunucu servis verme
  102. authorizer = DummyAuthorizer()
  103. #authorizer.add_user('user', '12345', homedir='.', perm='elradfmwMT')
  104. authorizer.add_anonymous(homedir='./servisler',perm='elradfmwMT')
  105. handler = MyHandler
  106. handler.authorizer = authorizer
  107. server = FTPServer((HOST,PORT), handler)
  108. server.serve_forever()
  109. if __name__ == "__main__":
  110. main()