-
Notifications
You must be signed in to change notification settings - Fork 585
/
loki-upgrader.py
executable file
·298 lines (257 loc) · 11.9 KB
/
loki-upgrader.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# -*- coding: utf-8 -*-
#
# LOKI Upgrader
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen #For python 3.5
import json
import zipfile
import shutil
import io
import os
import argparse
import traceback
from sys import platform as _platform
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
from os.path import exists
# Win32 Imports
if _platform == "win32":
try:
import win32api
except Exception:
platform = "linux" # crazy guess
from lib.lokilogger import *
# Platform
platform = ""
if _platform == "linux" or _platform == "linux2":
platform = "linux"
elif _platform == "darwin":
platform = "macos"
elif _platform == "win32":
platform = "windows"
def needs_update(sig_url):
try:
o=urlparse(sig_url)
path=o.path.split('/')
branch=path[4].split('.')[0]
path.pop(len(path)-1)
path.pop(len(path)-1)
url = o.scheme+'://api.'+o.netloc+'/repos'+'/'.join(path)+'/commits/'+branch
response_info = urlopen(url)
j = json.load(response_info)
sha=j['sha']
cache='_'.join(path)+'.cache'
changed=False
if exists(cache):
with open(cache, "r") as file:
old_sha = file.read().rstrip()
if sha != old_sha:
changed=True
else:
with open(cache, "w") as file:
file.write(sha)
changed=True
return changed
except Exception:
return True
class LOKIUpdater(object):
# Incompatible signatures
INCOMPATIBLE_RULES = []
UPDATE_URL_SIGS = [
"https://github.com/Neo23x0/signature-base/archive/master.zip",
"https://github.com/reversinglabs/reversinglabs-yara-rules/archive/develop.zip"
]
UPDATE_URL_LOKI = "https://api.github.com/repos/Neo23x0/Loki/releases/latest"
def __init__(self, debug, logger, application_path):
self.debug = debug
self.logger = logger
self.application_path = application_path
def update_signatures(self, clean=False):
try:
for sig_url in self.UPDATE_URL_SIGS:
if needs_update(sig_url):
# Downloading current repository
try:
self.logger.log("INFO", "Upgrader", "Downloading %s ..." % sig_url)
response = urlopen(sig_url)
except Exception:
if self.debug:
traceback.print_exc()
self.logger.log("ERROR", "Upgrader", "Error downloading the signature database - "
"check your Internet connection")
sys.exit(1)
# Preparations
try:
sigDir = os.path.join(self.application_path, os.path.abspath('signature-base/'))
if clean:
self.logger.log("INFO", "Upgrader", "Cleaning directory '%s'" % sigDir)
shutil.rmtree(sigDir)
for outDir in ['', 'iocs', 'yara', 'misc']:
fullOutDir = os.path.join(sigDir, outDir)
if not os.path.exists(fullOutDir):
os.makedirs(fullOutDir)
except Exception:
if self.debug:
traceback.print_exc()
self.logger.log("ERROR", "Upgrader", "Error while creating the signature-base directories")
sys.exit(1)
# Read ZIP file
try:
zipUpdate = zipfile.ZipFile(io.BytesIO(response.read()))
for zipFilePath in zipUpdate.namelist():
sigName = os.path.basename(zipFilePath)
if zipFilePath.endswith("/"):
continue
# Skip incompatible rules
skip = False
for incompatible_rule in self.INCOMPATIBLE_RULES:
if sigName.endswith(incompatible_rule):
self.logger.log("NOTICE", "Upgrader", "Skipping incompatible rule %s" % sigName)
skip = True
if skip:
continue
# Extract the rules
self.logger.log("DEBUG", "Upgrader", "Extracting %s ..." % zipFilePath)
if "/iocs/" in zipFilePath and zipFilePath.endswith(".txt"):
targetFile = os.path.join(sigDir, "iocs", sigName)
elif "/yara/" in zipFilePath and zipFilePath.endswith(".yar"):
targetFile = os.path.join(sigDir, "yara", sigName)
elif "/misc/" in zipFilePath and zipFilePath.endswith(".txt"):
targetFile = os.path.join(sigDir, "misc", sigName)
elif zipFilePath.endswith(".yara"):
targetFile = os.path.join(sigDir, "yara", sigName)
else:
continue
# New file
if not os.path.exists(targetFile):
self.logger.log("INFO", "Upgrader", "New signature file: %s" % sigName)
# Extract file
source = zipUpdate.open(zipFilePath)
target = open(targetFile, "wb")
with source, target:
shutil.copyfileobj(source, target)
target.close()
source.close()
except Exception:
if self.debug:
traceback.print_exc()
self.logger.log("ERROR", "Upgrader", "Error while extracting the signature files from the download "
"package")
sys.exit(1)
else:
self.logger.log("INFO", "Upgrader", "%s is up to date." % sig_url)
except Exception:
if self.debug:
traceback.print_exc()
return False
return True
def update_loki(self):
try:
# Downloading the info for latest release
try:
self.logger.log("INFO", "Upgrader", "Checking location of latest release %s ..." % self.UPDATE_URL_LOKI)
response_info = urlopen(self.UPDATE_URL_LOKI)
data = json.load(response_info)
# Get download URL
zip_url = data['assets'][0]['browser_download_url']
self.logger.log("INFO", "Upgrader", "Downloading latest release %s ..." % zip_url)
response_zip = urlopen(zip_url)
except Exception:
if self.debug:
traceback.print_exc()
self.logger.log("ERROR", "Upgrader", "Error downloading the loki update - check your Internet connection")
sys.exit(1)
# Read ZIP file
try:
zipUpdate = zipfile.ZipFile(io.BytesIO(response_zip.read()))
for zipFilePath in zipUpdate.namelist():
if zipFilePath.endswith("/") or "/config/" in zipFilePath or "/loki-upgrader.exe" in zipFilePath:
continue
source = zipUpdate.open(zipFilePath)
targetFile = "/".join(zipFilePath.split("/")[1:])
self.logger.log("INFO", "Upgrader", "Extracting %s ..." %targetFile)
try:
# Create file if not present
if not os.path.exists(os.path.dirname(targetFile)):
if os.path.dirname(targetFile) != '':
os.makedirs(os.path.dirname(targetFile))
except Exception:
if self.debug:
self.logger.log("DEBUG", "Upgrader", "Cannot create dir name '%s'" % os.path.dirname(targetFile))
traceback.print_exc()
try:
# Create target file
target = open(targetFile, "wb")
with source, target:
shutil.copyfileobj(source, target)
if self.debug:
self.logger.log("DEBUG", "Upgrader", "Successfully extracted '%s'" % targetFile)
target.close()
except Exception:
self.logger.log("ERROR", "Upgrader", "Cannot extract '%s'" % targetFile)
if self.debug:
traceback.print_exc()
except Exception:
if self.debug:
traceback.print_exc()
self.logger.log("ERROR", "Upgrader",
"Error while extracting the signature files from the download package")
sys.exit(1)
except Exception:
if self.debug:
traceback.print_exc()
return False
return True
def get_application_path():
try:
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(os.path.realpath(sys.executable))
else:
application_path = os.path.dirname(os.path.realpath(__file__))
if "~" in application_path and platform == "windows":
# print "Trying to translate"
# print application_path
application_path = win32api.GetLongPathName(application_path)
#if args.debug:
# logger.log("DEBUG", "Init", "Application Path: %s" % application_path)
return application_path
except Exception:
print("Error while evaluation of application path")
traceback.print_exc()
if __name__ == '__main__':
# Parse Arguments
parser = argparse.ArgumentParser(description='Loki - Upgrader')
parser.add_argument('-l', help='Log file', metavar='log-file', default='loki-upgrade.log')
parser.add_argument('--sigsonly', action='store_true', help='Update the signatures only', default=False)
parser.add_argument('--progonly', action='store_true', help='Update the program files only', default=False)
parser.add_argument('--nolog', action='store_true', help='Don\'t write a local log file', default=False)
parser.add_argument('--debug', action='store_true', default=False, help='Debug output')
parser.add_argument('--clean', action='store_true', default=False, help='Clean up the signature directory and get '
'a fresh set')
parser.add_argument('--detached', action='store_true', default=False, help=argparse.SUPPRESS)
args = parser.parse_args()
# Computername
if platform == "windows":
t_hostname = os.environ['COMPUTERNAME']
else:
t_hostname = os.uname()[1]
# Logger
logger = LokiLogger(args.nolog, args.l, t_hostname, '', '', False, False, False, args.debug, platform=platform, caller='upgrader')
# Update LOKI
updater = LOKIUpdater(args.debug, logger, get_application_path())
if not args.sigsonly:
logger.log("INFO", "Upgrader", "Updating LOKI ...")
updater.update_loki()
if not args.progonly:
logger.log("INFO", "Upgrader", "Updating Signatures ...")
updater.update_signatures(args.clean)
logger.log("INFO", "Upgrader", "Update complete")
if args.detached:
logger.log("INFO", "Upgrader", "Press any key to return ...")
sys.exit(0)