-
Notifications
You must be signed in to change notification settings - Fork 93
/
droidducky.sh
411 lines (357 loc) · 10.8 KB
/
droidducky.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/bash
# DroidDucky
# Simple Duckyscript interpreter in Bash. Based on android-keyboard-gadget and hid-gadget-test utility.
#
# Usage: droidducky.sh payload_file.dd
#
# Copyright (C) 2015 - Andrej Budinčević <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
defdelay=0
kb="/dev/hidg0 keyboard"
last_cmd=""
last_string=""
line_num=0
function convert()
{
local kbcode=""
if [ "$1" == " " ]
then
kbcode='space'
elif [ "$1" == "!" ]
then
kbcode='left-shift 1'
elif [ "$1" == "." ]
then
kbcode='period'
elif [ "$1" == "\`" ]
then
kbcode='backquote'
elif [ "$1" == "~" ]
then
kbcode='left-shift tilde'
elif [ "$1" == "+" ]
then
kbcode='kp-plus'
elif [ "$1" == "=" ]
then
kbcode='equal'
elif [ "$1" == "_" ]
then
kbcode='left-shift minus'
elif [ "$1" == "-" ]
then
kbcode='minus'
elif [ "$1" == "\"" ]
then
kbcode='left-shift quote'
elif [ "$1" == "'" ]
then
kbcode='quote'
elif [ "$1" == ":" ]
then
kbcode='left-shift semicolon'
elif [ "$1" == ";" ]
then
kbcode='semicolon'
elif [ "$1" == "<" ]
then
kbcode='left-shift comma'
elif [ "$1" == "," ]
then
kbcode='comma'
elif [ "$1" == ">" ]
then
kbcode='left-shift period'
elif [ "$1" == "?" ]
then
kbcode='left-shift slash'
elif [ "$1" == "\\" ]
then
kbcode='backslash'
elif [ "$1" == "|" ]
then
kbcode='left-shift backslash'
elif [ "$1" == "/" ]
then
kbcode='slash'
elif [ "$1" == "{" ]
then
kbcode='left-shift lbracket'
elif [ "$1" == "}" ]
then
kbcode='left-shift rbracket'
elif [ "$1" == "(" ]
then
kbcode='left-shift 9'
elif [ "$1" == ")" ]
then
kbcode='left-shift 0'
elif [ "$1" == "[" ]
then
kbcode='lbracket'
elif [ "$1" == "]" ]
then
kbcode='rbracket'
elif [ "$1" == "#" ]
then
kbcode='left-shift 3'
elif [ "$1" == "@" ]
then
kbcode='left-shift 2'
elif [ "$1" == "$" ]
then
kbcode='left-shift 4'
elif [ "$1" == "%" ]
then
kbcode='left-shift 5'
elif [ "$1" == "^" ]
then
kbcode='left-shift 6'
elif [ "$1" == "&" ]
then
kbcode='left-shift 7'
elif [ "$1" == "*" ]
then
kbcode='kp-multiply'
else
case $1 in
[[:upper:]])
tmp=$1
kbcode="left-shift ${tmp,,}"
;;
*)
kbcode="$1"
;;
esac
fi
echo "$kbcode"
}
while IFS='' read -r line || [[ -n "$line" ]]; do
((line_num++))
read -r cmd info <<< "$line"
if [ "$cmd" == "STRING" ]
then
last_string="$info"
last_cmd="$cmd"
for (( i=0; i<${#info}; i++ )); do
kbcode=$(convert "${info:$i:1}")
if [ "$kbcode" != "" ]
then
echo "$kbcode" | ./hid-gadget-test $kb > /dev/null
fi
done
elif [ "$cmd" == "ENTER" ]
then
last_cmd="enter"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "DELAY" ]
then
last_cmd="UNS"
((info = info*1000))
usleep $info
elif [ "$cmd" == "WINDOWS" -o "$cmd" == "GUI" ]
then
last_cmd="left-meta ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "MENU" -o "$cmd" == "APP" ]
then
last_cmd="menu"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "DOWNARROW" -o "$cmd" == "DOWN" ]
then
last_cmd="down"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "LEFTARROW" -o "$cmd" == "LEFT" ]
then
last_cmd="left"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "RIGHTARROW" -o "$cmd" == "RIGHT" ]
then
last_cmd="right"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "UPARROW" -o "$cmd" == "UP" ]
then
last_cmd="up"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "DEFAULT_DELAY" -o "$cmd" == "DEFAULTDELAY" ]
then
last_cmd="UNS"
((defdelay = info*1000))
elif [ "$cmd" == "BREAK" -o "$cmd" == "PAUSE" ]
then
last_cmd="pause"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "ESC" -o "$cmd" == "ESCAPE" ]
then
last_cmd="escape"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "PRINTSCREEN" ]
then
last_cmd="print"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "CAPSLOCK" -o "$cmd" == "DELETE" -o "$cmd" == "END" -o "$cmd" == "HOME" -o "$cmd" == "INSERT" -o "$cmd" == "NUMLOCK" -o "$cmd" == "PAGEUP" -o "$cmd" == "PAGEDOWN" -o "$cmd" == "SCROLLLOCK" -o "$cmd" == "SPACE" -o "$cmd" == "TAB" \
-o "$cmd" == "F1" -o "$cmd" == "F2" -o "$cmd" == "F3" -o "$cmd" == "F4" -o "$cmd" == "F5" -o "$cmd" == "F6" -o "$cmd" == "F7" -o "$cmd" == "F8" -o "$cmd" == "F9" -o "$cmd" == "F10" -o "$cmd" == "F11" -o "$cmd" == "F12" ]
then
last_cmd="${cmd,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "REM" ]
then
echo "$info"
elif [ "$cmd" == "SHIFT" ]
then
if [ "$info" == "DELETE" -o "$info" == "END" -o "$info" == "HOME" -o "$info" == "INSERT" -o "$info" == "PAGEUP" -o "$info" == "PAGEDOWN" -o "$info" == "SPACE" -o "$info" == "TAB" ]
then
last_cmd="left-shift ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == *"WINDOWS"* -o "$info" == *"GUI"* ]
then
read -r gui char <<< "$info"
last_cmd="left-shift left-meta ${char,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "DOWNARROW" -o "$info" == "DOWN" ]
then
last_cmd="left-shift down"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "LEFTARROW" -o "$info" == "LEFT" ]
then
last_cmd="left-shift left"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "RIGHTARROW" -o "$info" == "RIGHT" ]
then
last_cmd="left-shift right"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "UPARROW" -o "$info" == "UP" ]
then
last_cmd="left-shift up"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
else
echo "($line_num) Parse error: Disallowed $cmd $info"
fi
elif [ "$cmd" == "CONTROL" -o "$cmd" == "CTRL" ]
then
if [ "$info" == "BREAK" -o "$info" == "PAUSE" ]
then
last_cmd="left-ctrl pause"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "F1" -o "$info" == "F2" -o "$info" == "F3" -o "$info" == "F4" -o "$info" == "F5" -o "$info" == "F6" -o "$info" == "F7" -o "$info" == "F8" -o "$info" == "F9" -o "$info" == "F10" -o "$info" == "F11" -o "$info" == "F12" ]
then
last_cmd="left-ctrl ${cmd,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "ESC" -o "$info" == "ESCAPE" ]
then
last_cmd="left-ctrl escape"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "" ]
then
last_cmd="left-ctrl"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
else
last_cmd="left-ctrl ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
fi
elif [ "$cmd" == "ALT" ]
then
if [ "$info" == "END" -o "$info" == "SPACE" -o "$info" == "TAB" \
-o "$info" == "F1" -o "$info" == "F2" -o "$info" == "F3" -o "$info" == "F4" -o "$info" == "F5" -o "$info" == "F6" -o "$info" == "F7" -o "$info" == "F8" -o "$info" == "F9" -o "$info" == "F10" -o "$info" == "F11" -o "$info" == "F12" ]
then
last_cmd="left-alt ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "ESC" -o "$info" == "ESCAPE" ]
then
last_cmd="left-alt escape"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "" ]
then
last_cmd="left-alt"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
else
last_cmd="left-alt ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
fi
elif [ "$cmd" == "ALT-SHIFT" ]
then
last_cmd="left-shift left-alt"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$cmd" == "CTRL-ALT" ]
then
if [ "$info" == "BREAK" -o "$info" == "PAUSE" ]
then
last_cmd="left-ctrl left-alt pause"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "END" -o "$info" == "SPACE" -o "$info" == "TAB" -o "$info" == "DELETE" -o "$info" == "F1" -o "$info" == "F2" -o "$info" == "F3" -o "$info" == "F4" -o "$info" == "F5" -o "$info" == "F6" -o "$info" == "F7" -o "$info" == "F8" -o "$info" == "F9" -o "$info" == "F10" -o "$info" == "F11" -o "$info" == "F12" ]
then
last_cmd="left-ctrl left-alt ${cmd,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "ESC" -o "$info" == "ESCAPE" ]
then
last_cmd="left-ctrl left-alt escape"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "" ]
then
last_cmd="left-ctrl left-alt"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
else
last_cmd="left-ctrl left-alt ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
fi
elif [ "$cmd" == "CTRL-SHIFT" ]
then
if [ "$info" == "BREAK" -o "$info" == "PAUSE" ]
then
last_cmd="left-ctrl left-shift pause"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "END" -o "$info" == "SPACE" -o "$info" == "TAB" -o "$info" == "DELETE" -o "$info" == "F1" -o "$info" == "F2" -o "$info" == "F3" -o "$info" == "F4" -o "$info" == "F5" -o "$info" == "F6" -o "$info" == "F7" -o "$info" == "F8" -o "$info" == "F9" -o "$info" == "F10" -o "$info" == "F11" -o "$info" == "F12" ]
then
last_cmd="left-ctrl left-shift ${cmd,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "ESC" -o "$info" == "ESCAPE" ]
then
last_cmd="left-ctrl left-shift escape"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
elif [ "$info" == "" ]
then
last_cmd="left-ctrl left-shift"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
else
last_cmd="left-ctrl left-shift ${info,,}"
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
fi
elif [ "$cmd" == "REPEAT" ]
then
if [ "$last_cmd" == "UNS" -o "$last_cmd" == "" ]
then
echo "($line_num) Parse error: Using REPEAT with DELAY, DEFAULTDELAY or BLANK is not allowed."
else
for (( i=0; i<$info; i++ )); do
if [ "$last_cmd" == "STRING" ]
then
for (( j=0; j<${#last_string}; j++ )); do
kbcode=$(convert "${last_string:$j:1}")
if [ "$kbcode" != "" ]
then
echo "$kbcode" | ./hid-gadget-test $kb > /dev/null
fi
done
else
echo "$last_cmd" | ./hid-gadget-test $kb > /dev/null
fi
usleep $defdelay
done
fi
elif [ "$cmd" != "" ]
then
echo "($line_num) Parse error: Unexpected $cmd."
fi
usleep $defdelay
done < "$1"