cert-x-gen 1.9.2

Advanced Multi-Language Security Scanning Engine
Documentation
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
# 🏗️ CERT-X-GEN Architecture

**Technical architecture and design documentation for CERT-X-GEN**

This document provides a comprehensive overview of the CERT-X-GEN system architecture, design decisions, and implementation details.

---

## 📋 Table of Contents

- [System Overview]#system-overview
- [Core Components]#core-components
- [Polyglot Engine Architecture]#polyglot-engine-architecture
- [Template Search System]#template-search-system
- [Data Flow]#data-flow
- [Template System]#template-system
- [Plugin System]#plugin-system
- [Configuration]#configuration
- [Error Handling]#error-handling
- [Performance]#performance
- [Security Considerations]#security-considerations

---

## 🎯 System Overview

### High-Level Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                      CLI Interface                          │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐           │
│  │    Scan     │ │   Search    │ │  Template   │           │
│  │   Command   │ │   Command   │ │  Command    │           │
│  └─────────────┘ └─────────────┘ └─────────────┘           │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                    Core Engine                              │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐           │
│  │   CertXGen  │ │  Executor   │ │  Scheduler  │           │
│  │   (Main)    │ │             │ │             │           │
│  └─────────────┘ └─────────────┘ └─────────────┘           │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                Template Engine System                       │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐           │
│  │   Loader    │ │   Search    │ │   Engines   │           │
│  │             │ │   Engine    │ │  (12 langs) │           │
│  └─────────────┘ └─────────────┘ └─────────────┘           │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                  Template Execution                         │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐           │
│  │ Interpreted │ │  Compiled   │ │ Declarative │           │
│  │  Languages  │ │  Languages  │ │   (YAML)    │           │
│  │ (6 langs)   │ │  (5 langs)  │ │             │           │
│  └─────────────┘ └─────────────┘ └─────────────┘           │
└─────────────────────────────────────────────────────────────┘
```

### Key Design Principles

1. **Polyglot Architecture** - Support for 12 programming languages
2. **Modular Design** - Clean separation of concerns
3. **Unified Interface** - Consistent API across all engines
4. **Performance Optimization** - Compilation caching and parallel execution
5. **Extensibility** - Easy addition of new languages and features
6. **Security First** - Built-in security considerations

---

## 🔧 Core Components

### 1. CertXGen (Main Engine)

**Location**: `src/core.rs`

The central orchestrator that coordinates all scanning operations:

```rust
pub struct CertXGen {
    config: Arc<Config>,
    template_loader: Arc<TemplateLoader>,
    executor: Arc<Executor>,
    scheduler: Arc<RwLock<Scheduler>>,
}
```

**Responsibilities:**
- Initialize and configure all subsystems
- Coordinate template loading and execution
- Manage scanning workflows
- Handle error propagation and recovery

### 2. TemplateLoader

**Location**: `src/template.rs`

Manages template discovery, loading, and engine registration:

```rust
pub struct TemplateLoader {
    engines: Vec<Box<dyn TemplateEngine>>,
}
```

**Responsibilities:**
- Register template engines for all 12 languages
- Discover templates in the filesystem
- Load template metadata and content
- Provide template filtering and selection

### 3. Executor

**Location**: `src/executor.rs`

Handles the actual execution of templates:

```rust
pub struct Executor {
    config: Arc<Config>,
    network_client: Arc<NetworkClient>,
    session_manager: Arc<SessionManager>,
    flow_executor: Arc<FlowExecutor>,
    semaphore: Arc<Semaphore>,
}
```

**Responsibilities:**
- Execute templates with proper environment setup
- Manage concurrent template execution (via `semaphore`)
- Handle per-template timeouts
- Collect and aggregate findings

### 4. Scheduler

**Location**: `src/scheduler.rs`

Orders templates for execution by severity priority:

```rust
pub struct Scheduler {
    config: Arc<Config>,
    priority_queue: BinaryHeap<PrioritizedTemplate>,
}
```

**Responsibilities:**
- Prioritize template execution by severity
- Optimize execution order

---

## 🌐 Polyglot Engine Architecture

### Engine Hierarchy

```
TemplateEngine trait (interface)
    ├── Interpreted Engines (6)
    │   ├── PythonEngine      (.py)       → python3/python
    │   ├── JavaScriptEngine  (.js, .mjs) → node
    │   ├── RubyEngine        (.rb)       → ruby
    │   ├── PerlEngine        (.pl)       → perl
    │   ├── PhpEngine         (.php)      → php
    │   └── ShellEngine       (.sh)       → bash/sh
    │
    ├── Compiled Engines (5)
    │   ├── RustEngine        (.rs)       → rustc → binary
    │   ├── CEngine           (.c)        → gcc/clang → binary
    │   ├── CppEngine         (.cpp)      → g++/clang++ → binary
    │   ├── JavaEngine        (.java)     → javac → .class
    │   └── GoEngine          (.go)       → go build → binary
    │
    └── Declarative Engine (1)
        └── YamlTemplateEngine (.yaml, .yml) → built-in parser
