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
|
from chrysacase import ChrysalideTestCase
import pychrysalide
from pychrysalide.analysis.contents import MemoryContent
from pychrysalide.analysis.db import certs
from pychrysalide.analysis.db import AdminClient, AnalystClient
from pychrysalide.analysis.db import HubServer
import os
import shutil
import sys
import tempfile
import threading
class TestDbConnection(ChrysalideTestCase):
"""TestCase for analysis.db."""
@classmethod
def setUpClass(cls):
super(TestDbConnection, cls).setUpClass()
cls.log('Compile binary "strings" if needed...')
fullname = sys.modules[cls.__module__].__file__
dirpath = os.path.dirname(fullname)
cls._bin_path = os.path.realpath(dirpath + '/../../format/elf/')
os.system('make -C %s strings > /dev/null 2>&1' % cls._bin_path)
cls._tmp_path = tempfile.mkdtemp()
cls._server_path = '%s/.chrysalide/servers/localhost-9999/' % cls._tmp_path
os.makedirs(cls._server_path)
cls._server_authorized_path = '%s/authorized/' % cls._server_path
os.makedirs(cls._server_authorized_path)
cls._client_path = '%s/.chrysalide/clients/' % cls._tmp_path
os.makedirs(cls._client_path)
cls._client_cert_path = '%s/localhost-9999/' % cls._client_path
os.makedirs(cls._client_cert_path)
cls.log('Using temporary directory "%s"' % cls._tmp_path)
@classmethod
def tearDownClass(cls):
super(TestDbConnection, cls).tearDownClass()
cls.log('Delete built binaries...')
os.system('make -C %s clean > /dev/null 2>&1' % cls._bin_path)
## os.system('ls -laihR %s' % cls._tmp_path)
cls.log('Delete directory "%s"' % cls._tmp_path)
shutil.rmtree(cls._tmp_path)
def testServerListening(self):
"""List binaries available from server."""
from pychrysalide import core
core.set_verbosity(0)
identity = {
'C': 'FR',
'CN': 'Test authority'
}
ret = certs.build_keys_and_ca(self._server_path, 'ca', 3650 * 24 * 60 * 60, identity)
self.assertTrue(ret)
identity = {
'C': 'FR',
'CN': 'Test server'
}
ret = certs.build_keys_and_request(self._server_path, 'server', identity);
self.assertTrue(ret)
ret = certs.sign_cert('%s/server-csr.pem' % self._server_path, '%s/ca-cert.pem' % self._server_path, \
'%s/ca-key.pem' % self._server_path, '%s/server-cert.pem' % self._server_path, \
3650 * 24 * 60 * 60)
self.assertTrue(ret)
identity = {
'C': 'FR',
'CN': 'Test admin'
}
ret = certs.build_keys_and_request(self._client_path, 'client', identity);
self.assertTrue(ret)
ret = certs.sign_cert('%s/client-csr.pem' % self._client_path, '%s/ca-cert.pem' % self._server_path, \
'%s/ca-key.pem' % self._server_path, '%s/client-cert.pem' % self._client_cert_path, \
3650 * 24 * 60 * 60)
self.assertTrue(ret)
shutil.copy('%s/ca-cert.pem' % self._server_path,
'%s/ca-cert.pem' % self._client_cert_path)
shutil.copy('%s/client-cert.pem' % self._client_cert_path,
'%s/client-cert.pem' % self._server_authorized_path)
os.environ['XDG_CONFIG_HOME'] = self._tmp_path
os.environ['HOME'] = self._tmp_path
server = HubServer('localhost', '9999')
#print(server)
ret = server.start()
#print(ret)
# admin = AdminClient()
# ret = admin.start('localhost', '9999')
# self.assertTrue(ret)
# def _on_existing_binaries_updated(adm, evt):
# evt.set()
# event = threading.Event()
# admin.connect('existing-binaries-updated', _on_existing_binaries_updated, event)
# ret = admin.request_existing_binaries()
# self.assertTrue(ret)
# event.wait()
# self.assertEqual(len(admin.existing_binaries), 0)
cnt = MemoryContent(b'A' * 400 * 1024)
analyst = AnalystClient(cnt.checksum, "elf", [])
def _on_server_status_changed(analyst, hint, evt):
print(hint)
evt.set()
event = threading.Event()
analyst.connect('server-status-changed', _on_server_status_changed, event)
ret = analyst.start('localhost', '9999')
self.assertTrue(ret)
event.wait()
event.clear()
ret = analyst.send_content(cnt)
self.assertTrue(ret)
event.wait()
|