Skip to content

Commit e10870f

Browse files
committed
style: make baby flake happy
1 parent 4be6c07 commit e10870f

File tree

4 files changed

+96
-80
lines changed

4 files changed

+96
-80
lines changed

your_daily_dose_malware/__main__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from your_daily_dose_malware.commands import app
2-
app(prog_name="dd-run")
2+
3+
app(prog_name="dd-run")

your_daily_dose_malware/backends/malshare.py

+55-32
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,34 @@
1515
SELECTED_FILES_AND_CONTENTS = []
1616

1717

18-
1918
class Malshare:
20-
2119
@classmethod
22-
def parsing_malshare_sha256(cls,fetched_hashes:List[dict],progress: Progress):
20+
def parsing_malshare_sha256(cls, fetched_hashes: List[dict], progress: Progress):
2321
sha256_ids = list()
2422
for id_names in fetched_hashes:
2523
sha256_id = id_names["sha256"]
2624
sha256_ids.append(sha256_id)
2725
return sha256_ids
2826

2927
@classmethod
30-
def get_malshare_treshold(cls,hashes: list[dict], limit: int):
31-
"""
32-
Malshare treshold
28+
def get_malshare_treshold(cls, hashes: list[dict], limit: int):
3329
"""
34-
if limit == 0 :
30+
Malshare treshold
31+
"""
32+
if limit == 0:
3533
return hashes
3634
else:
3735
treshold = hashes[:limit]
3836
return treshold
3937

40-
4138
@classmethod
42-
def gather_selected_malwares(cls,new_hashes: list[str], choosen_path: Path , progress: Progress,req_session: requests.Session ):
39+
def gather_selected_malwares(
40+
cls,
41+
new_hashes: list[str],
42+
choosen_path: Path,
43+
progress: Progress,
44+
req_session: requests.Session,
45+
):
4346
"""
4447
Download selected malware samples
4548
@@ -68,18 +71,20 @@ def gather_selected_malwares(cls,new_hashes: list[str], choosen_path: Path , pro
6871
rich.print(f"[red]sha256_hash: {sha256_id[:6]} not found skipping")
6972
continue
7073
if json_response["query_status"] == "illegal_sha256_hash":
71-
rich.print(f"[red]Illegal SHA256 hash provided: {sha256_id[:6]} skipping")
74+
rich.print(
75+
f"[red]Illegal SHA256 hash provided: {sha256_id[:6]} skipping"
76+
)
7277
continue
7378
curr_time = dt.now().date().strftime("%Y-%m-%d")
7479
malware_content = response.content
7580
file_path = f"malware_{sha256_id[:6]}_{curr_time}-{str(malware_content[:4]).replace('\\','').replace("<!","").replace("<h","")}.zip"
76-
progress.update(dl_task, advance=100/len(new_hashes))
77-
path_and_content = ((choosen_path/file_path),malware_content)
81+
progress.update(dl_task, advance=100 / len(new_hashes))
82+
path_and_content = ((choosen_path / file_path), malware_content)
7883
global SELECTED_FILES_AND_CONTENTS
7984
SELECTED_FILES_AND_CONTENTS.append(path_and_content)
8085

8186
@classmethod
82-
def download_selected_malwares(cls,new_hashes: list[str], progress: Progress ):
87+
def download_selected_malwares(cls, new_hashes: list[str], progress: Progress):
8388
"""
8489
Download selected malware samples
8590
@@ -97,11 +102,12 @@ def download_selected_malwares(cls,new_hashes: list[str], progress: Progress ):
97102
for malwares_path_and_content in SELECTED_FILES_AND_CONTENTS:
98103
with open(file=malwares_path_and_content[0], mode="wb") as f:
99104
f.write(malwares_path_and_content[1])
100-
progress.update(dl_task, advance=100/total_file)
101-
105+
progress.update(dl_task, advance=100 / total_file)
102106

103107
@classmethod
104-
def catch_new_malwares(cls,hashes: list[str], choosen_path: Path ,current_progress: Progress ):
108+
def catch_new_malwares(
109+
cls, hashes: list[str], choosen_path: Path, current_progress: Progress
110+
):
105111
"""
106112
Catch new malware samples since the last fecth from malshare.com to avoid duplicates
107113
@@ -114,8 +120,8 @@ def catch_new_malwares(cls,hashes: list[str], choosen_path: Path ,current_progre
114120
"""
115121
new_hashes = []
116122
task = current_progress.add_task(
117-
"-[green]Checking existing malwares ...", total=len(hashes),start=False
118-
)
123+
"-[green]Checking existing malwares ...", total=len(hashes), start=False
124+
)
119125
for sha256_id in hashes:
120126
if (choosen_path / f"malware_{sha256_id[:6]}.zip").exists():
121127
continue
@@ -126,7 +132,7 @@ def catch_new_malwares(cls,hashes: list[str], choosen_path: Path ,current_progre
126132
return new_hashes
127133

128134
@classmethod
129-
def create_dir_and_return_path(cls,str_path: str) -> Path:
135+
def create_dir_and_return_path(cls, str_path: str) -> Path:
130136
"""
131137
Create directory and return path to save the downloaded malware samples
132138
Args:
@@ -136,13 +142,15 @@ def create_dir_and_return_path(cls,str_path: str) -> Path:
136142
Path: path to save the downloaded malware samples
137143
"""
138144
if str_path == "":
139-
(Path()/"Downloaded-Malwares").mkdir(parents=True, exist_ok=True)
140-
return (Path()/"Downloaded-Malwares")
145+
(Path() / "Downloaded-Malwares").mkdir(parents=True, exist_ok=True)
146+
return Path() / "Downloaded-Malwares"
141147
Path(str_path).mkdir(parents=True, exist_ok=True)
142148
return Path(str_path)
143149

144150
@classmethod
145-
def scrap_malshare(cls,choosen_path: str="", not_twins: bool = False, limit: int = 24):
151+
def scrap_malshare(
152+
cls, choosen_path: str = "", not_twins: bool = False, limit: int = 24
153+
):
146154
"""
147155
Scrap malshare.com and download the last 24 hours malware samples
148156
@@ -154,9 +162,9 @@ def scrap_malshare(cls,choosen_path: str="", not_twins: bool = False, limit: int
154162
None
155163
"""
156164
with Progress(
157-
SpinnerColumn(finished_text="[bold green]finished ✓[/bold green]"),
158-
*Progress.get_default_columns(),
159-
transient=True,
165+
SpinnerColumn(finished_text="[bold green]finished ✓[/bold green]"),
166+
*Progress.get_default_columns(),
167+
transient=True,
160168
) as progress:
161169
if API_KEY is None or API_KEY == "":
162170
return None
@@ -168,7 +176,9 @@ def scrap_malshare(cls,choosen_path: str="", not_twins: bool = False, limit: int
168176
)
169177
response.raise_for_status()
170178
except requests.RequestException as err:
171-
rich.print(f"[red]Malshare will be skipped. An unexpected error occurred: {err} ")
179+
rich.print(
180+
f"[red]Malshare will be skipped. An unexpected error occurred: {err} "
181+
)
172182
return None
173183
hashes = response.json()
174184
sliced_hashes = cls.get_malshare_treshold(hashes, limit)
@@ -177,11 +187,24 @@ def scrap_malshare(cls,choosen_path: str="", not_twins: bool = False, limit: int
177187
downloaded_malwares_path = cls.create_dir_and_return_path(choosen_path)
178188
with requests.Session() as session:
179189
if not_twins:
180-
new_hashes = cls.catch_new_malwares(sha256_ids, downloaded_malwares_path, progress)
181-
cls.gather_selected_malwares(new_hashes=new_hashes, choosen_path=downloaded_malwares_path, progress=progress,req_session=session)
182-
cls.download_selected_malwares(new_hashes=new_hashes, progress=progress)
190+
new_hashes = cls.catch_new_malwares(
191+
sha256_ids, downloaded_malwares_path, progress
192+
)
193+
cls.gather_selected_malwares(
194+
new_hashes=new_hashes,
195+
choosen_path=downloaded_malwares_path,
196+
progress=progress,
197+
req_session=session,
198+
)
199+
cls.download_selected_malwares(
200+
new_hashes=new_hashes, progress=progress
201+
)
183202
else:
184203
new_hashes = sha256_ids
185-
cls.gather_selected_malwares(new_hashes=new_hashes, choosen_path=downloaded_malwares_path, progress=progress,req_session=session)
186-
cls.download_selected_malwares(new_hashes, progress)
187-
204+
cls.gather_selected_malwares(
205+
new_hashes=new_hashes,
206+
choosen_path=downloaded_malwares_path,
207+
progress=progress,
208+
req_session=session,
209+
)
210+
cls.download_selected_malwares(new_hashes, progress)

your_daily_dose_malware/backends/malware_bazaar.py

+21-24
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
API_KEY = os.getenv("MALSHARE_API_KEY")
1111

1212

13-
14-
1513
class MalwareBazaar:
16-
1714
@classmethod
18-
def hundred_most_recent(cls,headers_info):
15+
def hundred_most_recent(cls, headers_info):
1916
with Progress() as progress:
2017
# TODO Pending Refactor this method to match all_most_recent style
2118
response = requests.post(
@@ -30,7 +27,9 @@ def hundred_most_recent(cls,headers_info):
3027
"[red]No selector provided. Please use either time or limit as selector",
3128
)
3229
if json_response["query_status"] == "unknown_selector":
33-
rich.print("[red]Selector unknown. Please use either time or limit as selector")
30+
rich.print(
31+
"[red]Selector unknown. Please use either time or limit as selector"
32+
)
3433
return
3534
if json_response["query_status"] == "no_results":
3635
rich.print("[red]Your query yield no results")
@@ -43,12 +42,12 @@ def hundred_most_recent(cls,headers_info):
4342
curr_time = dt.now().date().strftime("%Y-%m-%d")
4443
task = progress.add_task(
4544
" - [green]Downloading Malware Files...",
46-
total=len(json_response["data"])
45+
total=len(json_response["data"]),
4746
)
4847
for data in json_response["data"]:
4948
sha256_name = data["sha256_hash"]
5049
file_path = f"malware_{sha256_name[:6]}_{curr_time}.zip"
51-
if (downloaded_malwares_path/file_path).exists():
50+
if (downloaded_malwares_path / file_path).exists():
5251
progress.update(task, advance=1)
5352
continue
5453
response = requests.post(
@@ -57,13 +56,13 @@ def hundred_most_recent(cls,headers_info):
5756
headers=headers_info,
5857
)
5958
file_path = f"malware_{sha256_name[:6]}_{curr_time}.zip"
60-
path = downloaded_malwares_path/file_path
59+
path = downloaded_malwares_path / file_path
6160
with open(path.absolute(), "wb+") as f:
6261
f.write(response.content)
6362
progress.update(task, advance=1)
6463

6564
@classmethod
66-
def get_new_hashes(cls,malware_response: requests.Response):
65+
def get_new_hashes(cls, malware_response: requests.Response):
6766
with open("sha256_names.txt", "wb+") as f:
6867
f.write(malware_response.content)
6968
f.seek(0)
@@ -78,12 +77,10 @@ def get_new_hashes(cls,malware_response: requests.Response):
7877
...
7978

8079
@classmethod
81-
def all_most_recent(cls,headers_info):
82-
80+
def all_most_recent(cls, headers_info):
8381
with Progress() as progress:
8482
response = requests.post(
85-
"https://bazaar.abuse.ch/export/txt/sha256/recent",
86-
headers=headers_info
83+
"https://bazaar.abuse.ch/export/txt/sha256/recent", headers=headers_info
8784
)
8885
new_hashes = cls.get_new_hashes(malware_response=response)
8986
rich.print(f"- [green]Malware Bazaar: {len(new_hashes)} Samples")
@@ -94,16 +91,14 @@ def all_most_recent(cls,headers_info):
9491
sha256_hashes = open("sha256_names.txt", "r").readlines()
9592
print(sha256_hashes)
9693
Path("sha256_names.txt").unlink()
97-
task = progress.add_task(
98-
" - [green]Downloading Malware Files..."
99-
)
94+
task = progress.add_task(" - [green]Downloading Malware Files...")
10095
Path("Downloaded-Malwares").mkdir(exist_ok=True)
10196
for sha256_hash in sha256_hashes:
10297
curr_time = dt.now().date().strftime("%Y-%m-%d")
10398
downloaded_malwares_path = Path("Downloaded-Malwares").absolute()
10499
file_path = f"malware_{sha256_hash[:6]}_{curr_time}.zip"
105-
if (downloaded_malwares_path/file_path).exists():
106-
progress.update(task, advance=100/len(sha256_hashes))
100+
if (downloaded_malwares_path / file_path).exists():
101+
progress.update(task, advance=100 / len(sha256_hashes))
107102
continue
108103
response = requests.post(
109104
"https://mb-api.abuse.ch/api/v1/",
@@ -113,21 +108,25 @@ def all_most_recent(cls,headers_info):
113108
if response.status_code == 502:
114109
json_response = response.json()
115110
if json_response["query_status"] == "file_not_found":
116-
rich.print(f" [red]sha256_hash: {sha256_hash[:6]} not found skipping")
111+
rich.print(
112+
f" [red]sha256_hash: {sha256_hash[:6]} not found skipping"
113+
)
117114
progress.update(task, advance=1)
118115
continue
119116
if json_response["query_status"] == "illegal_sha256_hash":
120-
rich.print(f" [red]Illegal SHA256 hash provided: {sha256_hash[:6]} skipping")
117+
rich.print(
118+
f" [red]Illegal SHA256 hash provided: {sha256_hash[:6]} skipping"
119+
)
121120
progress.update(task, advance=1)
122121
continue
123122
targeted_file = f"malware_{sha256_hash[:6]}_{curr_time}.zip"
124-
final_path = downloaded_malwares_path/targeted_file
123+
final_path = downloaded_malwares_path / targeted_file
125124
with open(final_path, "wb") as f:
126125
f.write(response.content)
127126
progress.update(task, advance=1)
128127

129128
@classmethod
130-
def scrap_malware_bazaar(cls,hundred_recent:bool=False):
129+
def scrap_malware_bazaar(cls, hundred_recent: bool = False):
131130
if API_KEY == "":
132131
rich.print("[red]Please set MALWARE_BAZAAR_API_KEY")
133132
return None
@@ -138,5 +137,3 @@ def scrap_malware_bazaar(cls,hundred_recent:bool=False):
138137
else:
139138
cls.all_most_recent(headers)
140139
return typer.Exit(code=1)
141-
142-

your_daily_dose_malware/commands.py

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Annotated
2-
import typer
2+
import typer
33
from pathlib import Path
44
from rich.console import Console
55

@@ -10,6 +10,7 @@
1010

1111
app = typer.Typer()
1212

13+
1314
@app.callback()
1415
def callback():
1516
console = Console()
@@ -19,23 +20,15 @@ def callback():
1920

2021
@app.command()
2122
def malshare(
22-
path : str = typer.Option(
23-
None, help="path to save the downloaded malware samples"
24-
),
25-
not_twins : bool = typer.Option(
23+
path: str = typer.Option(None, help="path to save the downloaded malware samples"),
24+
not_twins: bool = typer.Option(
2625
False, help="do not download already downloaded malware samples"
2726
),
28-
limit : int = typer.Option(
29-
0, help="number of malware samples to download"
30-
),
27+
limit: int = typer.Option(0, help="number of malware samples to download"),
3128
):
3229
"download the last 24 hours malware samples from malshare.com"
3330
if path is not None and Path(path).exists():
34-
Malshare.scrap_malshare(
35-
choosen_path=path,
36-
not_twins=not_twins,
37-
limit=limit
38-
)
31+
Malshare.scrap_malshare(choosen_path=path, not_twins=not_twins, limit=limit)
3932
else:
4033
Malshare.scrap_malshare(limit=limit)
4134

@@ -45,26 +38,28 @@ def malware_bazaar(
4538
hundred: Annotated[
4639
bool,
4740
typer.Option(
48-
"--hundred",
49-
"-h",
50-
is_flag=True,
51-
),
52-
]=False,
41+
"--hundred",
42+
"-h",
43+
is_flag=True,
44+
),
45+
] = False,
5346
most_recent: Annotated[
5447
bool,
5548
"--most-recent",
5649
"-mr",
5750
typer.Option(
58-
is_flag=True,
59-
)
60-
]=False
51+
is_flag=True,
52+
),
53+
] = False,
6154
):
6255
"""
63-
Scrap malshare.com and download the last 24 hours malware samples
56+
Scrap malshare.com and download the last 24 hours malware samples
6457
"""
6558
if hundred is True and most_recent is False:
6659
MalwareBazaar.scrap_malware_bazaar(hundred_recent=True)
6760
elif hundred is False and most_recent is True:
6861
MalwareBazaar.scrap_malware_bazaar(hundred_recent=False)
6962
else:
70-
raise typer.BadParameter("You must to choose either <hundred> or <most-recent> options")
63+
raise typer.BadParameter(
64+
"You must to choose either <hundred> or <most-recent> options"
65+
)

0 commit comments

Comments
 (0)