Spaces:
Paused
Paused
Update the CyberForge ML service
Browse files58 unmodified lines5960616263645 unmodified lines707172737475148 unmodified lines224225226227228229312 unmodified lines5425435445455465475 unmodified lines5535545555565575584 unmodified lines5635645655665675685 unmodified lines5745755765775785794 unmodified lines58458558658758858959059159258 unmodified linesURL_COLLECTION = "bgspaditya/byt-malicious-url-treatment" # ~640k URLs; used for its malware URLs and some benign onesPHISHING_REFERENCE = "pirocheto/phishing-url" # 11k balanced full URLs, CC-BY-4.0, with its own test splitPOPULAR_SITES = "phl-ldm/tranco_top_million" # landing pages of top-million sites, CC-BY-4.0 (one small file of it)ATTACK_PAYLOADS = "shengqin/web-attacks" # SQL-injection and XSS payloads, and normal strings# The Tranco list of most-visited sites, by its permanent ID (the list the# popular-sites crawl above was made from), for the domain-popularity feature.5 unmodified lines"popular_sample": 60000, # landing pages of popular sites"phishing_sample": 120000,"malware_sample": 30000,"attack_benign_sample": 40000,"test_fraction": 0.2,"max_iter": 300,148 unmodified linesreturn outdef load_payloads(token: Optional[str]) -> Dict[str, List[str]]:paths = _download(ATTACK_PAYLOADS, lambda name: name in ("train.csv", "validate.csv", "test.csv"), token)frame = pd.concat([pd.read_csv(path) for path in paths], ignore_index=True)312 unmodified linescollection = load_url_collection(token)reference = load_phishing_reference(token)popular = load_popular_sites(token)payloads = load_payloads(token) if "web_attack_detection" in wanted else {"attack": [], "normal": []}reputation = load_popular_domains(settings["popular_domains"], popular)global POPULAR5 unmodified linesPHISHING_REFERENCE: {"revision": _revision(PHISHING_REFERENCE, token),"rows": {split: {status: len(urls) for status, urls in parts.items()} for split, parts in reference.items()}},POPULAR_SITES: {"revision": _revision(POPULAR_SITES, token), "rows": {"landing pages": len(popular)}},ATTACK_PAYLOADS: {"revision": _revision(ATTACK_PAYLOADS, token), "rows": {kind: len(texts) for kind, texts in payloads.items()}},}4 unmodified lines# URLs of the reference test split must not be trained on.reference_test = {bare_url(url).lower() for part in reference["test"].values() for url in part}keep = lambda urls: [url for url in urls if bare_url(url).lower() not in reference_test] # noqa: E731# Benign URLs of several shapes, so that no model can tell benign from5 unmodified lines"popular sites": keep(sample(popular, settings["popular_sample"])),"reference, legitimate": keep(reference["train"]["legitimate"]),}all_benign = [url for urls in benign.values() for url in urls]results: Dict[str, Dict] = {}4 unmodified lines"phishing collection, phishing": keep(sample(phishing["phishing"], settings["phishing_sample"])),"reference, phishing": keep(reference["train"]["phishing"]),}results["phishing_detection"] = train_classifier("phishing_detection", positives, benign, lexical, settings,extra_tests={PHISHING_REFERENCE + " (test split)": {"negatives": reference["test"]["legitimate"], "positives": reference["test"]["phishing"]}})if "malware_detection" in wanted:progress("training malware_detection")58 unmodified lines59606162636465665 unmodified lines72737475767778798081148 unmodified lines230231232233234235236237238239240241242243244245246247248249250251252253312 unmodified lines5665675685695705715725735745755765775 unmodified lines5835845855865875885895905914 unmodified lines5965975985996006016025 unmodified lines6086096106116126136146154 unmodified lines62062162262362462562662762862963063163263363463563663763863964064158 unmodified linesURL_COLLECTION = "bgspaditya/byt-malicious-url-treatment" # ~640k URLs; used for its malware URLs and some benign onesPHISHING_REFERENCE = "pirocheto/phishing-url" # 11k balanced full URLs, CC-BY-4.0, with its own test splitPOPULAR_SITES = "phl-ldm/tranco_top_million" # landing pages of top-million sites, CC-BY-4.0 (one small file of it)PHISHING_FEDERATED = "flwrlabs/fed-phishing-urls" # ~1.1M URLs merged and de-duplicated from two public collections, Apache-2.0, with a test splitPHISHING_PAGES = "it4lia/PhiUSIIL_cleaned" # PhiUSIIL (UCI): ~100k phishing URLs reported Oct 2022 to May 2023, CC-BY-4.0ATTACK_PAYLOADS = "shengqin/web-attacks" # SQL-injection and XSS payloads, and normal strings# The Tranco list of most-visited sites, by its permanent ID (the list the# popular-sites crawl above was made from), for the domain-popularity feature.5 unmodified lines"popular_sample": 60000, # landing pages of popular sites"phishing_sample": 120000,"malware_sample": 30000,# Added November 2026. 0 leaves a source out, so a run can be compared with and without it."federated_benign_sample": 100000,"federated_phishing_sample": 150000,"pages_phishing_sample": 80000, # PhiUSIIL phishing only: its legitimate rows are all bare home pages"attack_benign_sample": 40000,"test_fraction": 0.2,"max_iter": 300,148 unmodified linesreturn outdef load_phishing_federated(token: Optional[str]) -> Dict[str, Dict[str, List[str]]]:"""{"train": {"benign", "phishing"}, "test": {...}} from the collection's own splits."""out = {}for split in ("train", "test"):path = _download(PHISHING_FEDERATED, lambda name, split=split: name.startswith(f"data/{split}-") and name.endswith(".parquet"), token)[0]frame = pd.read_parquet(path, columns=["url", "label"])out[split] = {"benign": clean_urls(frame.loc[frame["label"] == 0, "url"].tolist()),"phishing": clean_urls(frame.loc[frame["label"] == 1, "url"].tolist())}return outdef load_phishing_pages(token: Optional[str]) -> List[str]:"""The phishing URLs of PhiUSIIL (label 0 there means phishing)."""path = _download(PHISHING_PAGES, lambda name: name.endswith("metadata.parquet"), token)[0]frame = pd.read_parquet(path, columns=["phiusiil_url", "label_int"])return clean_urls(frame.loc[frame["label_int"] == 0, "phiusiil_url"].tolist())def load_payloads(token: Optional[str]) -> Dict[str, List[str]]:paths = _download(ATTACK_PAYLOADS, lambda name: name in ("train.csv", "validate.csv", "test.csv"), token)frame = pd.concat([pd.read_csv(path) for path in paths], ignore_index=True)312 unmodified linescollection = load_url_collection(token)reference = load_phishing_reference(token)popular = load_popular_sites(token)use_federated = settings["federated_benign_sample"] > 0 or settings["federated_phishing_sample"] > 0empty = {"benign": [], "phishing": []}# The federated collection's test split is always read: it is a second outside# yardstick, measured the same way whether or not its train split is used.federated = load_phishing_federated(token) if "phishing_detection" in wanted or use_federated else {"train": empty, "test": empty}pages = load_phishing_pages(token) if settings["pages_phishing_sample"] > 0 and "phishing_detection" in wanted else []payloads = load_payloads(token) if "web_attack_detection" in wanted else {"attack": [], "normal": []}reputation = load_popular_domains(settings["popular_domains"], popular)global POPULAR5 unmodified linesPHISHING_REFERENCE: {"revision": _revision(PHISHING_REFERENCE, token),"rows": {split: {status: len(urls) for status, urls in parts.items()} for split, parts in reference.items()}},POPULAR_SITES: {"revision": _revision(POPULAR_SITES, token), "rows": {"landing pages": len(popular)}},PHISHING_FEDERATED: {"revision": _revision(PHISHING_FEDERATED, token),"rows": {split: {kind: len(urls) for kind, urls in parts.items()} for split, parts in federated.items()}},PHISHING_PAGES: {"revision": _revision(PHISHING_PAGES, token), "rows": {"phishing": len(pages)}},ATTACK_PAYLOADS: {"revision": _revision(ATTACK_PAYLOADS, token), "rows": {kind: len(texts) for kind, texts in payloads.items()}},}4 unmodified lines# URLs of the reference test split must not be trained on.reference_test = {bare_url(url).lower() for part in reference["test"].values() for url in part}reference_test |= {bare_url(url).lower() for part in federated["test"].values() for url in part}keep = lambda urls: [url for url in urls if bare_url(url).lower() not in reference_test] # noqa: E731# Benign URLs of several shapes, so that no model can tell benign from5 unmodified lines"popular sites": keep(sample(popular, settings["popular_sample"])),"reference, legitimate": keep(reference["train"]["legitimate"]),}if settings["federated_benign_sample"] > 0:benign["federated collection, benign"] = keep(sample(federated["train"]["benign"], settings["federated_benign_sample"]))all_benign = [url for urls in benign.values() for url in urls]results: Dict[str, Dict] = {}4 unmodified lines"phishing collection, phishing": keep(sample(phishing["phishing"], settings["phishing_sample"])),"reference, phishing": keep(reference["train"]["phishing"]),}if settings["federated_phishing_sample"] > 0:positives["federated collection, phishing"] = keep(sample(federated["train"]["phishing"], settings["federated_phishing_sample"]))if pages:positives["PhiUSIIL, phishing"] = keep(sample(pages, settings["pages_phishing_sample"]))# Outside test sets must not share a site with the training rows either,# or a memorised site would count as a detection.trained_sites = {registered_domain(url) for urls in list(positives.values()) + list(benign.values()) for url in urls}unseen = lambda urls: [url for url in urls if registered_domain(url) not in trained_sites] # noqa: E731results["phishing_detection"] = train_classifier("phishing_detection", positives, benign, lexical, settings,extra_tests={PHISHING_REFERENCE + " (test split)": {"negatives": reference["test"]["legitimate"], "positives": reference["test"]["phishing"]},PHISHING_FEDERATED + " (test split)": {"negatives": federated["test"]["benign"], "positives": federated["test"]["phishing"]},PHISHING_FEDERATED + " (test split, sites not in the training rows)": {"negatives": unseen(federated["test"]["benign"]), "positives": unseen(federated["test"]["phishing"])},})if "malware_detection" in wanted:progress("training malware_detection")
trainer.py+50-1