Skip to content

Commit c0c1f7c

Browse files
committed
Drop Python 2 support
1 parent fcfd40f commit c0c1f7c

6 files changed

Lines changed: 24 additions & 51 deletions

File tree

cortexutils/analyzer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# encoding: utf-8
32

43
import os
54
import tempfile
@@ -11,7 +10,7 @@
1110

1211
class Analyzer(Worker):
1312
def __init__(self, job_directory=None, secret_phrases=None):
14-
Worker.__init__(self, job_directory, secret_phrases)
13+
super().__init__(job_directory, secret_phrases)
1514

1615
# Not breaking compatibility
1716
self.artifact = self._input
@@ -31,13 +30,13 @@ def get_data(self):
3130
return self.get_param("data", None, "Missing data field")
3231

3332
def get_param(self, name, default=None, message=None):
34-
data = super(Analyzer, self).get_param(name, default, message)
33+
data = super().get_param(name, default, message)
3534
if (
3635
name == "file"
3736
and self.data_type == "file"
3837
and self.job_directory is not None
3938
):
40-
path = "%s/input/%s" % (self.job_directory, data)
39+
path = f"{self.job_directory}/input/{data}"
4140
if os.path.isfile(path):
4241
return path
4342
else:
@@ -117,7 +116,7 @@ def report(self, full_report, ensure_ascii=False):
117116
operation_list = self.operations(full_report)
118117
except Exception:
119118
pass # nosec B110
120-
super(Analyzer, self).report(
119+
super().report(
121120
{
122121
"success": True,
123122
"summary": summary,

cortexutils/extractor.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
import re
3-
from builtins import str as unicode
43

54

65
class ExtractionError(Exception):
@@ -67,7 +66,7 @@ def __init_regex():
6766
+ "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"
6867
+ ")"
6968
)
70-
regex.append({"type": "ip", "regex": re.compile(r"{}".format(r))})
69+
regex.append({"type": "ip", "regex": re.compile(r)})
7170

7271
# URL
7372
regex.append({"type": "url", "regex": re.compile(r"^(http://|https://)")})
@@ -95,7 +94,7 @@ def __init_regex():
9594
{
9695
"type": "user-agent",
9796
"regex": re.compile(
98-
r"^(Mozilla/[45]\.0 |AppleWebKit/[0-9]{3}\.[0-9]{2} |Chrome/[0-9]{2}\.[0-9]\." # noqa
97+
r"^(Mozilla/[45]\.0 |AppleWebKit/[0-9]{3}\.[0-9]{2} |Chrome/[0-9]{2}\.[0-9]\." # noqa: E501
9998
r"[0-9]{4}\.[0-9]{3} |Safari/[0-9]{3}\.[0-9]{2} ).*?$"
10099
),
101100
}
@@ -115,7 +114,7 @@ def __init_regex():
115114
"type": "registry",
116115
"regex": re.compile(
117116
r"^(HKEY|HKLM|HKCU|HKCR|HKCC)"
118-
r"(_LOCAL_MACHINE|_CURRENT_USER|_CURRENT_CONFIG|_CLASSES_ROOT|)[\\a-zA-Z0-9]+$" # noqa
117+
r"(_LOCAL_MACHINE|_CURRENT_USER|_CURRENT_CONFIG|_CLASSES_ROOT|)[\\a-zA-Z0-9]+$" # noqa: E501
119118
),
120119
}
121120
)
@@ -149,7 +148,7 @@ def __checktype(self, value):
149148
if self.ignore == value:
150149
return ""
151150