```

### Engine Implementation

Each engine implements the `TemplateEngine` trait:

```rust
#[async_trait]
pub trait TemplateEngine: Send + Sync {
    fn name(&self) -> &str;
    fn language(&self) -> TemplateLanguage;
    fn supports_file(&self, path: &Path) -> bool;
    
    async fn load_template(&self, path: &Path) -> Result<Box<dyn Template>>;
    async fn execute_template(
        &self,
        template: &dyn Template,
        target: &Target,
        context: &Context,
    ) -> Result<Vec<Finding>>;
}
```

### Common Utilities

**Location**: `src/engine/common.rs`

Shared functionality across all engines:

- **`build_env_vars()`** - Build environment variables for templates
- **`parse_findings()`** - Parse JSON output from templates
- **`create_metadata()`** - Generate template metadata from file
- **`get_ports_to_scan()`** - Extract port configuration from context

### Compilation Caching

Compiled languages implement intelligent caching:

1. **Source Hash**: Calculate SHA-256 hash of source file
2. **Cache Key**: `{language}/{hash}`
3. **Cache Check**: Look for existing binary/class file
4. **Compilation**: Only compile if cache miss
5. **Cache Storage**: Store in `/tmp/cert-x-gen-cache/{language}/`

---

## 🔍 Template Search System

### Search Engine Architecture

**Location**: `src/search.rs`

The search system provides powerful template discovery capabilities:

```rust
pub struct TemplateSearchEngine {
    templates: Vec<SearchResult>,
    index: HashMap<String, Vec<usize>>, // word -> template indices
    content_index: HashMap<String, Vec<usize>>, // word -> template indices (content)
}
```

### Search Features

1. **Full-Text Search** - Search in names, descriptions, tags, and content
2. **Regex Support** - Advanced pattern matching
3. **Filtering** - By language, severity, tags, author, CWE
4. **Sorting** - By relevance, name, language, severity, date
5. **Multiple Output Formats** - Table, JSON, YAML, CSV, List, Detailed

### Search Integration

The search system integrates seamlessly with the scanning workflow:

```bash
# Use search results in scanning
TEMPLATES=$(cxg search --query "redis" --ids-only | tr '\n' ',')
cxg scan --target example.com --templates "$TEMPLATES"
```

---

## 📊 Data Flow

### 1. Template Discovery

```
File System → TemplateLoader → Engine Selection → Template Loading
     │              │                │                    │
     ▼              ▼                ▼                    ▼
templates/     File Extension    Engine Registry    Template Object
```

### 2. Template Execution

```
Template → Environment Setup → Execution → Output Parsing → Findings
    │              │              │            │              │
    ▼              ▼              ▼            ▼              ▼
Metadata      Env Variables   Process/HTTP   JSON Parse    Finding[]
```

### 3. Search Flow

```
Query → Search Engine → Index Lookup → Filtering → Sorting → Results
  │           │              │            │          │         │
  ▼           ▼              ▼            ▼          ▼         ▼
