-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretaliation.py
More file actions
192 lines (166 loc) · 5.21 KB
/
retaliation.py
File metadata and controls
192 lines (166 loc) · 5.21 KB
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
#!/bin/env python
# Borrowed heavily from Retaliation from codedance (http://github.com/codedance/Retaliation)
import sys
import platform
import time
import socket
import urllib
import re
import json
import usb.core
import usb.util
COMMAND_SETS = {
"yung" : (
("zero", 0),
("right", 5000),
("up", 3000),
("fire", 1),
("zero", 0),
),
"dan" : (
("zero", 0),
("right", 4500),
("up", 2500),
("fire", 1),
("zero", 0),
),
"aaron": (
("zero", 0),
("right", 3000),
("up", 2500),
("fire", 1),
("zero", 0),
),
"jon": (
("zero", 0),
("right", 2000),
("up", 2500),
("fire", 1),
("zero", 0),
),
"phil": (
("zero", 0),
("right", 750),
("up", 4000),
("fire", 1),
("zero", 0),
),
"antony": (
("zero", 0),
("right", 14000),
("up", 4000),
("fire", 1),
("zero", 0),
)
}
NOTIFICATION_UDP_PORT = 22222
DEVICE = None
DOWN = 1 << 0
UP = 1 << 1
LEFT = 1 << 2
RIGHT = 1 << 3
FIRE = 1 << 4
STOP = 1 << 5
def usage():
print "Usage: retaliation.py [command] [value]"
print ""
print " commands:"
print " stalk - sit around waiting for a "
print " notification, then attack the victim!"
print ""
print " up - move up <value> milliseconds"
print " down - move down <value> milliseconds"
print " right - move right <value> milliseconds"
print " left - move left <value> milliseconds"
print " fire - fire <value> times (between 1-4)"
print " zero - park at zero position (bottom-left)"
print " pause - pause <value> milliseconds"
print ""
print " <command_set_name> - run/test a defined COMMAND_SET"
print " e.g. run:"
print " retaliation.py 'chris'"
print " to test targeting of chris as defined in your command set."
print ""
def setup_usb():
global DEVICE
DEVICE = usb.core.find(idVendor=0x0a81, idProduct=0x0701)
if DEVICE is None:
raise ValueError('Missile device not found')
DEVICE.set_configuration()
def send_cmd(cmd):
DEVICE.ctrl_transfer(0x21, 0x09, 0, 0, [cmd])
def send_move(cmd, duration_ms):
send_cmd(cmd)
time.sleep(duration_ms / 1000.0)
send_cmd(STOP)
def run_command(command, value):
command = command.lower()
if command == "right":
send_move(RIGHT, value)
elif command == "left":
send_move(LEFT, value)
elif command == "up":
send_move(UP, value)
elif command == "down":
send_move(DOWN, value)
elif command == "zero" or command == "park" or command == "reset":
send_move(DOWN, 2000)
send_move(LEFT, 8000)
elif command == "pause" or command == "sleep":
time.sleep(value / 1000.0)
elif command == "fire" or command == "shoot":
if value < 1 or value > 4:
value = 1
# stabilize prior to the shot, then allow for reload time after
time.sleep(0.5)
for i in range(value):
send_cmd(FIRE)
time.sleep(4.5)
else:
print "Error: unknown command: '%s'" % command
def run_command_set(commands):
for cmd, value in commands:
run_command(cmd, value)
def target_user(user):
# Not efficient but our user list is probably less than 1k.
# Do a case insenstive search for convenience.
for key in COMMAND_SETS:
print "Checking against key %s" % key
if key.lower() == user.lower():
# We have a command set that targets our user so got for it!
run_command_set(COMMAND_SETS[key])
match = True
break
if not match:
print "WARNING: No target command set defined for user %s" % user
def wait_for_event():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', NOTIFICATION_UDP_PORT))
while True:
data, addr = sock.recvfrom(8 * 1024)
try:
notification_data = json.loads(data)
victim = notification_data["victim"]
print "Targeting victim: " + victim
target_user(victim)
except:
pass
def main(args):
if len(args) < 2:
usage()
sys.exit(1)
setup_usb()
if args[1] == "stalk":
print "Listening and waiting for victims..."
wait_for_event()
return
command = args[1]
value = 0
if len(args) > 2:
value = int(args[2])
if command in COMMAND_SETS:
run_command_set(COMMAND_SETS[command])
else:
run_command(command, value)
if __name__ == '__main__':
main(sys.argv)