diff --git a/your_daily_dose_malware/backends/malshare.py b/your_daily_dose_malware/backends/malshare.py index b53f5e0..fb74732 100644 --- a/your_daily_dose_malware/backends/malshare.py +++ b/your_daily_dose_malware/backends/malshare.py @@ -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__":