forked from jaketae/deep-malware-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dasmalwerk.py
53 lines (41 loc) · 1.4 KB
/
dasmalwerk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import argparse
import logging
import os
from io import BytesIO
from typing import Dict
from zipfile import BadZipFile, ZipFile
import requests
from bs4 import BeautifulSoup
logger = logging.getLogger(__name__)
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--save_dir", type=str, default="raw/dasmalwerk")
args = parser.parse_args()
return args
def get_hrefs() -> Dict[str, str]:
html = requests.get("https://das-malwerk.herokuapp.com").text
soup = BeautifulSoup(html, "html.parser")
rows = soup.find_all("tr")[1:]
malware2href = {}
for row in rows:
a_tags = row.find_all("a")
file_hash = a_tags[1].text
href = a_tags[0]["href"]
malware2href[file_hash] = href
return malware2href
def download(malware2href: Dict[str, str], save_dir: str) -> None:
os.makedirs(save_dir, exist_ok=True)
for file_hash, href in malware2href.items():
source = requests.get(href, allow_redirects=True)
try:
with ZipFile(BytesIO(source.content)) as f:
f.extractall(path=save_dir, pwd=b"infected")
except BadZipFile:
logger.debug(f"Skipping {file_hash}")
continue
def main(args: argparse.Namespace) -> None:
malware2href = get_hrefs()
download(malware2href, args.save_dir)
if __name__ == "__main__":
args = get_args()
main(args)