# Threat Detection & Analysis Guide

## Automated Threat Detection

Lilith implements multi-layered threat detection using:

### 1. Statistical Anomaly Detection
- **Algorithm**: 3-sigma deviation from rolling baseline
- **Window**: 5-minute observation vs 60-minute baseline
- **Sensitivity**: Configurable per threat type
- **False Positive Mitigation**: Minimum threshold enforcement

### 2. Pattern-Based Detection
- **Authentication Anomalies**: Credential stuffing, brute force patterns
- **AI Safety Violations**: Prompt injection, jailbreak attempts
- **Content Violations**: Policy breaches, harmful content

### 3. Behavioral Analysis
- **User Behavior**: Unusual access patterns, geographic anomalies
- **Service Behavior**: Unexpected API usage, resource consumption
- **Network Behavior**: Traffic anomalies, connection patterns

## Threat Types & Detection

### Authentication Threats

#### Credential Stuffing
**Detection Criteria:**
- Multiple failed authentications across different usernames
- High velocity from multiple IP addresses
- Success rate < 1%

**Detection Logic:**
```javascript
const isCredentialStuffing = (events) => {
  const failureRate = events.filter(e => e.type === 'auth_failed').length / events.length;
  const uniqueIPs = new Set(events.map(e => e.context.ip_address)).size;
  const uniqueUsers = new Set(events.map(e => e.context.username)).size;

  return failureRate > 0.99 && uniqueIPs > 10 && uniqueUsers > 50;
};
```

#### Account Takeover
**Detection Criteria:**
- Successful authentication after multiple failures
- Login from new geographic location
- Changed security settings immediately after login

### AI Safety Threats

#### Prompt Injection
**Detection Criteria:**
- Specific injection patterns in user input
- Unusual token usage or response patterns
- Safety model confidence below threshold

**Example Patterns:**
- "Ignore previous instructions"
- "You are now in developer mode"
- Embedded system prompts

#### Jailbreak Attempts
**Detection Criteria:**
- Attempts to bypass safety guardrails
- Roleplay requests for harmful content
- Multi-turn attack patterns

### Content Moderation Threats

#### Policy Violations
**Detection Criteria:**
- Harmful content patterns
- Inappropriate persona requests
- Cultural sensitivity violations

## Response Automation

### Automated Responses

```javascript
const responseActions = {
  'credential_stuffing_detected': [
    'enable_enhanced_rate_limiting',
    'flag_suspicious_ips',
    'notify_security_team'
  ],
  'jailbreak_attempt': [
    'log_detailed_context',
    'update_safety_filters',
    'alert_ai_safety_team'
  ],
  'content_policy_violation': [
    'queue_for_human_review',
    'temporarily_restrict_user',
    'log_violation_pattern'
  ]
};
```

### Escalation Matrix

| Threat Level | Automated Response | Human Involvement | Timeline |
|--------------|-------------------|-------------------|----------|
| Low | Log + Monitor | Daily review | 24h |
| Medium | Rate limit + Alert | Team notification | 4h |
| High | Block + Escalate | On-call response | 1h |
| Critical | Lock + Incident | Immediate response | 15m |

## Testing & Validation

### Automated Testing

The security monitoring system includes comprehensive automated testing:

```bash
# Run security monitoring tests
npm test -- tests/observability.security-monitoring.spec.js

# Run anomaly detection tests
npm test -- tests/observability.security-anomaly.spec.js

# Validate threat detection patterns
npm test -- tests/security-pattern-validation.spec.js
```

### Manual Testing

```bash
# Test security event ingestion
curl -X POST "$OBSERVABILITY_URL/v1/security/events" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "type": "test_event",
    "severity": "low",
    "source": "test",
    "context": {"test": true}
  }'

# Verify dashboard aggregation
curl "$OBSERVABILITY_URL/v1/security/dashboard"
```

---
*Generated automatically by security-docs-generator.js*
*Last updated: 2025-09-27T18:05:41.726Z*
