|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import requests |
| 4 | +import vt |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from github import Github |
| 8 | + |
| 9 | +# Download release assets, scan with VirusTotal, update README |
| 10 | + |
| 11 | +def main(): |
| 12 | + vt_key = os.getenv('VT_API_KEY') |
| 13 | + gh_token = os.getenv('GITHUB_TOKEN') |
| 14 | + repo_name = os.getenv('REPOSITORY_TO_SCAN') |
| 15 | + exclude_names = set(n.strip() for n in os.getenv('EXCLUDE_NAMES', '').split(',') if n.strip()) |
| 16 | + prefixes = [p for p in os.getenv('EXCLUDE_PREFIXES', '').split(',') if p] |
| 17 | + |
| 18 | + if not vt_key or not gh_token or not repo_name: |
| 19 | + print('ERROR: VT_API_KEY, GITHUB_TOKEN, and REPOSITORY_TO_SCAN must be set') |
| 20 | + sys.exit(1) |
| 21 | + |
| 22 | + vt_client = vt.Client(vt_key) |
| 23 | + gh = Github(gh_token) |
| 24 | + repo = gh.get_repo(repo_name) |
| 25 | + |
| 26 | + results = [] |
| 27 | + releases = requests.get( |
| 28 | + f'https://api.github.com/repos/{repo_name}/releases', |
| 29 | + headers={'Authorization': f'token {gh_token}', 'Accept': 'application/vnd.github.v3+json'} |
| 30 | + ).json() |
| 31 | + |
| 32 | + for release in releases: |
| 33 | + for asset in release.get('assets', []): |
| 34 | + name = asset.get('name') |
| 35 | + if not name or name in exclude_names or any(name.startswith(pref) for pref in prefixes): |
| 36 | + print(f"Skipping {name}") |
| 37 | + continue |
| 38 | + |
| 39 | + url = asset.get('browser_download_url') |
| 40 | + if not url: |
| 41 | + continue |
| 42 | + |
| 43 | + print(f"Downloading and scanning {name}...") |
| 44 | + try: |
| 45 | + with tempfile.NamedTemporaryFile(delete=False) as tmp_file: |
| 46 | + download = requests.get(url, stream=True) |
| 47 | + for chunk in download.iter_content(chunk_size=8192): |
| 48 | + tmp_file.write(chunk) |
| 49 | + tmp_file_path = tmp_file.name |
| 50 | + |
| 51 | + with open(tmp_file_path, 'rb') as f: |
| 52 | + analysis = vt_client.scan_file(f, wait_for_completion=True) |
| 53 | + stats = analysis.data.attributes.stats |
| 54 | + results.append((name, |
| 55 | + stats.get('malicious', 0), |
| 56 | + stats.get('suspicious', 0), |
| 57 | + stats.get('undetected', 0))) |
| 58 | + os.remove(tmp_file_path) |
| 59 | + except Exception as e: |
| 60 | + print(f"Failed to scan {name}: {e}") |
| 61 | + |
| 62 | + vt_client.close() |
| 63 | + |
| 64 | + if not results: |
| 65 | + print("No scan results to update.") |
| 66 | + return |
| 67 | + |
| 68 | + lines = [ |
| 69 | + '# VirusTotal Scan Results', |
| 70 | + '', |
| 71 | + '| Filename | Malicious | Suspicious | Undetected |', |
| 72 | + '| --- | --- | --- | --- |' |
| 73 | + ] |
| 74 | + for name, mal, sus, und in results: |
| 75 | + lines.append(f"| {name} | {mal} | {sus} | {und} |") |
| 76 | + content = '\n'.join(lines) |
| 77 | + |
| 78 | + contents = repo.get_contents('README.md') |
| 79 | + repo.update_file( |
| 80 | + contents.path, |
| 81 | + 'chore: update README with VT scan results', |
| 82 | + content, |
| 83 | + contents.sha |
| 84 | + ) |
| 85 | + print("README.md updated.") |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + main() |
0 commit comments