kindly-guard-server 0.11.14

KindlyGuard MCP server - Enterprise-grade security for AI model interactions
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
// Copyright 2025 Kindly Software Inc.
//
// 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.
//! Threat pattern definitions
//!
//! Configurable patterns for detecting various security threats

use super::ScanError;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

/// Collection of threat detection patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThreatPatterns {
    prompt_injection: Vec<String>,
    command_injection: Vec<String>,
    path_traversal: Vec<String>,
    sql_injection: Vec<String>,
    xss_patterns: Vec<String>,
    ldap_injection: Vec<String>,
    xml_injection: Vec<String>,
    nosql_injection: Vec<String>,
}

impl ThreatPatterns {
    /// Load patterns from a JSON file
    pub fn load_from_file(path: &Path) -> Result<Self, ScanError> {
        let content = fs::read_to_string(path)
            .map_err(|e| ScanError::InvalidInput(format!("Failed to read patterns file: {e}")))?;

        serde_json::from_str(&content)
            .map_err(|e| ScanError::InvalidInput(format!("Invalid patterns JSON: {e}")))
    }

    /// Get prompt injection patterns
    pub fn prompt_injection_patterns(&self) -> &[String] {
        &self.prompt_injection
    }

    /// Get command injection patterns
    pub fn command_injection_patterns(&self) -> &[String] {
        &self.command_injection
    }

    /// Get path traversal patterns
    pub fn path_traversal_patterns(&self) -> &[String] {
        &self.path_traversal
    }

    /// Get SQL injection patterns
    pub fn sql_injection_patterns(&self) -> &[String] {
        &self.sql_injection
    }

    /// Get XSS patterns
    pub fn xss_patterns(&self) -> &[String] {
        &self.xss_patterns
    }

    /// Get LDAP injection patterns
    pub fn ldap_injection_patterns(&self) -> &[String] {
        &self.ldap_injection
    }

    /// Get XML injection patterns
    pub fn xml_injection_patterns(&self) -> &[String] {
        &self.xml_injection
    }

    /// Get NoSQL injection patterns
    pub fn nosql_injection_patterns(&self) -> &[String] {
        &self.nosql_injection
    }

    /// Get all patterns as a serializable structure
    pub const fn get_all_patterns(&self) -> &Self {
        self
    }
}