Text      Template DB    Word Index    Criteria   Algorithm  SearchResult[]
```

---

## 📝 Template System

### Template Structure

All templates follow a unified structure:

1. **Metadata** - Template information (ID, name, author, severity)
2. **Environment Variables** - Configuration from environment
3. **Scanning Logic** - Security checks and probes
4. **JSON Output** - Standardized findings format

### Communication Protocol

Templates communicate via environment variables and JSON output:

#### Environment Variables
```bash
CERT_X_GEN_TARGET_HOST=example.com
CERT_X_GEN_TARGET_PORT=80
CERT_X_GEN_TARGET_KIND=https
CERT_X_GEN_ADD_PORTS=8080,9090
CERT_X_GEN_TEMPLATE_ID=my-template
CERT_X_GEN_TEMPLATE_NAME="My Template"
```

For a `cli://` target the "host" is the binary path and `CERT_X_GEN_TARGET_PORT`
names nothing — ignore it when `KIND=cli`. cxg additionally delivers probe
input (`CERT_X_GEN_ARGV`, `CERT_X_GEN_STDIN_FILE`, `CERT_X_GEN_INPUT_DIR`,
`CERT_X_GEN_TARGET_ENV`) and what the build can reveal
(`CERT_X_GEN_TARGET_INSTRUMENTATION`). See the probe contract in
[`ENGINE_ARCHITECTURE.md`](ENGINE_ARCHITECTURE.md#the-probe-contract).

#### JSON Output Format
```json
{
  "findings": [
    {
      "id": "vulnerability-id",
      "name": "Vulnerability Name",
      "severity": "critical",
      "description": "Description of the vulnerability",
      "evidence": {
        "type": "http_response",
        "data": "Evidence data"
      },
      "tags": ["tag1", "tag2"],
      "cwe": "CWE-89"
    }
  ]
}
```

### Template Types

#### 1. Interpreted Templates
- **Execution**: Direct interpreter execution
- **Performance**: Slower startup, flexible
- **Languages**: Python, JavaScript, Ruby, Perl, PHP, Shell

#### 2. Compiled Templates
- **Execution**: Compile → cache → execute
- **Performance**: Faster execution, compilation overhead
- **Languages**: Rust, C, C++, Java, Go

#### 3. Declarative Templates
- **Execution**: Native parsing and execution
- **Performance**: Fastest for simple operations
- **Languages**: YAML

---

## 🔌 Plugin System

### Plugin Architecture

**Location**: `src/plugin.rs`

The plugin system allows for extensibility:

```rust
pub trait Plugin: Send + Sync {
    fn name(&self) -> &str;
    fn on_finding(&self, finding: &Finding) -> Result<()>;
    fn on_scan_start(&self, scan_id: Uuid) -> Result<()>;
    fn on_scan_complete(&self, results: &ScanResults) -> Result<()>;
    fn on_error(&self, error: &Error) -> Result<()>;
}
```

### Built-in Plugins

1. **LoggingPlugin** - Structured logging
2. **WebhookPlugin** - HTTP webhook notifications
3. **MetricsPlugin** - Performance metrics collection

---

## ⚙️ Configuration

### Configuration System

**Location**: `src/config.rs`

Supports multiple configuration formats:

```rust
pub struct Config {
    pub targets: Vec<Target>,
    pub templates: TemplateConfig,
    pub execution: ExecutionConfig,
    pub output: OutputConfig,
    pub network: NetworkConfig,
}
```

### Configuration Sources

1. **Command Line** - CLI arguments
2. **Configuration Files** - YAML, TOML, JSON
3. **Environment Variables** - Runtime configuration
4. **Defaults** - Sensible defaults

---

## 🚨 Error Handling

### Error Hierarchy

```rust
pub enum Error {
    Config(ConfigError),
    Network(NetworkError),
    Template(TemplateError),
    Execution(ExecutionError),
    Output(OutputError),
    Plugin(PluginError),
}
```

### Error Recovery

1. **Graceful Degradation** - Continue with available templates
2. **Retry Logic** - Automatic retry for transient failures
3. **Error Reporting** - Detailed error information
4. **Logging** - Comprehensive error logging

---

## ⚡ Performance

### Optimization Strategies

1. **Compilation Caching** - Avoid recompilation of templates
2. **Parallel Execution** - Concurrent template execution
3. **Resource Management** - Memory and CPU limits
4. **Connection Pooling** - Reuse HTTP connections
5. **Lazy Loading** - Load templates on demand

### Performance Metrics

- **Template Loading Time** - Time to load and parse templates
- **Compilation Time** - Time to compile templates (compiled languages)
- **Execution Time** - Time to execute templates
- **Memory Usage** - Memory consumption per template
- **Cache Hit Rate** - Compilation cache efficiency

---

## 🔒 Security Considerations

### Template execution is not confined

Templates execute as ordinary child processes with the invoking user's privileges and full
network and filesystem access. Review templates before running them.

cxg implements no sandboxing, isolation, or resource limiting — there is no flag,
configuration key, or default that provides any. Confinement, if you need it, must come from
outside the process: run cxg in a container or VM, as a non-privileged user, with cgroup and
network-namespace limits applied by the host.

### Security Measures

1. **Input Validation** - Sanitize all inputs
2. **Secure Defaults** - Safe configuration defaults
3. **Error Handling** - Don't leak sensitive information

### Template Security

1. **Code Review** - Review all template code
2. **Dependency Management** - Manage external dependencies
3. **Output Validation** - Validate template output
4. **Access Control** - Control template access

---

## 🚀 Future Enhancements

### Planned Features

1. **WASM Support** - WebAssembly template execution
2. **Plugin Marketplace** - Community plugin sharing
3. **Remote Execution** - Distributed template execution
4. **Template Marketplace** - Community template sharing
5. **AI Integration** - AI-powered template generation

### Language Additions

1. **C#** - .NET template support
2. **Kotlin** - JVM-based templates
3. **Swift** - macOS/iOS template support
4. **Lua** - Lightweight scripting support

---

## 📚 Related Documentation

- **[ENGINE_ARCHITECTURE.md]ENGINE_ARCHITECTURE.md** - Detailed engine architecture
- **[How cxg executes templates]https://docs.bugb.io/cxg/concepts/how-cxg-executes-templates/** - Language-specific execution
- **[Usage guide]https://docs.bugb.io/cxg/get-started/first-scan/** - Installing, scanning, and reading results
- **[TEMPLATE_REGISTRY.md]templates/TEMPLATE_REGISTRY.md** - Template catalog

---

## 🎉 Conclusion

CERT-X-GEN's architecture is designed for maximum flexibility, performance, and extensibility. The polyglot template engine system supports 12 programming languages, providing unprecedented flexibility in security scanning template development.

The modular design ensures maintainability, the compilation caching system optimizes performance, and the comprehensive error handling provides reliability. This architecture positions CERT-X-GEN as the most flexible and powerful polyglot security scanning framework available.

The addition of the template search system further enhances the user experience by providing powerful template discovery and exploration capabilities, making it easy to find and use the right templates for any security scanning scenario.