File size: 20,566 Bytes
ec24219 3626226 ec24219 3626226 ec24219 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 |
# C2Sentinel API Reference
Complete technical documentation for the C2Sentinel Python API.
**Author:** Daniel Ostrow
**Website:** [neuralintellect.com](https://neuralintellect.com)
---
## Table of Contents
1. [C2Sentinel Class](#c2sentinel-class)
2. [AnalysisResult Class](#analysisresult-class)
3. [ConnectionContext Class](#connectioncontext-class)
4. [ReconSupport Class](#reconsupport-class)
5. [FeatureExtractor Class](#featureextractor-class)
6. [LogParser Class](#logparser-class)
7. [Enums and Constants](#enums-and-constants)
---
## C2Sentinel Class
Main interface for C2 detection.
### Constructor
```python
C2Sentinel(model: LogBERTC2Sentinel, config: C2SentinelConfig, device: str = 'auto')
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `model` | LogBERTC2Sentinel | The neural network model |
| `config` | C2SentinelConfig | Model configuration |
| `device` | str | Device for inference ('auto', 'cpu', 'cuda') |
### Class Methods
#### load
```python
@classmethod
def load(cls, path: str, device: str = 'auto') -> 'C2Sentinel'
```
Load a pre-trained model from safetensors format.
| Parameter | Type | Description |
|-----------|------|-------------|
| `path` | str | Path to model files (without extension) |
| `device` | str | Device for inference |
**Returns:** C2Sentinel instance
**Example:**
```python
sentinel = C2Sentinel.load('c2_sentinel')
sentinel = C2Sentinel.load('/path/to/c2_sentinel', device='cuda')
```
#### create_new
```python
@classmethod
def create_new(cls, device: str = 'auto') -> 'C2Sentinel'
```
Create a new untrained model instance.
**Returns:** C2Sentinel instance with random weights
---
### Instance Methods
#### analyze
```python
def analyze(
self,
connections: List[Dict],
threshold: float = 0.5,
context: Optional[ConnectionContext] = None,
include_features: bool = False,
strict_mode: bool = False
) -> AnalysisResult
```
Analyze a list of connections for C2 activity.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `connections` | List[Dict] | required | List of connection records |
| `threshold` | float | 0.5 | Detection threshold (0.0-1.0) |
| `context` | ConnectionContext | None | Optional context for enrichment |
| `include_features` | bool | False | Include raw feature vector in result |
| `strict_mode` | bool | False | Enforce minimum 0.7 threshold |
**Returns:** AnalysisResult object
**Connection Record Fields:**
```python
{
'timestamp': float, # Required: Unix timestamp
'dst_ip': str, # Required: Destination IP
'dst_port': int, # Required: Destination port
'bytes_sent': int, # Required: Bytes sent
'bytes_recv': int, # Required: Bytes received
'src_ip': str, # Optional: Source IP
'src_port': int, # Optional: Source port
'protocol': str, # Optional: 'tcp' or 'udp'
'duration': float # Optional: Duration in seconds
}
```
**Example:**
```python
connections = [
{'timestamp': 1000, 'dst_ip': '10.0.0.1', 'dst_port': 443,
'bytes_sent': 200, 'bytes_recv': 500},
{'timestamp': 1060, 'dst_ip': '10.0.0.1', 'dst_port': 443,
'bytes_sent': 200, 'bytes_recv': 500},
]
result = sentinel.analyze(connections)
result = sentinel.analyze(connections, threshold=0.7, strict_mode=True)
```
---
#### analyze_batch
```python
def analyze_batch(
self,
connection_groups: List[List[Dict]],
threshold: float = 0.5,
contexts: Optional[List[ConnectionContext]] = None,
parallel: bool = True
) -> List[AnalysisResult]
```
Analyze multiple connection groups.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `connection_groups` | List[List[Dict]] | required | List of connection lists |
| `threshold` | float | 0.5 | Detection threshold |
| `contexts` | List[ConnectionContext] | None | Context for each group |
| `parallel` | bool | True | Enable parallel processing |
**Returns:** List of AnalysisResult objects
**Example:**
```python
groups = [
[conn1, conn2, conn3],
[conn4, conn5, conn6],
]
results = sentinel.analyze_batch(groups)
```
---
#### analyze_logs
```python
def analyze_logs(
self,
log_lines: List[str],
group_by_dst: bool = True,
threshold: float = 0.5
) -> List[Dict]
```
Parse and analyze raw log lines.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `log_lines` | List[str] | required | Raw log lines |
| `group_by_dst` | bool | True | Group connections by destination IP |
| `threshold` | float | 0.5 | Detection threshold |
**Returns:** List of result dictionaries, sorted by probability (descending)
**Supported Formats:**
- JSON logs with standard fields
- Zeek/Bro conn.log (tab-separated)
- Syslog with IP:port patterns
**Example:**
```python
with open('conn.log') as f:
lines = f.readlines()
results = sentinel.analyze_logs(lines, group_by_dst=True)
for r in results:
print(f"{r['dst_ip']}: {r['c2_probability']}")
```
---
#### add_whitelist
```python
def add_whitelist(
self,
ips: List[str] = None,
domains: List[str] = None
)
```
Add IPs or domains to the whitelist. Whitelisted destinations receive reduced C2 probability.
| Parameter | Type | Description |
|-----------|------|-------------|
| `ips` | List[str] | IP addresses to whitelist |
| `domains` | List[str] | Domain names to whitelist |
**Example:**
```python
sentinel.add_whitelist(
ips=['8.8.8.8', '1.1.1.1'],
domains=['google.com', 'github.com']
)
```
---
#### add_blacklist
```python
def add_blacklist(
self,
ips: List[str] = None,
domains: List[str] = None
)
```
Add IPs or domains to the blacklist. Blacklisted destinations receive increased C2 probability.
| Parameter | Type | Description |
|-----------|------|-------------|
| `ips` | List[str] | IP addresses to blacklist |
| `domains` | List[str] | Domain names to blacklist |
---
#### save
```python
def save(self, path: str)
```
Save model to safetensors format.
| Parameter | Type | Description |
|-----------|------|-------------|
| `path` | str | Output path (without extension) |
Creates two files:
- `{path}.safetensors` - Model weights
- `{path}.json` - Configuration
---
### Instance Attributes
| Attribute | Type | Description |
|-----------|------|-------------|
| `model` | LogBERTC2Sentinel | The neural network |
| `config` | C2SentinelConfig | Model configuration |
| `device` | torch.device | Inference device |
| `feature_extractor` | FeatureExtractor | Feature extraction module |
| `log_parser` | LogParser | Log parsing module |
| `context_engine` | ContextInference | Context inference module |
| `recon` | ReconSupport | Reconnaissance module |
---
## AnalysisResult Class
Dataclass containing analysis results.
### Attributes
| Attribute | Type | Description |
|-----------|------|-------------|
| `is_c2` | bool | True if C2 detected |
| `c2_probability` | float | Probability score (0.0-1.0) |
| `anomaly_score` | float | Anomaly detection score |
| `evasion_score` | float | Evasion technique detection score |
| `confidence` | float | Model confidence in prediction |
| `c2_type` | str | Detected C2 framework type |
| `c2_type_confidence` | float | Confidence in C2 type classification |
| `detection_method` | str | Detection method used |
| `immediate_detection` | bool | True if signature-based detection |
| `context_applied` | bool | True if context was applied |
| `original_probability` | float | Probability before context adjustment |
| `probability_modifier` | float | Context probability modifier |
| `matched_legitimate_pattern` | str | Name of matched legitimate pattern |
| `legitimate_confidence` | float | Confidence in legitimate pattern match |
| `risk_factors` | List[str] | Factors supporting C2 classification |
| `mitigating_factors` | List[str] | Factors against C2 classification |
| `service_type` | str | Detected service type |
| `recommendations` | List[str] | Suggested follow-up actions |
| `features` | List[float] | Raw 40-dimensional feature vector |
| `connections_analyzed` | int | Number of connections processed |
| `suspicious_connections` | List[Dict] | All connections with individual scores (if C2 detected) |
| `iocs` | Dict | Extracted IOCs for threat intel (if C2 detected) |
| `time_range` | Dict | Start, end, and duration of analyzed traffic |
| `destination_summary` | Dict | Destination IPs, ports, and byte totals |
### Machine-Readable Output Fields
When C2 is detected, these fields are populated for scripting and automation:
**suspicious_connections** - List of all connections with scores:
```python
[
{
'index': 0,
'timestamp': 1705600000,
'src_ip': '192.168.1.100',
'src_port': 52341,
'dst_ip': '45.33.32.156',
'dst_port': 443,
'bytes_sent': 200,
'bytes_recv': 500,
'score': 0.92
},
...
]
```
**iocs** - Indicators of Compromise for threat intel:
```python
{
'ip_addresses': ['45.33.32.156'],
'ports': [443],
'c2_type': 'cobalt_strike',
'timing_signature': {
'mean_interval': 60.0,
'interval_cv': 0.05
},
'size_signature': {
'mean_bytes_sent': 200.0,
'mean_bytes_recv': 500.0,
'sent_cv': 0.02,
'recv_cv': 0.03
},
'behavioral_indicators': ['Regular timing with consistent sizes', ...]
}
```
**time_range** - Temporal bounds of analyzed traffic:
```python
{
'start': 1705600000.0,
'end': 1705600420.0,
'duration': 420.0
}
```
**destination_summary** - Traffic summary:
```python
{
'unique_ips': ['45.33.32.156'],
'unique_ports': [443],
'destinations': {'45.33.32.156:443': 8},
'total_bytes_sent': 1600,
'total_bytes_recv': 4000
}
```
### Methods
#### to_dict
```python
def to_dict(self) -> Dict[str, Any]
```
Convert result to dictionary.
**Returns:** Dictionary representation of all attributes
---
#### to_json
```python
def to_json(self, indent: int = 2) -> str
```
Convert result to JSON string for scripting.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `indent` | int | 2 | JSON indentation level |
**Returns:** JSON string of all attributes
**Example:**
```python
result = sentinel.analyze(connections)
json_output = result.to_json()
# Write to file
with open('detection_result.json', 'w') as f:
f.write(result.to_json())
# Parse in pipeline
import json
data = json.loads(result.to_json())
```
---
#### to_ioc_format
```python
def to_ioc_format(self) -> Dict[str, Any]
```
Convert result to STIX-like format for threat intelligence platforms.
**Returns:**
```python
{
'type': 'indicator',
'spec_version': '2.1',
'pattern_type': 'c2-beacon',
'valid_from': timestamp,
'labels': ['malicious-activity', 'c2'],
'confidence': 92,
'indicators': { ... } # Same as iocs field
}
```
**Example:**
```python
result = sentinel.analyze(connections)
if result.is_c2:
stix_indicator = result.to_ioc_format()
# Send to threat intel platform
send_to_misp(stix_indicator)
```
---
## ConnectionContext Class
Dataclass for providing additional context to improve detection accuracy.
### Constructor
```python
ConnectionContext(
# Process information
process_name: Optional[str] = None,
process_path: Optional[str] = None,
process_pid: Optional[int] = None,
parent_process: Optional[str] = None,
command_line: Optional[str] = None,
# Network metadata
dns_queries: Optional[List[str]] = None,
resolved_hostname: Optional[str] = None,
tls_sni: Optional[str] = None,
tls_ja3: Optional[str] = None,
tls_ja3s: Optional[str] = None,
certificate_issuer: Optional[str] = None,
certificate_subject: Optional[str] = None,
certificate_valid: Optional[bool] = None,
http_user_agent: Optional[str] = None,
http_host: Optional[str] = None,
# Reputation
ip_reputation: Optional[float] = None,
domain_reputation: Optional[float] = None,
known_good: Optional[bool] = None,
known_bad: Optional[bool] = None,
threat_intel_match: Optional[str] = None,
# Host context
source_hostname: Optional[str] = None,
source_user: Optional[str] = None,
source_is_server: Optional[bool] = None,
source_is_workstation: Optional[bool] = None,
# Additional
geo_country: Optional[str] = None,
geo_asn: Optional[str] = None,
tags: Optional[List[str]] = None
)
```
### Attribute Details
| Attribute | Type | Effect on Analysis |
|-----------|------|-------------------|
| `process_name` | str | Known processes reduce probability |
| `known_good` | bool | True reduces probability by 90% |
| `known_bad` | bool | True increases probability by 5x |
| `ip_reputation` | float | Score > 0.8 reduces probability |
| `threat_intel_match` | str | Match increases probability by 5x |
| `tls_ja3` | str | Known C2 JA3 increases probability |
| `certificate_valid` | bool | False increases probability |
### Methods
#### to_dict
```python
def to_dict(self) -> Dict[str, Any]
```
Convert to dictionary, excluding None values.
---
## ReconSupport Class
Reconnaissance and enrichment utilities.
### Class Methods
#### analyze_ip
```python
@classmethod
def analyze_ip(cls, ip: str) -> Dict[str, Any]
```
Analyze an IP address.
| Parameter | Type | Description |
|-----------|------|-------------|
| `ip` | str | IP address to analyze |
**Returns:**
```python
{
'ip': str, # Original IP
'is_valid': bool, # Valid IP format
'is_private': bool, # RFC 1918 private range
'is_loopback': bool, # Loopback address
'is_multicast': bool, # Multicast address
'is_cdn': bool, # Known CDN range
'cdn_provider': str, # CDN name if applicable
'ip_version': int, # 4 or 6
'reverse_dns': str, # Reverse DNS lookup result
'numeric': int # Numeric representation
}
```
**Known CDN Ranges:**
- Cloudflare
- AWS
- Google Cloud
- Azure
- Akamai
---
#### analyze_connection_patterns
```python
@classmethod
def analyze_connection_patterns(cls, connections: List[Dict]) -> Dict[str, Any]
```
Analyze connection patterns for threat hunting.
| Parameter | Type | Description |
|-----------|------|-------------|
| `connections` | List[Dict] | Connection records |
**Returns:**
```python
{
'connection_count': int,
'unique_destinations': int,
'unique_ports': int,
'timing': {
'duration_seconds': float,
'mean_interval': float,
'interval_stddev': float,
'interval_cv': float # Coefficient of variation
},
'volume': {
'total_sent': int,
'total_recv': int,
'mean_sent': float,
'mean_recv': float,
'sent_recv_ratio': float
},
'ports': {
port_number: count, # Port distribution
...
},
'destinations': {
ip: analyze_ip_result, # Per-IP analysis
...
},
'indicators': {
'single_destination': bool,
'consistent_timing': bool,
'consistent_sizes': bool,
'uses_common_port': bool,
'uses_high_port': bool,
'has_cdn_destination': bool,
'all_private_destinations': bool
}
}
```
---
#### generate_iocs
```python
@classmethod
def generate_iocs(
cls,
connections: List[Dict],
result: Dict
) -> Dict[str, List[str]]
```
Generate Indicators of Compromise from detected C2.
| Parameter | Type | Description |
|-----------|------|-------------|
| `connections` | List[Dict] | Connection records |
| `result` | Dict | Analysis result dictionary |
**Returns:**
```python
{
'ips': List[str], # Destination IPs
'ports': List[str], # Destination ports
'timing_signatures': List[str], # Beacon timing patterns
'behavioral_indicators': List[str] # Behavioral markers
}
```
Only generates IOCs if `result['is_c2']` is True.
---
## FeatureExtractor Class
Extracts 40-dimensional feature vectors from connections.
### Constants
#### C2_TYPES
List of detectable C2 framework types:
```python
[
'unknown', 'metasploit', 'cobalt_strike', 'sliver', 'havoc',
'mythic', 'poshc2', 'merlin', 'empire', 'covenant',
'brute_ratel', 'koadic', 'pupy', 'silenttrinity', 'faction',
'ibombshell', 'godoh', 'dnscat2', 'iodine', 'dns_generic',
'http_custom', 'https_custom', 'websocket', 'domain_fronting',
'cloud_fronting', 'cdn_abuse', 'apt_generic', 'apt28', 'apt29',
'apt41', 'lazarus', 'fin7', 'turla', 'winnti', 'custom'
]
```
### Methods
#### extract_features
```python
def extract_features(self, connections: List[Dict]) -> np.ndarray
```
Extract 40-dimensional feature vector.
**Returns:** numpy array of shape (40,)
**Feature Groups:**
- Features 0-9: Timing (intervals, jitter, regularity, periodicity)
- Features 10-17: Destinations (diversity, persistence, ports)
- Features 18-27: Payload (sizes, ratios, consistency)
- Features 28-35: Evasion (jitter patterns, bursts, session length)
- Features 36-39: Advanced (night activity, fast beacon ratio, duration)
---
#### check_metasploit_signature
```python
def check_metasploit_signature(
self,
connections: List[Dict]
) -> Tuple[bool, float]
```
Check for Metasploit-specific signature patterns.
**Returns:** (is_metasploit, confidence)
---
#### check_ssh_keepalive
```python
def check_ssh_keepalive(
self,
connections: List[Dict]
) -> Tuple[bool, float]
```
Check for SSH keepalive pattern.
**Criteria:**
- Port 22
- Small packets (< 100 bytes)
- Symmetric traffic (sent/recv ratio 0.5-2.0)
- Consistent sizes (CV < 0.2)
- Regular intervals matching common keepalive values
**Returns:** (is_ssh_keepalive, confidence)
---
## LogParser Class
Parses various log formats into connection records.
### Static Methods
#### parse_json
```python
@staticmethod
def parse_json(log_line: str) -> Optional[Dict]
```
Parse JSON formatted log line.
**Recognized Fields:**
- timestamp, @timestamp
- src_ip, source_ip, src
- dst_ip, dest_ip, dst
- src_port, source_port
- dst_port, dest_port
- bytes_sent, bytes_out
- bytes_recv, bytes_in
---
#### parse_zeek_conn
```python
@staticmethod
def parse_zeek_conn(log_line: str) -> Optional[Dict]
```
Parse Zeek/Bro conn.log format (tab-separated).
---
#### parse_syslog
```python
@staticmethod
def parse_syslog(log_line: str) -> Optional[Dict]
```
Parse common syslog/netflow patterns.
**Recognized Patterns:**
- `YYYY-MM-DD HH:MM:SS ... IP:port -> IP:port`
- `src=IP ... dst=IP ... sport=port ... dport=port`
---
## Enums and Constants
### DetectionMethod
```python
class DetectionMethod(Enum):
SIGNATURE = "signature" # Port + behavior signature match
BEHAVIORAL = "behavioral" # Pure behavioral analysis
ML = "ml" # Machine learning inference
CONTEXT = "context" # Context-adjusted detection
HEURISTIC = "heuristic" # Rule-based detection
WHITELIST = "whitelist" # Matched whitelist pattern
```
### ServiceType
```python
class ServiceType(Enum):
SSH = "ssh"
HTTP = "http"
HTTPS = "https"
DNS = "dns"
DATABASE = "database"
API = "api"
STREAMING = "streaming"
GAMING = "gaming"
VPN = "vpn"
MONITORING = "monitoring"
UNKNOWN = "unknown"
```
### C2_INDICATOR_PORTS
High-confidence C2 signature ports:
```python
{4444, 4445, 5555, 31337, 40056}
```
### C2_COMMON_PORTS
Ports commonly used by C2 (require behavioral analysis):
```python
{80, 443, 53, 8080, 8443, 8888}
```
---
## Convenience Functions
### load_model
```python
def load_model(path: str, device: str = 'auto') -> C2Sentinel
```
Shorthand for `C2Sentinel.load()`.
### create_model
```python
def create_model(device: str = 'auto') -> C2Sentinel
```
Shorthand for `C2Sentinel.create_new()`.
### quick_analyze
```python
def quick_analyze(
connections: List[Dict],
model_path: str = 'c2_sentinel'
) -> AnalysisResult
```
One-shot analysis without keeping model in memory.
---
## Error Handling
The API uses standard Python exceptions:
| Exception | Cause |
|-----------|-------|
| `FileNotFoundError` | Model files not found |
| `ValueError` | Invalid connection format |
| `RuntimeError` | CUDA/device errors |
All methods handle empty or malformed input gracefully, returning neutral results rather than raising exceptions.
|