{"id":35,"date":"2024-01-30T23:21:25","date_gmt":"2024-01-30T23:21:25","guid":{"rendered":"https:\/\/shaynepatelcybersecurityportfolio.online\/?p=35"},"modified":"2024-01-31T21:51:19","modified_gmt":"2024-01-31T21:51:19","slug":"implementing-a-keylogger-using-python","status":"publish","type":"post","link":"https:\/\/shaynepatelcybersecurityportfolio.online\/?p=35","title":{"rendered":"Implementing a Keylogger using Python"},"content":{"rendered":"\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">This project utilizes Python to make a keylogger, a common tool used by many hackers to track keystrokes of a user. There are many ways to go about this, especially in Python but mine came out in only 10 lines as you see above. Very simple!<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">I started up my Kali VM box and opened my terminal.  Before getting started, I needed to install pynput which is a library that allows you to monitor input devices like a mouse and keyboard. Perfect for keylogging.<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"258\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/pynput-1024x258.png\" alt=\"\" class=\"wp-image-37\" style=\"width:738px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/pynput-1024x258.png 1024w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/pynput-300x76.png 300w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/pynput-768x193.png 768w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/pynput-1536x387.png 1536w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/pynput.png 1740w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Next, I used vim in the command line to create and edit a file for future use.  This was named &#8216;Keylogger.py&#8217; and is where I entered my keylogger script.<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"48\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/vim-keylogger.png\" alt=\"\" class=\"wp-image-38\"\/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">I will now go over a line-by-line explanation for better understanding. To finish, I will show what I entered when I ran the script and show the final output to confirm if this was a success.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Lines 1-2<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"454\" height=\"46\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/Line-1-2.png\" alt=\"\" class=\"wp-image-39\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/Line-1-2.png 454w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/Line-1-2-300x30.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">These first two lines are for importing required libraries. &#8216;Pynput&#8217; reads keystrokes as the user types and &#8216;logging&#8217; will log the keystrokes into a file. The &#8216;Key&#8217; class represents keys on the keyboard and the &#8216;Listener&#8217; class is used to listen and handle events related to inputs.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Line 4<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"44\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-4-1024x44.png\" alt=\"\" class=\"wp-image-40\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-4-1024x44.png 1024w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-4-300x13.png 300w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-4-768x33.png 768w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-4.png 1118w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Here, I created a basic configuration for the logging system where I specify where the keystrokes will be recorded. I chose &#8216;keylog.txt&#8217; for my location. The level is setting the log level to &#8216;DEBUG&#8217;, meaning any severity level with that or above will be recorded. The following specifies the format in which the keystrokes will be recorded. It will be seen as:<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Year-Month-Day Hour-Minute-Second(ms) &#8211; Key<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Line 6-7<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"310\" height=\"56\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/Line-6-7.png\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/Line-6-7.png 310w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/Line-6-7-300x54.png 300w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">These lines start with a function called &#8216;on_press&#8217; in which it takes an argument indicating the key pressed by the user and logs it into the file after converting it into a string.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Line 9-10<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"54\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-9-10.png\" alt=\"\" class=\"wp-image-42\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-9-10.png 522w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/line-9-10-300x31.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">This line creates a &#8216;Listener&#8217; object with a &#8216;on_press&#8217; function as a callback. The with statement ensures that resources are properly released when the program exits.  Line 10 starts the listener and will only stop when it&#8217;s told to.  This runs in the background, capturing the key pressed and invoking the &#8216;on_press&#8217; function to log them.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Running Keylogger.py script<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"438\" height=\"112\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/script-running.png\" alt=\"\" class=\"wp-image-44\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/script-running.png 438w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/script-running-300x77.png 300w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">I started running the &#8216;Keylogger.py&#8217; file I created in vim using python3 in the VM. While running, I entered a few lines of words like &#8220;Hello World&#8221; and &#8220;My favorite food is Taco&#8217;s&#8221;. I then stopped running the script because that needed to be done manually as mentioned earlier.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Keylog.txt output<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"896\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/keylog.txt-1024x896.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/keylog.txt-1024x896.png 1024w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/keylog.txt-300x263.png 300w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/keylog.txt-768x672.png 768w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/keylog.txt.png 1460w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">My last step was to open the keylog.txt file I created in the script where everything logs. As you can see, everything I entered was captured from letters to other keys. The format I entered in the logging configuration looks correct as well.  <\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">In conclusion, it was great to create a tool used in the cybersecurity world and gain confidence and experience with Python. Although it&#8217;s only ten lines, I had my challenges and looked into many sources for confirmation. Trial and error is the best learning experience!<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Thanks for reading along!<\/mark><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This project utilizes Python to make a keylogger, a common tool used by many hackers to track keystrokes of a user. There are many ways to go about this, especially in Python but mine came out in only 10 lines as you see above. Very simple! I started up my Kali VM box and opened [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":36,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":2,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/72"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/media\/36"}],"wp:attachment":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}