-
Notifications
You must be signed in to change notification settings - Fork 45
/
Duckyspark_translator.py
268 lines (225 loc) · 8.05 KB
/
Duckyspark_translator.py
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
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# Duckyspark - Translator from USB-Rubber-Ducky payloads (Ducky script) to a Digispark code.
# Authors:
# Alex Bridges https://github.com/toxydose
# https://awake.pro/
# Vlad Khomenko https://github.com/eclipse7
#
# Usage of this program is only allowed within boundaries of law.
# Developers assume no liability and are not responsible for any misuse or damage caused by this program.
from __future__ import print_function
import sys
out = sys.__stdout__
VERSION = '0.4.1'
HELP = '''
========== Duckyspark v.''' + VERSION + ''' ==============================
https://github.com/toxydose/Duckyspark/
Translator from USB-Rubber-Ducky payloads (Ducky script) to a Digispark code.
=============================================================
Authors:
Alex Bridges https://github.com/toxydose
Vlad Khomenko https://github.com/eclipse7
https://awake.pro/ community
Options:
-h, --help Show basic help message and exit
Usage:
python3 Duckyspark_translator.py [payload.txt] [output_file] -
specify payload file and output file
python Duckyspark_translator.py [payload.txt] -
translated payload will be saved in "digipayload.ino"
---------------------------------------------------------------
Ducky payloads you can find here:
https://github.com/hak5darren/USB-Rubber-Ducky/wiki/Payloads
Or, you can simply write your own payloads using Ducky script:
https://github.com/hak5darren/USB-Rubber-Ducky/wiki/Duckyscript
----------------------------------------------------------------
'''
SUCCESS = 'Success!'
ERROR = 'ERROR! -h or --help for help\n'
# order is important
SPECIAL_BUTTONS = {'CONTROL_RIGHT': 'MOD_CONTROL_RIGHT',
'CONTROL_LEFT': 'MOD_CONTROL_LEFT',
'CONTROL': 'MOD_CONTROL_LEFT',
'CTRL_RIGHT': 'MOD_CONTROL_RIGHT',
'CTRL_LEFT': 'MOD_CONTROL_LEFT',
'CTRL': 'MOD_CONTROL_LEFT',
'SHIFT_RIGHT': 'MOD_SHIFT_RIGHT',
'SHIFT_LEFT': 'MOD_SHIFT_LEFT',
'SHIFT': 'MOD_SHIFT_LEFT',
'ALT_RIGHT': 'MOD_ALT_RIGHT',
'ALT_LEFT': 'MOD_ALT_LEFT',
'ALT': 'MOD_ALT_LEFT',
'GUI_RIGHT': 'MOD_GUI_RIGHT',
'GUI_LEFT': 'MOD_GUI_LEFT',
'GUI': 'MOD_GUI_LEFT',
'WINDOWS': 'MOD_GUI_LEFT'}
# menu = Shift + F10
BUTTONS = {'a': 'KEY_A',
'b': 'KEY_B',
'c': 'KEY_С',
'd': 'KEY_D',
'e': 'KEY_E',
'f': 'KEY_F',
'g': 'KEY_G',
'h': 'KEY_H',
'i': 'KEY_I',
'j': 'KEY_J',
'k': 'KEY_K',
'l': 'KEY_L',
'm': 'KEY_M',
'n': 'KEY_N',
'o': 'KEY_O',
'p': 'KEY_P',
'q': 'KEY_Q',
'r': 'KEY_R',
's': 'KEY_S',
't': 'KEY_T',
'u': 'KEY_U',
'v': 'KEY_V',
'w': 'KEY_W',
'x': 'KEY_X',
'y': 'KEY_Y',
'z': 'KEY_Z',
'F1': 'KEY_F1',
'F2': 'KEY_F2',
'F3': 'KEY_F3',
'F4': 'KEY_F4',
'F5': 'KEY_F5',
'F6': 'KEY_F6',
'F7': 'KEY_F7',
'F8': 'KEY_F8',
'F9': 'KEY_F9',
'F10': 'KEY_F10',
'F11': 'KEY_F11',
'F12': 'KEY_F12',
'LEFTARROW': 'KEY_ARROW_LEFT',
'RIGHTARROW': 'KEY_ARROW_RIGHT',
'UPARROW': 'KEY_ARROW_UP',
'DOWNARROW': 'KEY_ARROW_DOWN',
'LEFT': 'KEY_ARROW_LEFT',
'RIGHT': 'KEY_ARROW_RIGHT',
'UP': 'KEY_ARROW_UP',
'DOWN': 'KEY_ARROW_DOWN',
'DELETE': 'KEY_DELETE',
'DEL': 'KEY_DELETE',
'PRINTSCREEN': 'KEY_PRT_SCR',
'TAB': 'KEY_TAB',
'ESCAPE': 'KEY_ESC',
'SPACE': 'KEY_SPACE',
'ENTER': 'KEY_ENTER'}
# arguments
payload_input = ''
payload_len = 0
if '-h' in sys.argv or '--help' in sys.argv:
print(HELP)
exit()
elif '-v' in sys.argv or '--version' in sys.argv:
print ('''
========== Duckyspark v.''' + VERSION + ''' ==============================
https://github.com/toxydose/Duckyspark/
Translator from USB-Rubber-Ducky payloads (Ducky script) to a Digispark code.
=============================================================''')
exit()
elif len(sys.argv) == 2:
try:
payload_input = open(sys.argv[1], "r")
sys.stdout = open("digipayload.ino", "w")
payload_len = len(open(sys.argv[1], "r").readlines())
except IOError:
error_reason = ('File "' + sys.argv[1] + '" does not exist!\n')
print(ERROR + error_reason)
exit()
elif len(sys.argv) == 3:
try:
payload_input = open(sys.argv[1], "r")
sys.stdout = open(sys.argv[2] + '.ino', 'w')
payload_len = len(open(sys.argv[1], "r").readlines())
except IOError:
error_reason = ('File "' + sys.argv[1] + '" does not exist!\n')
print(ERROR + error_reason)
exit()
elif len(sys.argv) > 3:
error_reason = 'Too much Arguments'
print(ERROR + error_reason)
exit()
else:
try:
payload_input = open('payload.txt', "r")
sys.stdout = open("digipayload.ino", "w")
payload_len = len(open('payload.txt', "r").readlines())
except FileNotFoundError:
print('\nERROR: Payload file "payload.txt" was not found')
print(HELP)
exit()
# Digispark program fragment
print ('''//generated by Duckyspark https://github.com/toxydose/Duckyspark
#include "DigiKeyboard.h"
#define KEY_ESC 41
#define KEY_BACKSPACE 42
#define KEY_TAB 43
#define KEY_PRT_SCR 70
#define KEY_DELETE 76
#define KEY_ARROW_RIGHT 0x4F
#define KEY_ARROW_DOWN 0x51
#define KEY_ARROW_UP 0x52
void setup() {
DigiKeyboard.delay(5000);
DigiKeyboard.sendKeyStroke(0);
''')
# ---------------------------------------
for i in range(payload_len):
line = payload_input.readline().replace('\n', '')
if len(line) < 1:
print('', end='')
else:
if 'REM' in line:
print('//', line.replace('REM ', ''))
else:
if 'DELAY' in line:
print('DigiKeyboard.', end='')
print(line.replace('DELAY', 'delay(').replace(' ', ''), end='')
print(');')
elif 'STRING' in line:
print('DigiKeyboard.', end='')
print(line.replace('"', '")); DigiKeyboard.print(char(34)); DigiKeyboard.print(F("')
.replace('\\', '")); DigiKeyboard.print(char(92)); DigiKeyboard.print(F("')
.replace('STRING ', 'print(F("'), end='')
print('")', end='')
print(');')
elif 'MENU' in line:
line = 'KEY_F10'
mod = 'MOD_SHIFT_LEFT'
print('DigiKeyboard.', end='')
print('sendKeyStroke(', end='')
print(str(line), end='')
print(',' + mod, end='')
print(');')
else:
mod = ''
for key in SPECIAL_BUTTONS.keys():
if key in line: # order in keys is important
line = line.replace(key, '')
mod += SPECIAL_BUTTONS.get(key) + ' | '
mod += '0'
line = line.replace(' ', '')
if line in BUTTONS.keys():
line = BUTTONS.get(line)
else:
line = '0'
print('DigiKeyboard.', end='')
print('sendKeyStroke(', end='')
print(str(line), end='')
print(',' + mod, end='')
print(');')
if len(line) < 1:
print('', end='')
# Digispark program fragment
print('''
}
void loop() {
}''')
# -----------------------------------
payload_input.close()
sys.stdout = out
print(SUCCESS)