Skip to content

Commit 1ffc3cf

Browse files
authored
Merge pull request #31 from JuanTecedor/drop_python_2_support
Drop Python 2 support
2 parents fcfd40f + 63c4129 commit 1ffc3cf

7 files changed

Lines changed: 49 additions & 64 deletions

File tree

cortexutils/analyzer.py

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

44
import os
55
import tempfile
@@ -11,7 +11,7 @@
1111

1212
class Analyzer(Worker):
1313
def __init__(self, job_directory=None, secret_phrases=None):
14-
Worker.__init__(self, job_directory, secret_phrases)
14+
super().__init__(job_directory, secret_phrases)
1515

1616
# Not breaking compatibility
1717
self.artifact = self._input
@@ -31,15 +31,15 @@ def get_data(self):
3131
return self.get_param("data", None, "Missing data field")
3232

3333
def get_param(self, name, default=None, message=None):
34-
data = super(Analyzer, self).get_param(name, default, message)
34+
data = super().get_param(name, default, message)
3535
if (
3636
name == "file"
3737
and self.data_type == "file"
3838
and self.job_directory is not None
3939
):
40-
path = "%s/input/%s" % (self.job_directory, data)
41-
if os.path.isfile(path):
42-
return path
40+
input_path = os.path.join(self.job_directory, "input", data)
41+
if os.path.isfile(input_path):
42+
return input_path
4343
else:
4444
return data
4545

@@ -117,7 +117,7 @@ def report(self, full_report, ensure_ascii=False):
117117
operation_list = self.operations(full_report)
118118
except Exception:
119119
pass # nosec B110
120-
super(Analyzer, self).report(
120+
super().report(
121121
{
122122
"success": True,
123123
"summary": summary,

cortexutils/extractor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
24
import re
3-
from builtins import str as unicode
45

56

67
class ExtractionError(Exception):
@@ -67,7 +68,7 @@ def __init_regex():
6768
+ "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"
6869
+ ")"
6970
)
70-
regex.append({"type": "ip", "regex": re.compile(r"{}".format(r))})
71+
regex.append({"type": "ip", "regex": re.compile(r)})
7172

7273
# URL
7374
regex.append({"type": "url", "regex": re.compile(r"^(http://|https://)")})
@@ -95,7 +96,7 @@ def __init_regex():
9596
{
9697
"type": "user-agent",
9798
"regex": re.compile(
98-
r"^(Mozilla/[45]\.0 |AppleWebKit/[0-9]{3}\.[0-9]{2} |Chrome/[0-9]{2}\.[0-9]\." # noqa
99+
r"^(Mozilla/[45]\.0 |AppleWebKit/[0-9]{3}\.[0-9]{2} |Chrome/[0-9]{2}\.[0-9]\." # noqa: E501
99100
r"[0-9]{4}\.[0-9]{3} |Safari/[0-9]{3}\.[0-9]{2} ).*?$"
100101
),
101102
}
@@ -115,7 +116,7 @@ def __init_regex():
115116
"type": "registry",
116117
"regex": re.compile(
117118
r"^(HKEY|HKLM|HKCU|HKCR|HKCC)"
118-
r"(_LOCAL_MACHINE|_CURRENT_USER|_CURRENT_CONFIG|_CLASSES_ROOT|)[\\a-zA-Z0-9]+$" # noqa
119+
r"(_LOCAL_MACHINE|_CURRENT_USER|_CURRENT_CONFIG|_CLASSES_ROOT|)[\\a-zA-Z0-9]+$" # noqa: E501
119120
),
120121
}
121122
)
@@ -149,7 +150,7 @@ def __checktype(self, value):
149150
if self.ignore == value:
150151
return ""
151152

