-
Notifications
You must be signed in to change notification settings - Fork 0
/
DuckyScriptTester.ps1
135 lines (120 loc) · 3.41 KB
/
DuckyScriptTester.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
##################################################
# DuckyScriptTester
##################################################
# Author: Cribbit
# Version: 1.0
# Tested on: Windows 10 21H1 (Powershell 5.1)
# Notes: This is a very basic tester it does not support all of the ducky script language
######################Config######################
# Path to ducky script (Not the encoded bin file)
$path=".\ds.txt"
##################################################
[system.reflection.assembly]::Loadwithpartialname("system.windows.forms")
$wshell = New-Object -ComObject wscript.shell
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class KBd {
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);}
"@
function Hold-Key {
param (
$raw
)
[KBd]::keybd_event($raw, 0x45, 0, 0);
}
function Release-Key {
param (
$raw
)
[KBd]::keybd_event($raw, 0x45, 0x2, 0);
}
#https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=windowsdesktop-6.0
function Convert-toFormsKey {
param (
[string]$raw
)
switch ( $raw )
{
{$PSItem -match '(WINDOWS|GUI)'}
{
Write-Output ([int]([System.Windows.Forms.Keys]::LWin))
continue
}
{$PSItem -match '(CONTROL|CTRL)'}
{
Write-Output ([int]([System.Windows.Forms.Keys]::LControlKey))
continue
}
SHIFT
{
Write-Output ([int]([System.Windows.Forms.Keys]::LShiftKey))
continue
}
ALT
{
Write-Output ([int]([System.Windows.Forms.Keys]::Menu))
continue
}
default
{
Write-Output ([int]([System.Windows.Forms.Keys]::None))
}
}
}
function Convert-toShellKey {
param (
[string]$raw
)
#https://ss64.com/vb/sendkeys.html
$raw=($raw -replace "ESC","ESCAPE" -replace "SPACE", " " -replace "PRINTSCREEN", "PRTSC" -replace "PAGEUP", "PGUP" -replace "PAGEDOWN", "PGDN" -replace "ARROW", "")
Write-Output "{$raw}"
}
Clear
sleep -Milliseconds 500
gc $path | % {
$line=$_
$pos = $_.IndexOf(" ")
$command = if ($pos -gt 0) {$line.Substring(0, $pos)} Else {$line}
$params = $line.Substring($pos+1)
switch ( $command )
{
DELAY
{
if ($pos -gt 0) {
sleep -Milliseconds $params
}
continue
}
STRING
{
$params = ($params -replace "{","OPENBRACKET" -replace "}","{}}" -replace "OPENBRACKET", "{{}" -replace "~","{~}" -replace "!","{!}" -replace "\^","{\^}" -replace "\+","{\+}" -replace "%","{%}")
$wshell.SendKeys($params)
continue
}
{$PSItem -match '^(WINDOWS|GUI|CONTROL|CTRL|ALT|SHIFT).*'}
{
$stringArray =$command.Split("-")
$stringArray | % { Hold-Key (Convert-toFormsKey $_) }
if ($pos -gt 0)
{
if ($params.length -gt 1)
{
$wshell.SendKeys((Convert-toShellKey $params))
}
else
{
$wshell.SendKeys($params)
}
}
$stringArray | % { Release-Key (Convert-toFormsKey $_) }
continue
}
{$PSItem -notmatch 'REM' -and $pos -lt 0}
{
$wshell.SendKeys((Convert-toShellKey $command))
continue
}
}
}