15
15
SELECTED_FILES_AND_CONTENTS = []
16
16
17
17
18
-
19
18
class Malshare :
20
-
21
19
@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 ):
23
21
sha256_ids = list ()
24
22
for id_names in fetched_hashes :
25
23
sha256_id = id_names ["sha256" ]
26
24
sha256_ids .append (sha256_id )
27
25
return sha256_ids
28
26
29
27
@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 ):
33
29
"""
34
- if limit == 0 :
30
+ Malshare treshold
31
+ """
32
+ if limit == 0 :
35
33
return hashes
36
34
else :
37
35
treshold = hashes [:limit ]
38
36
return treshold
39
37
40
-
41
38
@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
+ ):
43
46
"""
44
47
Download selected malware samples
45
48
@@ -68,18 +71,20 @@ def gather_selected_malwares(cls,new_hashes: list[str], choosen_path: Path , pro
68
71
rich .print (f"[red]sha256_hash: { sha256_id [:6 ]} not found skipping" )
69
72
continue
70
73
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
+ )
72
77
continue
73
78
curr_time = dt .now ().date ().strftime ("%Y-%m-%d" )
74
79
malware_content = response .content
75
80
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 )
78
83
global SELECTED_FILES_AND_CONTENTS
79
84
SELECTED_FILES_AND_CONTENTS .append (path_and_content )
80
85
81
86
@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 ):
83
88
"""
84
89
Download selected malware samples
85
90
@@ -97,11 +102,12 @@ def download_selected_malwares(cls,new_hashes: list[str], progress: Progress ):
97
102
for malwares_path_and_content in SELECTED_FILES_AND_CONTENTS :
98
103
with open (file = malwares_path_and_content [0 ], mode = "wb" ) as f :
99
104
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 )
102
106
103
107
@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
+ ):
105
111
"""
106
112
Catch new malware samples since the last fecth from malshare.com to avoid duplicates
107
113
@@ -114,8 +120,8 @@ def catch_new_malwares(cls,hashes: list[str], choosen_path: Path ,current_progre
114
120
"""
115
121
new_hashes = []
116
122
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
+ )
119
125
for sha256_id in hashes :
120
126
if (choosen_path / f"malware_{ sha256_id [:6 ]} .zip" ).exists ():
121
127
continue
@@ -126,7 +132,7 @@ def catch_new_malwares(cls,hashes: list[str], choosen_path: Path ,current_progre
126
132
return new_hashes
127
133
128
134
@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 :
130
136
"""
131
137
Create directory and return path to save the downloaded malware samples
132
138
Args:
@@ -136,13 +142,15 @@ def create_dir_and_return_path(cls,str_path: str) -> Path:
136
142
Path: path to save the downloaded malware samples
137
143
"""
138
144
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"
141
147
Path (str_path ).mkdir (parents = True , exist_ok = True )
142
148
return Path (str_path )
143
149
144
150
@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
+ ):
146
154
"""
147
155
Scrap malshare.com and download the last 24 hours malware samples
148
156
@@ -154,9 +162,9 @@ def scrap_malshare(cls,choosen_path: str="", not_twins: bool = False, limit: int
154
162
None
155
163
"""
156
164
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 ,
160
168
) as progress :
161
169
if API_KEY is None or API_KEY == "" :
162
170
return None
@@ -168,7 +176,9 @@ def scrap_malshare(cls,choosen_path: str="", not_twins: bool = False, limit: int
168
176
)
169
177
response .raise_for_status ()
170
178
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
+ )
172
182
return None
173
183
hashes = response .json ()
174
184
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
177
187
downloaded_malwares_path = cls .create_dir_and_return_path (choosen_path )
178
188
with requests .Session () as session :
179
189
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
+ )
183
202
else :
184
203
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 )
0 commit comments