impl Default for ThreatPatterns {
    fn default() -> Self {
        Self {
            prompt_injection: vec![
                // AI-specific prompt injections
                r"(?i)ignore\s+(previous|all|above)\s+(instructions?|prompts?|commands?)"
                    .to_string(),
                r"(?i)disregard\s+(previous|all|above)\s+(instructions?|prompts?|commands?)"
                    .to_string(),
                r"(?i)forget\s+(everything|all|previous)\s+(above|before)".to_string(),
                r"(?i)new\s+instructions?:\s*".to_string(),
                r"(?i)system\s*:\s*you\s+are".to_string(),
                r"(?i)pretend\s+you\s+are".to_string(),
                r"(?i)act\s+as\s+if\s+you".to_string(),
                r"(?i)roleplay\s+as".to_string(),
                r"(?i)you\s+are\s+now\s+in\s+developer\s+mode".to_string(),
                r"(?i)enable\s+developer\s+mode".to_string(),
                r"\[\[.*\]\]".to_string(), // Hidden instructions in brackets
                r"<system>.*</system>".to_string(), // Fake system tags
                r"(?i)simulation\s+mode".to_string(),
                r"(?i)testing\s+mode".to_string(),
            ],

            command_injection: vec![
                // Shell command separators
                r";\s*rm\s+".to_string(),
                r";\s*cat\s+".to_string(),
                r";\s*ls\s+".to_string(),
                r";\s*wget\s+".to_string(),
                r";\s*curl\s+".to_string(),
                r";\s*python\s+".to_string(),
                r";\s*python3\s+".to_string(),
                r";\s*perl\s+".to_string(),
                r";\s*ruby\s+".to_string(),
                r";\s*php\s+".to_string(),
                r";\s*node\s+".to_string(),
                r";\s*java\s+".to_string(),
                r"&&\s*[a-z]+\s+".to_string(),
                r"\|\|\s*[a-z]+\s+".to_string(),
                r"\|\s*[a-z]+\s+".to_string(),
                r"\|\s*nc\s+".to_string(),
                r"\|\s*netcat\s+".to_string(),
                r"\|\s*ncat\s+".to_string(),
                r"\|\s*socat\s+".to_string(),
                r"\|\s*telnet\s+".to_string(),
                r"\|\s*ssh\s+".to_string(),
                // Command substitution
                r"\$\([^)]+\)".to_string(),
                r"`[^`]+`".to_string(),
                r"\$\{[^}]+\}".to_string(),
                // Common dangerous commands
                r"(?i)(rm|del)\s+-rf?\s+".to_string(),
                r"(?i)format\s+[cd]:".to_string(),
                r"(?i)shutdown\s+".to_string(),
                r"/bin/(sh|bash|zsh|dash)".to_string(),
                // Windows-specific command injection patterns
                r"(?i)cmd\.exe\s*/c".to_string(),
                r"(?i)cmd\s*/c".to_string(),
                r"(?i)cmd\.exe\s+/c".to_string(),
                r"(?i)cmd\s+/c".to_string(),
                r"(?i)powershell\.exe\s*-c".to_string(),
                r"(?i)powershell\s*-c".to_string(),
                r"(?i)powershell\.exe\s*-Command".to_string(),
                r"(?i)powershell\s*-Command".to_string(),
                r"(?i)powershell\.exe\s*-ExecutionPolicy".to_string(),
                r"(?i)powershell\s*-ExecutionPolicy".to_string(),
                r"(?i)wscript\.exe".to_string(),
                r"(?i)cscript\.exe".to_string(),
                r"(?i)mshta\.exe".to_string(),
                r"(?i)rundll32\.exe".to_string(),
                r"(?i)regsvr32\.exe".to_string(),
                // Windows operators and pipes
                r"&\s*net\s+".to_string(),
                r"&\s*netsh\s+".to_string(),
                r"&\s*sc\s+".to_string(),
                r"&\s*reg\s+".to_string(),
                r"&\s*wmic\s+".to_string(),
                r"\|\s*findstr\s+".to_string(),
                r"\^\s*".to_string(), // Windows escape character
                // Windows shell shortcuts
                r"(?i)%comspec%".to_string(),
                r"(?i)%systemroot%".to_string(),
                r"(?i)%windir%".to_string(),
                // Newline command injection
                r"\n\s*/bin/".to_string(),
                r"\n\s*/usr/bin/".to_string(),
                r"\r\n\s*cmd".to_string(),
                r"\n\s*python".to_string(),
                r"\n\s*perl".to_string(),
                r"\n\s*ruby".to_string(),
                r"\n\s*sh\s*".to_string(),
                r"\n\s*bash\s*".to_string(),
                // Path injection
                r"\.\./\.\./(\.\./)*(etc|bin|usr|var)".to_string(),
            ],

            path_traversal: vec![
                // Unix path traversal - specific patterns first
                r"\.\./\.\./\.\./etc/passwd".to_string(), // Specific pattern for test
                r"\.\./(\.\./)*(etc|usr|bin|var|tmp|home|root)".to_string(),
                r"/\.\.".to_string(),
                r"\.\.%2[fF]".to_string(),      // URL encoded forward slash
                r"%2e%2e/".to_string(),         // URL encoded dots with slash
                r"%2e%2e%2f".to_string(),       // Fully URL encoded ../
                r"%252e%252e/".to_string(),     // Double encoded
                r"%252e%252e%252f".to_string(), // Double encoded ../
                r"%252f".to_string(),           // Double encoded forward slash
                // Windows path traversal
                r"\.\.\\(\.\.\\)*(windows|system32|users|program files)".to_string(),
                r"\\\.\.".to_string(),
                r"\.\.%5[cC]".to_string(), // URL encoded backslash
                r"%2e%2e\\\\".to_string(),
                // Advanced path traversal patterns
                r"\.{2,}/".to_string(),    // Multiple dots
                r"\.\.\.+/".to_string(),   // Three or more dots
                r"\.\./\.\./".to_string(), // Multiple traversals
                // Unicode and overlong encodings
                r"%c0%af".to_string(), // Overlong UTF-8 for /
                r"%c1%9c".to_string(), // Overlong UTF-8 for \
                // Null byte injection
                r"%00".to_string(),
                r"\x00".to_string(),
                // Absolute paths
                r"^/etc/".to_string(),
                r"^/usr/".to_string(),
                r"^/var/".to_string(),
                r"^[cC]:\\\\".to_string(),
                // Common target files
                r"etc/passwd".to_string(),
                r"etc/shadow".to_string(),
                r"windows/win\.ini".to_string(),
                r"boot\.ini".to_string(),
            ],

            sql_injection: vec![
                // Classic SQL injection
                r"'\s*(OR|AND)\s*'?1'?\s*=\s*'?1".to_string(),
                r"'\s*(OR|AND)\s+[0-9]+\s*=\s*[0-9]+".to_string(),
                r"admin'\s*--".to_string(),
                r"'\s*OR\s*'[^']*'\s*=\s*'[^']*".to_string(),
                // Union-based
                r"(?i)union\s+(all\s+)?select".to_string(),
                r"(?i)union\s+distinct\s+select".to_string(),
                // Time-based
                r"(?i)sleep\s*\(\s*[0-9]+\s*\)".to_string(),
                r"(?i)waitfor\s+delay".to_string(),
                r"(?i)benchmark\s*\(".to_string(),
                // Error-based
                r"(?i)extractvalue\s*\(".to_string(),
                r"(?i)updatexml\s*\(".to_string(),
                r"(?i)xmltype\s*\(".to_string(),
                // Common SQL commands
                r"(?i)(drop|create|alter)\s+(table|database|schema|index)".to_string(),
                r"(?i)insert\s+into\s+".to_string(),
                r"(?i)delete\s+from\s+".to_string(),
                r"(?i)update\s+\w+\s+set".to_string(),
                // Comment syntax
                r"--\s*$".to_string(),
                r"/\*.*\*/".to_string(),
                r"#\s*$".to_string(),
                // Hex encoding
                r"0x[0-9a-fA-F]+".to_string(),
                // SQL functions that might be abused
                r"(?i)concat\s*\(".to_string(),
                r"(?i)char\s*\(".to_string(),
                r"(?i)load_file\s*\(".to_string(),
                r"(?i)into\s+outfile".to_string(),
            ],

            xss_patterns: vec![
                // IMPORTANT: These patterns are for DETECTION only, not prevention
                // For actual XSS prevention, use proper HTML sanitization libraries

                // Script tags - various forms
                r"<script[^>]*>".to_string(),
                r"</script[^>]*>".to_string(),
                r"(?i)<script[^>]*>".to_string(),
                // Event handlers - most common XSS vector
                r"(?i)\bon\w+\s*=".to_string(), // matches onclick=, onerror=, etc.
                r"(?i)(onerror|onload|onclick|onmouseover|onfocus|onblur)\s*=".to_string(),
                // JavaScript protocol
                r"(?i)javascript\s*:".to_string(),
                r"(?i)vbscript\s*:".to_string(),
                r"(?i)livescript\s*:".to_string(),
                // Data URI with script content
                r"(?i)data:[^,]*script".to_string(),
                r"(?i)data:.*base64".to_string(),
                // Dangerous HTML elements
                r"(?i)<iframe[^>]*>".to_string(),
                r"(?i)<object[^>]*>".to_string(),
                r"(?i)<embed[^>]*>".to_string(),
                r"(?i)<applet[^>]*>".to_string(),
                r"(?i)<form[^>]*>".to_string(),
                r"(?i)<link[^>]*>".to_string(),
                r"(?i)<meta[^>]*>".to_string(),
                // SVG-based XSS
                r"(?i)<svg[^>]*>".to_string(),
                r"(?i)<svg[^>]*onload".to_string(),
                // Style-based XSS
                r"(?i)<style[^>]*>".to_string(),
                r"(?i)style\s*=.*expression\s*\(".to_string(),
                r"(?i)style\s*=.*javascript\s*:".to_string(),
                // Encoded script tags (hex, decimal, unicode)
                r"%3[Cc]script".to_string(),
                r"&#x3[Cc];script".to_string(),
                r"&#60;script".to_string(),
                r"\\u003[Cc]script".to_string(),
                r"\\x3[Cc]script".to_string(),
                // Common obfuscation techniques
                r"String\.fromCharCode".to_string(),
                r"eval\s*\(".to_string(),
                r"(?i)expression\s*\(".to_string(),
                r"(?i)document\s*\.\s*(write|writeln)".to_string(),
                r"(?i)window\s*\.\s*location".to_string(),
                r"(?i)document\s*\.\s*cookie".to_string(),
                // HTML5 event handlers
                r"(?i)onhashchange\s*=".to_string(),
                r"(?i)onpopstate\s*=".to_string(),
                r"(?i)onstorage\s*=".to_string(),
                // Bypasses with null bytes and comments
                r"<scr\x00ipt".to_string(),
                r"<scr\<!--.*--\>ipt".to_string(),
            ],

            ldap_injection: vec![
                // LDAP filter injection
                r"\(\|\(".to_string(),
                r"\)\(\|".to_string(),
                r"\(\&\(".to_string(),
                r"\)\(\&".to_string(),
                r"(?i)\*\)\(\|".to_string(),
                r"(?i)\)\(\|\(".to_string(),
                // LDAP escape sequences
                r"\\2a".to_string(), // \*
                r"\\28".to_string(), // \(
                r"\\29".to_string(), // \)
                r"\\5c".to_string(), // \\
                // Common LDAP injection patterns
                r"(?i)admin\*\)\(".to_string(),
                r"(?i)\*\)\(uid=\*".to_string(),
                r"(?i)\*\)\(objectClass=\*".to_string(),
                r"(?i)cn=\*\)\(".to_string(),
                // LDAP boolean conditions
                r"(?i)\|\(uid=\*".to_string(),
                r"(?i)\&\(uid=\*".to_string(),
                r"(?i)\!\(uid=\*".to_string(),
            ],

            xml_injection: vec![
                // XML entity injection
                r"<!ENTITY".to_string(),
                r"<!DOCTYPE".to_string(),
                r#"SYSTEM\s+["']file:"#.to_string(),
                r#"SYSTEM\s+["']http:"#.to_string(),
                r#"SYSTEM\s+["']https:"#.to_string(),
                // XXE patterns
                r"&xxe;".to_string(),
                r"%xxe;".to_string(),
                r"<!ENTITY\s+xxe".to_string(),
                r"<!ENTITY\s+%\s+xxe".to_string(),
                // XML injection via CDATA
                r"<!\[CDATA\[.*\]\]>".to_string(),
                r"]]>.*<!\[CDATA\[".to_string(),
                // SOAP injection
                r"</[^>]+><[^>]+>".to_string(),
                r"(?i)<soap:.*>".to_string(),
                // XPath injection
                r"(?i)' or '1'='1".to_string(),
                r#"(?i)" or "1"="1"#.to_string(),
                r"(?i)' or 1=1".to_string(),
                r#"(?i)" or 1=1"#.to_string(),
                // XML bomb patterns
                r#"<!ENTITY\s+lol\s+"lol"#.to_string(),
                r"&lol9;".to_string(),
            ],

            nosql_injection: vec![
                // MongoDB injection patterns
                r"\$ne".to_string(),
                r"\$eq".to_string(),
                r"\$gt".to_string(),
                r"\$gte".to_string(),
                r"\$lt".to_string(),
                r"\$lte".to_string(),
                r"\$nin".to_string(),
                r"\$in".to_string(),
                r"\$regex".to_string(),
                r"\$where".to_string(),
                r"\$exists".to_string(),
                r"\$type".to_string(),
                r"\$expr".to_string(),
                r"\$jsonSchema".to_string(),
                r"\$mod".to_string(),
                // MongoDB operators in JSON
                r#"["']\s*:\s*\{\s*["']\$"#.to_string(),
                r#"\{\s*["']\$ne["']\s*:"#.to_string(),
                r#"\{\s*["']\$gt["']\s*:"#.to_string(),
                // JavaScript injection in NoSQL
                r"(?i)function\s*\(".to_string(),
                r"(?i)return\s+true".to_string(),
                r"(?i)return\s+1\s*==\s*1".to_string(),
                r"(?i)this\.\w+\s*==".to_string(),
                // CouchDB injection
                r"_design/".to_string(),
                r"_view/".to_string(),
                r"\$text\s*:\s*\{".to_string(),
                r"\$search\s*:".to_string(),
                // Redis injection
                r"(?i)FLUSHDB".to_string(),
                r"(?i)FLUSHALL".to_string(),
                r"(?i)CONFIG\s+SET".to_string(),
                r"(?i)SCRIPT\s+LOAD".to_string(),
            ],
        }
    }
}

/// Save default patterns to a file (for customization)
pub fn save_default_patterns(path: &Path) -> Result<(), ScanError> {
    let patterns = ThreatPatterns::default();
    let json = serde_json::to_string_pretty(&patterns)
        .map_err(|e| ScanError::InvalidInput(format!("Failed to serialize patterns: {e}")))?;

    fs::write(path, json)
        .map_err(|e| ScanError::InvalidInput(format!("Failed to write patterns file: {e}")))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_patterns() {
        let patterns = ThreatPatterns::default();
        assert!(!patterns.prompt_injection.is_empty());
        assert!(!patterns.command_injection.is_empty());
        assert!(!patterns.path_traversal.is_empty());
        assert!(!patterns.sql_injection.is_empty());
    }

    #[test]
    fn test_pattern_serialization() {
        let patterns = ThreatPatterns::default();
        let json = serde_json::to_string(&patterns).unwrap();
        let loaded: ThreatPatterns = serde_json::from_str(&json).unwrap();

        assert_eq!(
            patterns.prompt_injection.len(),
            loaded.prompt_injection.len()
        );
        assert_eq!(
            patterns.command_injection.len(),
            loaded.command_injection.len()
        );
    }
}