152-
if isinstance(value, (str, unicode)):
151+
if isinstance(value, str):
153152
for r in self.regex:
154153
if r.get("regex").match(value):
155154
return r.get("type")
@@ -179,7 +178,7 @@ def check_iterable(self, iterable):
179178
"""
180179
results = []
181180
# Only the string left
182-
if isinstance(iterable, (str, unicode)):
181+
if isinstance(iterable, str):
183182
dt = self.__checktype(iterable)
184183
if len(dt) > 0:
185184
results.append({"dataType": dt, "data": iterable})

cortexutils/responder.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/usr/bin/env python
2-
# encoding: utf-8
3-
42
from cortexutils.worker import Worker
53

64

75
class Responder(Worker):
86
def __init__(self, job_directory=None, secret_phrases=None):
9-
Worker.__init__(self, job_directory, secret_phrases)
7+
super().__init__(job_directory, secret_phrases)
108

119
# Not breaking compatibility
1210
self.artifact = self._input
@@ -28,7 +26,7 @@ def report(self, full_report, ensure_ascii=False):
2826
operation_list = self.operations(full_report)
2927
except Exception:
3028
pass # nosec B110
31-
super(Responder, self).report(
29+
super().report(
3230
{"success": True, "full": full_report, "operations": operation_list},
3331
ensure_ascii,
3432
)

cortexutils/worker.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env python
2-
# encoding: utf-8
3-
42
import codecs
53
import json
64
import os
@@ -9,7 +7,7 @@
97
DEFAULT_SECRET_PHRASES = ("key", "password", "secret")
108

119

12-
class Worker(object):
10+
class Worker:
1311
READ_TIMEOUT = 3 # seconds
1412

1513
def __init__(self, job_directory, secret_phrases):
@@ -25,8 +23,9 @@ def __init__(self, job_directory, secret_phrases):
2523
self.secret_phrases = secret_phrases
2624
# Load input
2725
self._input = {}
28-
if os.path.isfile("%s/input/input.json" % self.job_directory):
29-
with open("%s/input/input.json" % self.job_directory) as f_input:
26+
input_path = f"{self.job_directory}/input/input.json"
27+
if os.path.isfile(input_path):
28+
with open(input_path) as f_input:
3029
self._input = json.load(f_input)
3130
else:
3231
# If input file doesn't exist,
@@ -72,15 +71,9 @@ def __set_proxies(self):
7271
def __set_encoding():
7372
try:
7473
if sys.stdout.encoding != "UTF-8":
75-
if sys.version_info[0] == 3:
76-
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer, "strict")
77-
else:
78-
sys.stdout = codecs.getwriter("utf-8")(sys.stdout, "strict")
74+
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer, "strict")
7975
if sys.stderr.encoding != "UTF-8":
80-
if sys.version_info[0] == 3:
81-
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.buffer, "strict")
82-
else:
83-
sys.stderr = codecs.getwriter("utf-8")(sys.stderr, "strict")
76+
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.buffer, "strict")
8477
except Exception:
8578
pass # nosec B110
8679

@@ -123,13 +116,9 @@ def __write_output(self, data, ensure_ascii=False):
123116
if self.job_directory is None:
124117
json.dump(data, sys.stdout, ensure_ascii=ensure_ascii)
125118
else:
126-
try:
127-
os.makedirs("%s/output" % self.job_directory)
128-
except Exception:
129-
pass # nosec B110
130-
with open(
131-
"%s/output/output.json" % self.job_directory, mode="w"
132-
) as f_output:
119+
output_path = f"{self.job_directory}/output"
120+
os.makedirs(output_path, exist_ok=True)
121+
with open(f"{output_path}/output.json", mode="w") as f_output:
133122
json.dump(data, f_output, ensure_ascii=ensure_ascii)
134123

135124
def get_data(self):

tests/test_suite_analyzer.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
#!/usr/bin/env python
2-
# coding: utf-8
32

43
import os
54
import sys
65
import json
76
import unittest
87

9-
from io import open
108
from cortexutils.analyzer import Analyzer
119

12-
# Different lib when using python3 or 2
13-
if sys.version_info >= (3, 0):
14-
from io import StringIO
15-
else:
16-
from StringIO import StringIO
10+
from io import StringIO
1711

1812

1913
def load_test_fixture(fixture_path):
2014
path = os.path.dirname(os.path.abspath(__file__))
21-
fixture_file = open(path + "/" + fixture_path)
22-
input = fixture_file.read()
23-
fixture_file.close()
15+
with open(path + "/" + fixture_path) as fixture_file:
16+
input = fixture_file.read()
2417
sys.stdin = StringIO(input)
2518
sys.stdout = StringIO()
2619

tests/test_suite_integration.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
#!/usr/bin/env python
2-
# coding: utf-8
32
import json
43
import unittest
54
import sys
65

76
from cortexutils.analyzer import Analyzer
87

9-
# Different lib when using python3 or 2
10-
if sys.version_info >= (3, 0):
11-
from io import StringIO
12-
else:
13-
from StringIO import StringIO
8+
from io import StringIO
149

1510

1611
class AnalyzerExtractorOutputTest(unittest.TestCase):

0 commit comments

Comments
 (0)