reverse-skills-max: initial public release (restored, redacted, hardened)
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to the Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by the Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding any notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. Please do not remove or change
|
||||
the license header comment from a contributed file except when
|
||||
necessary.
|
||||
|
||||
Copyright 2026 mukul975
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,182 @@
|
||||
---
|
||||
name: performing-automated-malware-analysis-with-cape
|
||||
description: Deploy and operate the CAPEv2 malware sandbox (a Cuckoo derivative) to run samples in a
|
||||
monitored Windows guest VM, capturing behavioral signatures, dropped files, PCAP network traffic,
|
||||
and family-specific configuration extraction (e.g. Emotet, TrickBot, Cobalt Strike) via
|
||||
cape-parsers. Use when a suspicious file or payload needs automated dynamic analysis, anti-evasion
|
||||
debugger tricks, or config/payload extraction.
|
||||
domain: cybersecurity
|
||||
subdomain: malware-analysis
|
||||
tags:
|
||||
- cape
|
||||
- sandbox
|
||||
- automated-analysis
|
||||
- malware-analysis
|
||||
- behavioral-analysis
|
||||
- payload-extraction
|
||||
- cuckoo
|
||||
version: '1.0'
|
||||
author: mahipal
|
||||
license: Apache-2.0
|
||||
nist_csf:
|
||||
- DE.AE-02
|
||||
- RS.AN-03
|
||||
- ID.RA-01
|
||||
- DE.CM-01
|
||||
mitre_attack:
|
||||
- T1027
|
||||
- T1055
|
||||
- T1140
|
||||
- T1497
|
||||
- T1070
|
||||
---
|
||||
# Performing Automated Malware Analysis with CAPE
|
||||
|
||||
## Overview
|
||||
|
||||
CAPE (Config And Payload Extraction) is an open-source malware sandbox derived from Cuckoo that automates behavioral analysis, payload dumping, and configuration extraction. CAPEv2 features API hooking for behavioral instrumentation, captures files created/modified/deleted during execution, records network traffic in PCAP format, and includes 70+ custom configuration extractors (cape-parsers) for families like Emotet, TrickBot, Cobalt Strike, AsyncRAT, and Rhadamanthys. The signature system includes 1000+ behavioral signatures detecting evasion techniques, persistence, credential theft, and ransomware behavior. CAPE's debugger enables dynamic anti-evasion bypasses combining debugger actions within YARA signatures. Recommended deployment: Ubuntu LTS host with Windows 10 21H2 guest VM.
|
||||
|
||||
|
||||
## When to Use
|
||||
|
||||
- When conducting security assessments that involve performing automated malware analysis with cape
|
||||
- When following incident response procedures for related security events
|
||||
- When performing scheduled security testing or auditing activities
|
||||
- When validating security controls through hands-on testing
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Ubuntu 22.04 LTS server (8+ CPU cores, 32GB+ RAM, 500GB+ SSD)
|
||||
- KVM/QEMU virtualization support
|
||||
- Windows 10 21H2 guest image
|
||||
- Python 3.9+ with CAPEv2 dependencies
|
||||
- Network configuration for isolated analysis network
|
||||
|
||||
## Workflow
|
||||
|
||||
### Step 1: Submit and Analyze Samples via API
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
"""CAPE sandbox API client for automated malware submission and analysis."""
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class CAPEClient:
|
||||
def __init__(self, base_url="http://localhost:8000", api_token=None):
|
||||
self.base_url = base_url.rstrip("/")
|
||||
self.headers = {}
|
||||
if api_token:
|
||||
self.headers["Authorization"] = f"Token {api_token}"
|
||||
|
||||
def submit_file(self, filepath, options=None):
|
||||
"""Submit a file for analysis."""
|
||||
url = f"{self.base_url}/apiv2/tasks/create/file/"
|
||||
files = {"file": open(filepath, "rb")}
|
||||
data = options or {}
|
||||
data.setdefault("timeout", 120)
|
||||
data.setdefault("enforce_timeout", False)
|
||||
|
||||
resp = requests.post(url, files=files, data=data, headers=self.headers)
|
||||
resp.raise_for_status()
|
||||
result = resp.json()
|
||||
task_id = result.get("data", {}).get("task_ids", [None])[0]
|
||||
print(f"[+] Submitted {filepath} -> Task ID: {task_id}")
|
||||
return task_id
|
||||
|
||||
def get_status(self, task_id):
|
||||
"""Check task analysis status."""
|
||||
url = f"{self.base_url}/apiv2/tasks/status/{task_id}/"
|
||||
resp = requests.get(url, headers=self.headers)
|
||||
return resp.json().get("data", "unknown")
|
||||
|
||||
def wait_for_completion(self, task_id, poll_interval=15, max_wait=600):
|
||||
"""Wait for analysis to complete."""
|
||||
elapsed = 0
|
||||
while elapsed < max_wait:
|
||||
status = self.get_status(task_id)
|
||||
if status == "reported":
|
||||
print(f"[+] Task {task_id} completed")
|
||||
return True
|
||||
time.sleep(poll_interval)
|
||||
elapsed += poll_interval
|
||||
print(f" Waiting... ({elapsed}s, status: {status})")
|
||||
return False
|
||||
|
||||
def get_report(self, task_id):
|
||||
"""Retrieve full analysis report."""
|
||||
url = f"{self.base_url}/apiv2/tasks/get/report/{task_id}/"
|
||||
resp = requests.get(url, headers=self.headers)
|
||||
return resp.json()
|
||||
|
||||
def get_config(self, task_id):
|
||||
"""Get extracted malware configuration."""
|
||||
report = self.get_report(task_id)
|
||||
configs = report.get("CAPE", {}).get("configs", [])
|
||||
return configs
|
||||
|
||||
def get_dropped_files(self, task_id):
|
||||
"""List files dropped during analysis."""
|
||||
report = self.get_report(task_id)
|
||||
return report.get("dropped", [])
|
||||
|
||||
def get_network_iocs(self, task_id):
|
||||
"""Extract network IOCs from analysis."""
|
||||
report = self.get_report(task_id)
|
||||
network = report.get("network", {})
|
||||
iocs = {
|
||||
"dns": [d.get("request") for d in network.get("dns", [])],
|
||||
"http": [h.get("uri") for h in network.get("http", [])],
|
||||
"tcp": [f"{h.get('dst')}:{h.get('dport')}"
|
||||
for h in network.get("tcp", [])],
|
||||
}
|
||||
return iocs
|
||||
|
||||
def analyze_sample(self, filepath):
|
||||
"""Full automated analysis pipeline."""
|
||||
task_id = self.submit_file(filepath)
|
||||
if not task_id:
|
||||
return None
|
||||
|
||||
if self.wait_for_completion(task_id):
|
||||
report = {
|
||||
"task_id": task_id,
|
||||
"config": self.get_config(task_id),
|
||||
"network_iocs": self.get_network_iocs(task_id),
|
||||
"dropped_files": len(self.get_dropped_files(task_id)),
|
||||
}
|
||||
return report
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print(f"Usage: {sys.argv[0]} <malware_sample> [cape_url]")
|
||||
sys.exit(1)
|
||||
|
||||
url = sys.argv[2] if len(sys.argv) > 2 else "http://localhost:8000"
|
||||
client = CAPEClient(url)
|
||||
result = client.analyze_sample(sys.argv[1])
|
||||
if result:
|
||||
print(json.dumps(result, indent=2))
|
||||
```
|
||||
|
||||
## Validation Criteria
|
||||
|
||||
- Samples submitted and analyzed within configured timeout
|
||||
- Behavioral signatures triggered for known malware families
|
||||
- Malware configurations extracted by cape-parsers
|
||||
- Network traffic captured and IOCs extracted
|
||||
- Dropped files and payloads collected for further analysis
|
||||
- Anti-evasion bypasses effective against sandbox-aware malware
|
||||
|
||||
## References
|
||||
|
||||
- [CAPEv2 GitHub](https://github.com/kevoreilly/CAPEv2)
|
||||
- [CAPE Sandbox Documentation](https://capev2.readthedocs.io/)
|
||||
- [Automating Malware Analysis with CAPE](https://endsec.au/blog/building-an-automated-malware-sandbox-using-cape/)
|
||||
- [Installing CAPEv2 on Ubuntu](https://medium.com/@rizqisetyokus/building-capev2-automated-malware-analysis-sandbox-part-1-da2a6ff69cdb)
|
||||
@@ -0,0 +1,25 @@
|
||||
# Analysis Report Template - performing-automated-malware-analysis-with-cape
|
||||
|
||||
## Sample Information
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| SHA-256 | |
|
||||
| File Type | |
|
||||
| Analysis Date | |
|
||||
| Analyst | |
|
||||
| Classification | TLP:AMBER |
|
||||
|
||||
## Findings
|
||||
| Finding | Severity | Details |
|
||||
|---------|----------|---------|
|
||||
| | | |
|
||||
|
||||
## IOCs Extracted
|
||||
| Type | Value | Context |
|
||||
|------|-------|---------|
|
||||
| | | |
|
||||
|
||||
## Recommendations
|
||||
1.
|
||||
2.
|
||||
3.
|
||||
@@ -0,0 +1,204 @@
|
||||
# API Reference: CAPE Sandbox Automated Malware Analysis
|
||||
|
||||
## Libraries Used
|
||||
|
||||
| Library | Purpose |
|
||||
|---------|---------|
|
||||
| `requests` | HTTP client for CAPE REST API v2 |
|
||||
| `json` | Parse analysis reports and task metadata |
|
||||
| `os` | Read `CAPE_URL` and `CAPE_API_KEY` environment variables |
|
||||
| `time` | Poll task status until analysis completes |
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install requests
|
||||
```
|
||||
|
||||
## Authentication
|
||||
|
||||
```python
|
||||
import requests
|
||||
import os
|
||||
|
||||
CAPE_URL = os.environ.get("CAPE_URL", "http://cape.example.com:8000")
|
||||
CAPE_KEY = os.environ.get("CAPE_API_KEY", "")
|
||||
headers = {"Authorization": f"Token {CAPE_KEY}"} if CAPE_KEY else {}
|
||||
```
|
||||
|
||||
## REST API v2 Endpoints
|
||||
|
||||
| Method | Endpoint | Description |
|
||||
|--------|----------|-------------|
|
||||
| POST | `/apiv2/tasks/create/file/` | Submit a file for analysis |
|
||||
| POST | `/apiv2/tasks/create/url/` | Submit a URL for analysis |
|
||||
| GET | `/apiv2/tasks/list/` | List all analysis tasks |
|
||||
| GET | `/apiv2/tasks/view/{id}/` | Get task status and metadata |
|
||||
| GET | `/apiv2/tasks/report/{id}/` | Get full analysis report |
|
||||
| GET | `/apiv2/tasks/report/{id}/lite/` | Get lightweight report |
|
||||
| DELETE | `/apiv2/tasks/delete/{id}/` | Delete a task and its data |
|
||||
| GET | `/apiv2/tasks/screenshots/{id}/` | Get analysis screenshots |
|
||||
| GET | `/apiv2/tasks/procmemory/{id}/` | Get process memory dumps |
|
||||
| GET | `/apiv2/files/view/sha256/{hash}/` | Look up file by SHA-256 |
|
||||
| GET | `/apiv2/files/get/{sha256}/` | Download the sample binary |
|
||||
| GET | `/apiv2/pcap/get/{id}/` | Download PCAP network capture |
|
||||
| GET | `/apiv2/machines/list/` | List analysis VMs |
|
||||
| GET | `/apiv2/cuckoo/status/` | Server status and version |
|
||||
|
||||
## Core Operations
|
||||
|
||||
### Submit a File for Analysis
|
||||
```python
|
||||
def submit_file(file_path, timeout_mins=5, machine=None):
|
||||
files = {"file": open(file_path, "rb")}
|
||||
data = {
|
||||
"timeout": timeout_mins * 60,
|
||||
"enforce_timeout": True,
|
||||
"options": "procmemdump=yes,import_reconstruction=yes",
|
||||
}
|
||||
if machine:
|
||||
data["machine"] = machine
|
||||
|
||||
resp = requests.post(
|
||||
f"{CAPE_URL}/apiv2/tasks/create/file/",
|
||||
files=files,
|
||||
data=data,
|
||||
headers=headers,
|
||||
timeout=60,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
result = resp.json()
|
||||
return result["data"]["task_ids"][0]
|
||||
```
|
||||
|
||||
### Submit a URL for Analysis
|
||||
```python
|
||||
def submit_url(url, timeout_mins=3):
|
||||
resp = requests.post(
|
||||
f"{CAPE_URL}/apiv2/tasks/create/url/",
|
||||
data={
|
||||
"url": url,
|
||||
"timeout": timeout_mins * 60,
|
||||
"options": "procmemdump=yes",
|
||||
},
|
||||
headers=headers,
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()["data"]["task_ids"][0]
|
||||
```
|
||||
|
||||
### Poll Task Until Complete
|
||||
```python
|
||||
import time
|
||||
|
||||
def wait_for_task(task_id, poll_interval=30, max_wait=600):
|
||||
elapsed = 0
|
||||
while elapsed < max_wait:
|
||||
resp = requests.get(
|
||||
f"{CAPE_URL}/apiv2/tasks/view/{task_id}/",
|
||||
headers=headers,
|
||||
timeout=30,
|
||||
)
|
||||
status = resp.json()["data"]["status"]
|
||||
if status == "reported":
|
||||
return True
|
||||
if status in ("failed_analysis", "failed_processing"):
|
||||
raise RuntimeError(f"Task {task_id} failed: {status}")
|
||||
time.sleep(poll_interval)
|
||||
elapsed += poll_interval
|
||||
raise TimeoutError(f"Task {task_id} did not complete within {max_wait}s")
|
||||
```
|
||||
|
||||
### Retrieve Analysis Report
|
||||
```python
|
||||
def get_report(task_id, lite=False):
|
||||
endpoint = "lite" if lite else ""
|
||||
resp = requests.get(
|
||||
f"{CAPE_URL}/apiv2/tasks/report/{task_id}/{endpoint}",
|
||||
headers=headers,
|
||||
timeout=120,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
```
|
||||
|
||||
### Extract Key Findings from Report
|
||||
```python
|
||||
def extract_findings(report):
|
||||
info = report.get("info", {})
|
||||
findings = {
|
||||
"score": info.get("score", 0),
|
||||
"duration": info.get("duration", 0),
|
||||
"signatures": [],
|
||||
"network_iocs": {"domains": [], "ips": [], "urls": []},
|
||||
"dropped_files": [],
|
||||
"yara_matches": [],
|
||||
}
|
||||
|
||||
# Behavioral signatures
|
||||
for sig in report.get("signatures", []):
|
||||
findings["signatures"].append({
|
||||
"name": sig["name"],
|
||||
"severity": sig["severity"],
|
||||
"description": sig["description"],
|
||||
})
|
||||
|
||||
# Network IOCs
|
||||
network = report.get("network", {})
|
||||
findings["network_iocs"]["domains"] = [
|
||||
d["domain"] for d in network.get("domains", [])
|
||||
]
|
||||
findings["network_iocs"]["ips"] = [
|
||||
h["ip"] for h in network.get("hosts", [])
|
||||
]
|
||||
|
||||
# YARA matches
|
||||
for target_yara in report.get("target", {}).get("file", {}).get("yara", []):
|
||||
findings["yara_matches"].append(target_yara["name"])
|
||||
|
||||
return findings
|
||||
```
|
||||
|
||||
### Download Network PCAP
|
||||
```python
|
||||
def download_pcap(task_id, output_path):
|
||||
resp = requests.get(
|
||||
f"{CAPE_URL}/apiv2/pcap/get/{task_id}/",
|
||||
headers=headers,
|
||||
timeout=60,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
with open(output_path, "wb") as f:
|
||||
f.write(resp.content)
|
||||
```
|
||||
|
||||
## Output Format
|
||||
|
||||
```json
|
||||
{
|
||||
"info": {
|
||||
"id": 42,
|
||||
"score": 8.5,
|
||||
"duration": 120,
|
||||
"machine": {"name": "win10-01", "label": "win10-01"},
|
||||
"started": "2025-01-15T10:30:00",
|
||||
"ended": "2025-01-15T10:32:00"
|
||||
},
|
||||
"signatures": [
|
||||
{"name": "ransomware_bcdedit", "severity": 5, "description": "Modifies boot configuration"},
|
||||
{"name": "creates_exe", "severity": 3, "description": "Creates executable files on disk"}
|
||||
],
|
||||
"network": {
|
||||
"hosts": [{"ip": "198.51.100.42", "country": "US"}],
|
||||
"domains": [{"domain": "c2.evil.example.com", "ip": "198.51.100.42"}]
|
||||
},
|
||||
"target": {
|
||||
"file": {
|
||||
"name": "sample.exe",
|
||||
"size": 245760,
|
||||
"sha256": "a1b2c3d4e5f6..."
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,9 @@
|
||||
# Standards Reference - performing-automated-malware-analysis-with-cape
|
||||
|
||||
## Applicable Standards
|
||||
- MITRE ATT&CK Framework
|
||||
- NIST SP 800-83 Guide to Malware Incident Prevention
|
||||
- NIST SP 800-86 Guide to Integrating Forensic Techniques
|
||||
|
||||
## Related MITRE ATT&CK Techniques
|
||||
See SKILL.md for specific technique mappings.
|
||||
@@ -0,0 +1,11 @@
|
||||
# Analysis Workflows - performing-automated-malware-analysis-with-cape
|
||||
|
||||
## Primary Workflow
|
||||
```
|
||||
[Sample Collection] --> [Static Analysis] --> [Dynamic Analysis] --> [IOC Extraction]
|
||||
|
|
||||
v
|
||||
[Report Generation]
|
||||
```
|
||||
|
||||
See SKILL.md for detailed step-by-step procedures.
|
||||
@@ -0,0 +1,288 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CAPE Sandbox automated malware analysis agent.
|
||||
|
||||
Submits malware samples to a CAPE Sandbox instance via its REST API,
|
||||
polls for analysis completion, and retrieves behavioral reports including
|
||||
process trees, network activity, dropped files, and extracted configs.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
||||
try:
|
||||
import requests
|
||||
except ImportError:
|
||||
print("[!] 'requests' library required: pip install requests", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def get_cape_config():
|
||||
"""Return CAPE server URL and optional API token."""
|
||||
server = os.environ.get("CAPE_URL", "http://localhost:8090")
|
||||
token = os.environ.get("CAPE_API_TOKEN", "")
|
||||
return server.rstrip("/"), token
|
||||
|
||||
|
||||
def submit_file(server, token, file_path, timeout_minutes=5, machine=None):
|
||||
"""Submit a file to CAPE for analysis."""
|
||||
url = f"{server}/apiv2/tasks/create/file/"
|
||||
headers = {"Authorization": f"Token {token}"} if token else {}
|
||||
if not os.path.isfile(file_path):
|
||||
print(f"[!] File not found: {file_path}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
data = {"timeout": timeout_minutes * 60}
|
||||
if machine:
|
||||
data["machine"] = machine
|
||||
print(f"[*] Submitting {os.path.basename(file_path)} to CAPE...")
|
||||
with open(file_path, "rb") as f:
|
||||
resp = requests.post(
|
||||
url,
|
||||
files={"file": (os.path.basename(file_path), f)},
|
||||
data=data,
|
||||
headers=headers,
|
||||
timeout=120,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
result = resp.json()
|
||||
if result.get("error"):
|
||||
print(f"[!] Submission error: {result.get('error_value', result['error'])}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
task_id = result.get("data", {}).get("task_ids", [None])[0]
|
||||
if not task_id:
|
||||
task_id = result.get("task_id") or result.get("data", {}).get("task_id")
|
||||
print(f"[+] Submitted successfully, task ID: {task_id}")
|
||||
return task_id
|
||||
|
||||
|
||||
def submit_url(server, token, target_url, timeout_minutes=5):
|
||||
"""Submit a URL to CAPE for analysis."""
|
||||
url = f"{server}/apiv2/tasks/create/url/"
|
||||
headers = {"Authorization": f"Token {token}"} if token else {}
|
||||
data = {"url": target_url, "timeout": timeout_minutes * 60}
|
||||
print(f"[*] Submitting URL: {target_url}")
|
||||
resp = requests.post(url, data=data, headers=headers, timeout=60)
|
||||
resp.raise_for_status()
|
||||
result = resp.json()
|
||||
task_id = result.get("data", {}).get("task_ids", [None])[0]
|
||||
if not task_id:
|
||||
task_id = result.get("task_id")
|
||||
print(f"[+] Submitted, task ID: {task_id}")
|
||||
return task_id
|
||||
|
||||
|
||||
def poll_task_status(server, token, task_id, max_wait=600, interval=15):
|
||||
"""Poll CAPE until the analysis task completes or times out."""
|
||||
url = f"{server}/apiv2/tasks/status/{task_id}/"
|
||||
headers = {"Authorization": f"Token {token}"} if token else {}
|
||||
print(f"[*] Waiting for analysis to complete (max {max_wait}s)...")
|
||||
elapsed = 0
|
||||
while elapsed < max_wait:
|
||||
try:
|
||||
resp = requests.get(url, headers=headers, timeout=30)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
status = data.get("data", data.get("status", "unknown"))
|
||||
if isinstance(status, dict):
|
||||
status = status.get("status", "unknown")
|
||||
if status in ("reported", "completed"):
|
||||
print(f"[+] Analysis complete (status: {status})")
|
||||
return True
|
||||
if status in ("failed_analysis", "failed_processing"):
|
||||
print(f"[!] Analysis failed: {status}", file=sys.stderr)
|
||||
return False
|
||||
print(f" Status: {status} ({elapsed}s elapsed)")
|
||||
except requests.RequestException as e:
|
||||
print(f" Connection error: {e}")
|
||||
time.sleep(interval)
|
||||
elapsed += interval
|
||||
print("[!] Timed out waiting for analysis", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def get_report(server, token, task_id):
|
||||
"""Retrieve the full analysis report for a completed task."""
|
||||
url = f"{server}/apiv2/tasks/get/report/{task_id}/"
|
||||
headers = {"Authorization": f"Token {token}"} if token else {}
|
||||
resp = requests.get(url, headers=headers, timeout=120)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
def extract_findings(report):
|
||||
"""Extract structured findings from CAPE report."""
|
||||
findings = []
|
||||
|
||||
# Signatures (behavioral detections)
|
||||
for sig in report.get("signatures", []):
|
||||
findings.append({
|
||||
"category": "signature",
|
||||
"name": sig.get("name", "Unknown"),
|
||||
"severity": sig.get("severity", 0),
|
||||
"description": sig.get("description", ""),
|
||||
"families": sig.get("families", []),
|
||||
"ttp": sig.get("ttp", {}),
|
||||
})
|
||||
|
||||
# Network IOCs
|
||||
network = report.get("network", {})
|
||||
for dns_entry in network.get("dns", []):
|
||||
findings.append({
|
||||
"category": "network_dns",
|
||||
"request": dns_entry.get("request", ""),
|
||||
"type": dns_entry.get("type", ""),
|
||||
"answers": [a.get("data", "") for a in dns_entry.get("answers", [])],
|
||||
})
|
||||
for http_entry in network.get("http", []):
|
||||
findings.append({
|
||||
"category": "network_http",
|
||||
"method": http_entry.get("method", ""),
|
||||
"uri": http_entry.get("uri", ""),
|
||||
"host": http_entry.get("host", ""),
|
||||
"port": http_entry.get("port", 80),
|
||||
})
|
||||
|
||||
# Dropped files
|
||||
for dropped in report.get("dropped", []):
|
||||
findings.append({
|
||||
"category": "dropped_file",
|
||||
"name": dropped.get("name", ""),
|
||||
"path": dropped.get("filepath", ""),
|
||||
"type": dropped.get("type", ""),
|
||||
"size": dropped.get("size", 0),
|
||||
"sha256": dropped.get("sha256", ""),
|
||||
})
|
||||
|
||||
# CAPE extracted payloads and configs
|
||||
cape_data = report.get("CAPE", {})
|
||||
if isinstance(cape_data, dict):
|
||||
for cape_item in cape_data.get("payloads", []):
|
||||
findings.append({
|
||||
"category": "cape_payload",
|
||||
"name": cape_item.get("name", ""),
|
||||
"module": cape_item.get("module_path", ""),
|
||||
"sha256": cape_item.get("sha256", ""),
|
||||
"cape_type": cape_item.get("cape_type", ""),
|
||||
})
|
||||
for config in cape_data.get("configs", []):
|
||||
findings.append({
|
||||
"category": "cape_config",
|
||||
"family": config.get("family", "unknown"),
|
||||
"config_data": config,
|
||||
})
|
||||
|
||||
return findings
|
||||
|
||||
|
||||
def format_summary(report, findings):
|
||||
"""Print human-readable analysis summary."""
|
||||
info = report.get("info", {})
|
||||
target = report.get("target", {})
|
||||
score = report.get("malscore", info.get("score", 0))
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print(f" CAPE Sandbox Analysis Report")
|
||||
print(f"{'='*60}")
|
||||
print(f" Task ID : {info.get('id', 'N/A')}")
|
||||
print(f" Duration : {info.get('duration', 0)}s")
|
||||
print(f" Malscore : {score}/10")
|
||||
|
||||
file_info = target.get("file", {})
|
||||
if file_info:
|
||||
print(f" File : {file_info.get('name', 'N/A')}")
|
||||
print(f" SHA256 : {file_info.get('sha256', 'N/A')}")
|
||||
print(f" Type : {file_info.get('type', 'N/A')}")
|
||||
|
||||
sig_findings = [f for f in findings if f["category"] == "signature"]
|
||||
net_dns = [f for f in findings if f["category"] == "network_dns"]
|
||||
net_http = [f for f in findings if f["category"] == "network_http"]
|
||||
dropped = [f for f in findings if f["category"] == "dropped_file"]
|
||||
configs = [f for f in findings if f["category"] == "cape_config"]
|
||||
|
||||
print(f"\n Signatures : {len(sig_findings)}")
|
||||
print(f" DNS Queries : {len(net_dns)}")
|
||||
print(f" HTTP Reqs : {len(net_http)}")
|
||||
print(f" Dropped : {len(dropped)}")
|
||||
print(f" CAPE Configs: {len(configs)}")
|
||||
|
||||
if sig_findings:
|
||||
print(f"\n Top Signatures:")
|
||||
for s in sorted(sig_findings, key=lambda x: x.get("severity", 0), reverse=True)[:10]:
|
||||
sev = s.get("severity", 0)
|
||||
label = "HIGH" if sev >= 3 else "MEDIUM" if sev >= 2 else "LOW"
|
||||
print(f" [{label:6s}] {s['name']}: {s['description'][:80]}")
|
||||
|
||||
if configs:
|
||||
print(f"\n Extracted Configs:")
|
||||
for c in configs:
|
||||
print(f" Family: {c.get('family', 'unknown')}")
|
||||
|
||||
return score
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="CAPE Sandbox malware analysis agent"
|
||||
)
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("--file", help="Path to malware sample to submit")
|
||||
group.add_argument("--url", help="URL to submit for analysis")
|
||||
group.add_argument("--task-id", type=int, help="Retrieve report for existing task")
|
||||
parser.add_argument("--output", "-o", help="Output JSON report path")
|
||||
parser.add_argument("--server", help="CAPE server URL (or set CAPE_URL env var)")
|
||||
parser.add_argument("--token", help="API token (or set CAPE_API_TOKEN env var)")
|
||||
parser.add_argument("--machine", help="Specific VM to use for analysis")
|
||||
parser.add_argument("--timeout", type=int, default=5, help="Analysis timeout in minutes")
|
||||
parser.add_argument("--wait", type=int, default=600, help="Max seconds to wait for completion")
|
||||
parser.add_argument("--verbose", "-v", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.server:
|
||||
os.environ["CAPE_URL"] = args.server
|
||||
if args.token:
|
||||
os.environ["CAPE_API_TOKEN"] = args.token
|
||||
|
||||
server, token = get_cape_config()
|
||||
|
||||
if args.task_id:
|
||||
task_id = args.task_id
|
||||
elif args.file:
|
||||
task_id = submit_file(server, token, args.file, args.timeout, args.machine)
|
||||
else:
|
||||
task_id = submit_url(server, token, args.url, args.timeout)
|
||||
|
||||
if not args.task_id:
|
||||
if not poll_task_status(server, token, task_id, args.wait):
|
||||
sys.exit(1)
|
||||
|
||||
report = get_report(server, token, task_id)
|
||||
findings = extract_findings(report)
|
||||
score = format_summary(report, findings)
|
||||
|
||||
output_report = {
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"tool": "CAPE Sandbox",
|
||||
"task_id": task_id,
|
||||
"malscore": score,
|
||||
"findings_count": len(findings),
|
||||
"findings": findings,
|
||||
"risk_level": (
|
||||
"CRITICAL" if score >= 8
|
||||
else "HIGH" if score >= 5
|
||||
else "MEDIUM" if score >= 3
|
||||
else "LOW"
|
||||
),
|
||||
}
|
||||
|
||||
if args.output:
|
||||
with open(args.output, "w") as f:
|
||||
json.dump(output_report, f, indent=2)
|
||||
print(f"\n[+] Report saved to {args.output}")
|
||||
elif args.verbose:
|
||||
print(json.dumps(output_report, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user