-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
252 lines (194 loc) · 7.73 KB
/
main.py
File metadata and controls
252 lines (194 loc) · 7.73 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
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
from kivy.app import App
from kivy.lang import Builder
from kivy.lib import osc
from kivy.utils import platform
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import ObjectProperty, StringProperty, DictProperty
from kivy.logger import Logger
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader, TabbedPanelContent
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
import json
ACTIVITY_PORT = 3001
SERVICE_PORT = 3000
API_OFFSET = "/mitmcanary/"
Builder.load_string('''
<ScrollableLabel>:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
text: root.text
''')
class ScrollableLabel(ScrollView):
text = StringProperty('')
class ButtonListItem(Button):
wid = StringProperty('')
image = StringProperty('')
title = StringProperty('')
label = StringProperty('')
class AlertButton(Button):
alert_id = StringProperty('')
expected_request_name = StringProperty('')
timestamp = StringProperty('')
configuration = StringProperty('')
raw_message = DictProperty({})
def on_expected_request_name(self, instance, value):
self.update_text()
def on_timestamp(self, instance, value):
self.update_text()
def on_configuration(self, instance, value):
self.update_text()
def update_text(self):
self.text = "[{0}] {1} - {2}".format(
self.timestamp,
self.expected_request_name,
self.configuration,
)
def build_alert_decision_dialog(self):
popup = Popup(
title='Alert Details',
)
box = BoxLayout()
box.orientation = "vertical"
# todo Fill info box with useful data
info_box = BoxLayout()
info_box.orientation = "vertical"
info_box.size_hint_y = 0.90
scroll_label = ScrollableLabel(
text=json.dumps(
self.raw_message,
sort_keys=True,
indent=4,
separators=(',', ': ')
)
)
info_box.add_widget(scroll_label)
box.add_widget(info_box)
button_box = BoxLayout()
button_box.size_hint_y = 0.10
button_box.orientation = "horizontal"
close_button = Button(text="Cancel")
close_button.bind(on_press=popup.dismiss)
close_button.size_hint_x = 1
button_box.add_widget(close_button)
malicious_button = Button(text="Malicious")
malicious_button.bind(on_press=lambda x: self.report_malicious(popup))
button_box.add_widget(malicious_button)
benign_button = Button(text="Benign")
benign_button.bind(on_press=lambda x: self.report_benign(popup))
button_box.add_widget(benign_button)
box.add_widget(button_box)
popup.content = box
return popup
def report_malicious(self, popup):
osc.sendMsg(API_OFFSET + "classify-alert", [json.dumps({"id": self.alert_id, "malicious": 1}), ], port=SERVICE_PORT)
popup.dismiss()
app.alert_tab_alert_buttons.remove_widget(self)
def report_benign(self, popup):
global app
osc.sendMsg(API_OFFSET + "classify-alert", [json.dumps({"id": self.alert_id, "benign": 1}), ], port=SERVICE_PORT)
popup.dismiss()
app.alert_tab_alert_buttons.remove_widget(self)
def on_release(self):
popup = self.build_alert_decision_dialog()
popup.open()
class ButtonList(GridLayout):
pass
class ServiceApp(App):
def build(self):
if platform == 'android':
from android import AndroidService
service = AndroidService('my pong service', 'running')
service.start('service started')
self.service = service
osc.init()
oscid = osc.listen(ipAddr='127.0.0.1', port=ACTIVITY_PORT)
osc.bind(oscid, self.some_api_callback, API_OFFSET)
osc.bind(oscid, self.handle_alerts, API_OFFSET + "pending-alerts")
Clock.schedule_interval(lambda *x: osc.readQueue(oscid), 0)
Window.size = 640, 480
self.layout = TabbedPanel()
self.layout.do_default_tab = True
#self.status_tab = GridLayout()
#self.layout.default_tab_content = self.status_tab
#self.layout.default_tab_text = "Status"
#self.status_tab.add_widget(Button(text="TODO make this page..."))
#self.alert_tab = TabbedPanelHeader(text="Alert")
self.alert_tab_box = BoxLayout()
self.alert_tab_box.orientation = 'vertical'
self.alert_tab_box.size_hint = 1, 1
#self.alert_tab.content = self.alert_tab_box
self.alert_tab_scroll = ScrollView(
size_hint=(1, 1),
size=self.alert_tab_box.size,
#size=Window.size,
scroll_type=['bars', 'content'],
do_scroll_y=True,
do_scroll_x=False
)
self.alert_tab_box.add_widget(self.alert_tab_scroll)
self.alert_tab_alert_buttons = ButtonList()
self.alert_tab_alert_buttons.bind(minimum_height=self.alert_tab_alert_buttons.setter('height'))
self.alert_tab_alert_buttons.cols = 1
self.alert_tab_alert_buttons.size_hint_y = None
self.alert_tab_alert_buttons.row_default_height = '30dp'
self.alert_tab_alert_buttons.row_force_default = True
self.alert_tab_alert_buttons.spacing = 0, 0
self.alert_tab_alert_buttons.padding = 0, 0
self.alert_tab_scroll.add_widget(self.alert_tab_alert_buttons)
#self.requests_tab = TabbedPanelHeader(text="Requests")
self.layout.default_tab_content = self.alert_tab_box
self.layout.default_tab_text = "Alerts"
#self.layout.add_widget(self.status_tab)
#self.layout.add_widget(self.alert_tab)
#self.layout.add_widget(self.requests_tab)
self.request_pending_alerts()
return self.layout
def request_pending_alerts(self):
osc.sendMsg(API_OFFSET + "pending-alerts", ['', ], port=SERVICE_PORT)
def handle_alerts(self, message, *args):
alerts = json.loads(message[2])
for i, a in alerts["alerts"].items():
self.add_alert_button(
a["expected_request_name"],
a["timestamp"],
i,
a["expected_request_configuration"],
a
)
def configuration_to_string(self, config):
return ", ".join(["{0} = {1}".format(k, v) for k, v in config.items()])
def remove_alert_button(self, alert_id):
# todo DO THIS
pass
def add_alert_button(self,
expected_request_name,
timestamp,
alert_id,
expected_request_config,
raw_message
):
# todo Search for duplicates
ab = AlertButton()
ab.expected_request_name = expected_request_name
ab.timestamp = timestamp
ab.alert_id = alert_id
ab.configuration = self.configuration_to_string(expected_request_config)
ab.raw_message = raw_message
self.alert_tab_alert_buttons.add_widget(ab)
def some_api_callback(self, message, *args):
print "THIS FUNCTION DOES NOTHING!", message
#self.add_alert_button("butt request", "10", "1")
def ping(self):
print "Sending ping..."
osc.sendMsg(API_OFFSET, ['ping', ], port=SERVICE_PORT)
if __name__ == '__main__':
app = ServiceApp()
app.run()