Skip to content

Commit 918965d

Browse files
Akshit MaheshwaryAkshit Maheshwary
authored andcommitted
shell-escaped user input and switched to requests lib to handle downloading of signatures
1 parent dc35536 commit 918965d

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

api_app/analyzers_manager/file_analyzers/capa_info.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
import subprocess
8+
from shlex import quote
89
from zipfile import ZipFile
910

1011
import requests
@@ -45,11 +46,17 @@ def _download_rules(cls, latest_version: str):
4546

4647
file_to_download = latest_version + ".zip"
4748
file_url = RULES_URL + file_to_download
48-
response = requests.get(file_url, stream=True)
49-
logger.info(f"Started downloading rules from {file_url}")
50-
with open(RULES_FILE, mode="wb+") as file:
51-
for chunk in response.iter_content(chunk_size=10 * 1024):
52-
file.write(chunk)
49+
try:
50+
51+
response = requests.get(file_url, stream=True)
52+
logger.info(f"Started downloading rules from {file_url}")
53+
with open(RULES_FILE, mode="wb+") as file:
54+
for chunk in response.iter_content(chunk_size=10 * 1024):
55+
file.write(chunk)
56+
57+
except Exception as e:
58+
logger.error(f"Failed to download rules with error: {e}")
59+
raise AnalyzerRunException("Failed to download rules")
5360

5461
logger.info(f"Rules have been successfully downloaded at {RULES_LOCATION}")
5562

@@ -61,28 +68,23 @@ def _download_signatures(cls) -> None:
6168
os.makedirs(SIGNATURE_LOCATION)
6269

6370
signatures_url = "https://api.github.com/repos/mandiant/capa/contents/sigs"
64-
response = requests.get(signatures_url)
65-
signatures_list = response.json()
66-
67-
for signature in signatures_list:
68-
try:
69-
subprocess.run(
70-
[
71-
"/usr/bin/wget",
72-
"-P",
73-
SIGNATURE_LOCATION,
74-
signature["download_url"],
75-
],
76-
check=True,
77-
capture_output=True,
78-
)
71+
try:
72+
response = requests.get(signatures_url)
73+
signatures_list = response.json()
7974

80-
except subprocess.CalledProcessError as e:
81-
stderr = e.stderr
82-
logger.error(f"Failed to download signature: {e}")
83-
raise AnalyzerRunException(
84-
f"Failed to update signatures due to error: {stderr}"
85-
)
75+
for signature in signatures_list:
76+
77+
filename = signature["name"]
78+
download_url = signature["download_url"]
79+
80+
sig_content = requests.get(download_url, stream=True)
81+
with open(filename, mode="wb") as file:
82+
for chunk in sig_content.iter_content(chunk_size=10 * 1024):
83+
file.write(chunk)
84+
85+
except Exception as e:
86+
logger.error(f"Failed to download signature: {e}")
87+
raise AnalyzerRunException("Failed to update signatures")
8688
logger.info("Successfully updated singatures")
8789

8890
@classmethod
@@ -132,7 +134,7 @@ def run(self):
132134
command.append("-s")
133135
command.append(SIGNATURE_LOCATION)
134136

135-
command.append(self.filepath)
137+
command.append(quote(self.filepath))
136138

137139
logger.info(f"Starting CAPA analysis for {self.filename}")
138140

api_app/analyzers_manager/file_analyzers/floss.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import subprocess
66
from json import dumps, loads
7+
from shlex import quote
78

89
from api_app.analyzers_manager.classes import DockerBasedAnalyzer, FileAnalyzer
910
from api_app.analyzers_manager.exceptions import AnalyzerRunException
@@ -44,7 +45,7 @@ def run(self):
4445
"--no",
4546
"static",
4647
"--",
47-
self.filepath,
48+
quote(self.filepath),
4849
],
4950
capture_output=True,
5051
text=True,

0 commit comments

Comments
 (0)