152-
if isinstance(value, (str, unicode)):
153+
if isinstance(value, str):
153154
for r in self.regex:
154155
if r.get("regex").match(value):
155156
return r.get("type")
@@ -179,7 +180,7 @@ def check_iterable(self, iterable):
179180
"""
180181
results = []
181182
# Only the string left
182-
if isinstance(iterable, (str, unicode)):
183+
if isinstance(iterable, str):
183184
dt = self.__checktype(iterable)
184185
if len(dt) > 0:
185186
results.append({"dataType": dt, "data": iterable})

cortexutils/responder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python
2-
# encoding: utf-8
2+
# -*- coding: utf-8 -*-
33

44
from cortexutils.worker import Worker
55

66

77
class Responder(Worker):
88
def __init__(self, job_directory=None, secret_phrases=None):
9-
Worker.__init__(self, job_directory, secret_phrases)
9+
super().__init__(job_directory, secret_phrases)
1010

1111
# Not breaking compatibility
1212
self.artifact = self._input
@@ -28,7 +28,7 @@ def report(self, full_report, ensure_ascii=False):
2828
operation_list = self.operations(full_report)
2929
except Exception:
3030
pass # nosec B110
31-
super(Responder, self).report(
31+
super().report(
3232
{"success": True, "full": full_report, "operations": operation_list},
3333
ensure_ascii,
3434
)

cortexutils/worker.py

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

44
import codecs
55
import json
@@ -9,7 +9,7 @@
99
DEFAULT_SECRET_PHRASES = ("key", "password", "secret")
1010

1111

12-
class Worker(object):
12+
class Worker:
1313
READ_TIMEOUT = 3 # seconds
1414

1515
def __init__(self, job_directory, secret_phrases):
@@ -25,8 +25,9 @@ def __init__(self, job_directory, secret_phrases):
2525
self.secret_phrases = secret_phrases
2626
# Load input
2727
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:
28+
input_path = os.path.join(self.job_directory, "input", "input.json")
29+
if os.path.isfile(input_path):
30+
with open(input_path) as f_input:
3031
self._input = json.load(f_input)
3132
else:
3233
# If input file doesn't exist,
@@ -72,15 +73,9 @@ def __set_proxies(self):
7273
def __set_encoding():
7374
try:
7475
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")
76+
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer, "strict")
7977
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")
78+
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.buffer, "strict")
8479
except Exception:
8580
pass # nosec B110
8681

@@ -123,13 +118,10 @@ def __write_output(self, data, ensure_ascii=False):
123118
if self.job_directory is None:
124119
json.dump(data, sys.stdout, ensure_ascii=ensure_ascii)
125120
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:
121+
output_dir = os.path.join(self.job_directory, "output")
122+
os.makedirs(output_dir, exist_ok=True)
123+
output_path = os.path.join(output_dir, "output.json")
124+
with open(output_path, mode="w") as f_output:
133125
json.dump(data, f_output, ensure_ascii=ensure_ascii)
134126

135127
def get_data(self):

tests/test_suite_analyzer.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
#!/usr/bin/env python
2-
# coding: utf-8
2+
# -*- coding: utf-8 -*-
33

44
import os
55
import sys
66
import json
77
import unittest
88

9-
from io import open
109
from cortexutils.analyzer import Analyzer
1110

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
11+
from io import StringIO
1712

1813

1914
def load_test_fixture(fixture_path):
20-
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+
tests_dir = os.path.dirname(os.path.abspath(__file__))
16+
file_path = os.path.join(tests_dir, fixture_path)
17+
with open(file_path) as fixture_file:
18+
input = fixture_file.read()
2419
sys.stdin = StringIO(input)
2520
sys.stdout = StringIO()
2621

2722

2823
class TestMinimalConfig(unittest.TestCase):
2924
def setUp(self):
30-
load_test_fixture("fixtures/test-minimal-config.json")
25+
fixture_path = os.path.join("fixtures", "test-minimal-config.json")
26+
load_test_fixture(fixture_path)
3127
self.analyzer = Analyzer()
3228

3329
def test_default_config(self):
@@ -49,7 +45,8 @@ def test_params_data(self):
4945

5046
class TestProxyConfig(unittest.TestCase):
5147
def setUp(self):
52-
load_test_fixture("fixtures/test-proxy-config.json")
48+
fixture_path = os.path.join("fixtures", "test-proxy-config.json")
49+
load_test_fixture(fixture_path)
5350
self.analyzer = Analyzer()
5451

5552
def test_proxy_config(self):
@@ -64,7 +61,8 @@ def test_proxy_config(self):
6461

6562
class TestTlpConfig(unittest.TestCase):
6663
def setUp(self):
67-
load_test_fixture("fixtures/test-tlp-config.json")
64+
fixture_path = os.path.join("fixtures", "test-tlp-config.json")
65+
load_test_fixture(fixture_path)
6866
self.analyzer = Analyzer()
6967

7068
def test_check_tlp_disabled(self):
@@ -95,7 +93,8 @@ def test_check_tlp_ok(self):
9593

9694
class TestErrorResponse(unittest.TestCase):
9795
def setUp(self):
98-
load_test_fixture("fixtures/test-error-response.json")
96+
fixture_path = os.path.join("fixtures", "test-error-response.json")
97+
load_test_fixture(fixture_path)
9998
self.analyzer = Analyzer()
10099

101100
def test_error_response(self):
@@ -130,7 +129,8 @@ def test_error_response(self):
130129

131130
class TestReportResponse(unittest.TestCase):
132131
def setUp(self):
133-
load_test_fixture("fixtures/test-report-response.json")
132+
fixture_path = os.path.join("fixtures", "test-report-response.json")
133+
load_test_fixture(fixture_path)
134134
self.analyzer = Analyzer()
135135

136136
def test_report_response(self):

tests/test_suite_extractor.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
24
"""
35
This contains the unit tests for the extractor.
46
"""
@@ -21,13 +23,6 @@ def test_single_fqdn(self):
2123
"FQDN single string: wrong data type.",
2224
)
2325

24-
def test_single_fqdn_as_unicode(self):
25-
self.assertEqual(
26-
self.extractor.check_string(value="www.google.de"),
27-
"fqdn",
28-
"FQDN single string: wrong data type.",
29-
)
30-
3126
def test_single_domain(self):
3227
self.assertEqual(
3328
self.extractor.check_string(value="google.de"),

tests/test_suite_integration.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#!/usr/bin/env python
2-
# coding: utf-8
2+
# -*- coding: utf-8 -*-
3+
34
import json
45
import unittest
56
import sys
67

78
from cortexutils.analyzer import Analyzer
89

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
10+
from io import StringIO
1411

1512

1613
class AnalyzerExtractorOutputTest(unittest.TestCase):

0 commit comments

Comments
 (0)