Skip to content

Commit

Permalink
move try-except block at the right place
Browse files Browse the repository at this point in the history
  • Loading branch information
poneoneo committed Jun 14, 2024
1 parent c088335 commit d67cf1d
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions your_daily_dose_malware/backends/malshare.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,43 @@ def scrap_malshare():
)
response.raise_for_status()
hashes = response.json()
except Exception as err:
raise err
try:
rich.print(f"[green] - Malshare: {len(hashes)} Samples")
sha256_ids = [hashe["sha256"] for hashe in hashes]
task = progress.add_task(
" - [green]Downloading Malware Files...", total=len(sha256_ids)
)

Path("Downloaded-Malwares").mkdir(exist_ok=True)
downloaded_malwares_path = Path("Downloaded-Malwares").absolute()
for sha256_id in sha256_ids:
if (downloaded_malwares_path / f"malware_{sha256_id[:6]}.zip").exists():
progress.update(task, advance=1)
continue
except requests.RequestException as err:
rich.print(f"[red] An error occurred: {err}")
rich.print(f"[green] - Malshare: {len(hashes)} Samples")
sha256_ids = [hashe["sha256"] for hashe in hashes]
task = progress.add_task(
" - [green]Downloading Malware Files...", total=len(sha256_ids)
)
Path("Downloaded-Malwares").mkdir(exist_ok=True)
downloaded_malwares_path = Path("Downloaded-Malwares").absolute()
for sha256_id in sha256_ids:
if (downloaded_malwares_path / f"malware_{sha256_id[:6]}.zip").exists():
progress.update(task, advance=1)
continue
try:
response = requests.post(
"https://malshare.com/api.php",
params={"api_key": api_key, "action": "getfile", "hash": sha256_id},
verify=True,
)
response.raise_for_status()
curr_time = dt.now().date().strftime("%Y-%m-%d")
if response.status_code == 502:
json_response = response.json()
if json_response["query_status"] == "file_not_found":
rich.print(f" [red]sha256_hash: {sha256_id[:6]} not found skipping")
continue
if json_response["query_status"] == "illegal_sha256_hash":
rich.print(f" [red]Illegal SHA256 hash provided: {sha256_id[:6]} skipping")
continue
file_path = f"malware_{sha256_id[:6]}_{curr_time}.zip"
final_path = (downloaded_malwares_path/file_path)
with open(file=final_path, mode="wb") as f:
f.write(response.content)
except requests.RequestException as err:
raise err
curr_time = dt.now().date().strftime("%Y-%m-%d")
if response.status_code == 502:
json_response = response.json()
if json_response["query_status"] == "file_not_found":
rich.print(f" [red]sha256_hash: {sha256_id[:6]} not found skipping")
continue
if json_response["query_status"] == "illegal_sha256_hash":
rich.print(f" [red]Illegal SHA256 hash provided: {sha256_id[:6]} skipping")
continue
file_path = f"malware_{sha256_id[:6]}_{curr_time}.zip"
final_path = (downloaded_malwares_path/file_path)
with open(file=final_path, mode="wb") as f:
f.write(response.content)
progress.update(task, advance=1)
except Exception as err:
raise err


if __name__ == "__main__":
Expand Down

0 comments on commit d67cf1d

Please sign in to comment.