cipher-shield 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +666 -0
- package/index.js +75 -0
- package/package.json +57 -0
- package/src/core/aesEngine.js +369 -0
- package/src/core/blacklistMem.js +510 -0
- package/src/core/defconSystem.js +301 -0
- package/src/magicAuth.js +272 -0
- package/src/modules/aiScanner.js +482 -0
- package/src/modules/ghostHandler.js +279 -0
- package/src/modules/shadowHandler.js +266 -0
- package/src/shield.js +694 -0
- package/src/smartLogger.js +268 -0
- package/src/sslManager.js +345 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Omindu Dissanayaka
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
# cipher-shield 🛡️
|
|
2
|
+
|
|
3
|
+
**Industrial-grade security middleware for Node.js/Express applications**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/OminduDissanayaka/cipher-shield)
|
|
6
|
+
[](https://www.npmjs.com/package/cipher-shield)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
|
|
10
|
+
cipher-shield provides **layered active defense** against unauthorized access, automated threats, and data breaches. Built for production environments requiring robust security.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 📋 Table of Contents
|
|
15
|
+
|
|
16
|
+
- [⚡ 5-Minute Setup](#-5-minute-setup)
|
|
17
|
+
- [✨ Features Overview](#-features-overview)
|
|
18
|
+
- [🔐 AES Vault](#-aes-vault)
|
|
19
|
+
- [👻 Ghost Routes](#-ghost-routes)
|
|
20
|
+
- [🌑 Shadow Ban](#-shadow-ban)
|
|
21
|
+
- [🤖 AI Gate](#-ai-gate)
|
|
22
|
+
- [🚨 DEFCON System](#-defcon-system)
|
|
23
|
+
- [📋 Smart Blacklist](#-smart-blacklist)
|
|
24
|
+
- [🛡️ Helmet Security](#️-helmet-security)
|
|
25
|
+
- [🚦 Rate Limiting](#-rate-limiting)
|
|
26
|
+
- [🔒 SSL Manager](#-ssl-manager)
|
|
27
|
+
- [🏷️ Security Signature](#️-security-signature)
|
|
28
|
+
- [📝 Smart Logger](#-smart-logger)
|
|
29
|
+
- [🔑 Magic Auth](#-magic-auth)
|
|
30
|
+
- [📚 API Reference](#-api-reference)
|
|
31
|
+
- [⚠️ Security Best Practices](#️-security-best-practices)
|
|
32
|
+
- [🔍 Troubleshooting](#-troubleshooting)
|
|
33
|
+
- [📞 Support](#-support)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## ⚡ 5-Minute Setup
|
|
38
|
+
|
|
39
|
+
Get protected immediately with minimal configuration:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install cipher-shield
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const express = require('express');
|
|
47
|
+
const cipherShield = require('cipher-shield');
|
|
48
|
+
|
|
49
|
+
const app = express();
|
|
50
|
+
app.use(express.json());
|
|
51
|
+
|
|
52
|
+
// 🔒 Add security middleware
|
|
53
|
+
app.use(cipherShield({
|
|
54
|
+
ghostRoutes: ['/admin', '.env'], // Block unauthorized routes
|
|
55
|
+
shadowBan: { enabled: true, delayTime: 5000 }, // Delay malicious IPs
|
|
56
|
+
encryption: { // Encrypt sensitive data
|
|
57
|
+
enabled: true,
|
|
58
|
+
algorithm: 'aes-256-gcm',
|
|
59
|
+
secretKey: 'your-32-character-secret-key!!!'
|
|
60
|
+
}
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
app.get('/', (req, res) => {
|
|
64
|
+
res.json({ message: 'Your app is now protected! 🛡️' });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
app.listen(3000, () => {
|
|
68
|
+
console.log('🔒 Secure server running on port 3000');
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Test it:** Visit `http://localhost:3000/admin` (blocked) or `http://localhost:3000` (allowed).
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## ✨ Features Overview
|
|
77
|
+
|
|
78
|
+
| Feature | Description | Status |
|
|
79
|
+
|---------|-------------|--------|
|
|
80
|
+
| 🔐 **AES Vault** | Multi-algorithm encryption (GCM/CBC/CTR) with automatic key validation | ✅ Production-ready |
|
|
81
|
+
| 👻 **Ghost Routes** | Honeypot detection for unauthorized scanners | ✅ Production-ready |
|
|
82
|
+
| 🌑 **Shadow Ban** | Time-wasting delays for suspicious IPs | ✅ Production-ready |
|
|
83
|
+
| 🤖 **AI Gate** | AI-powered payload analysis (Gemini/OpenAI) | ✅ Production-ready |
|
|
84
|
+
| 🚨 **DEFCON System** | Adaptive threat escalation with automatic response | ✅ Production-ready |
|
|
85
|
+
| 📋 **Smart Blacklist** | IP management with automatic expiry and analytics | ✅ Production-ready |
|
|
86
|
+
| 🛡️ **Helmet Security** | Comprehensive HTTP security headers (CSP, HSTS, XSS) | ✅ Production-ready |
|
|
87
|
+
| 🚦 **Rate Limiting** | DDoS protection with configurable limits | ✅ Production-ready |
|
|
88
|
+
| 🔒 **SSL Manager** | Automated Let's Encrypt certificate management | ✅ Production-ready |
|
|
89
|
+
| 🏷️ **Security Signature** | Customizable security headers with branding | ✅ Production-ready |
|
|
90
|
+
| 📝 **Smart Logger** | Intelligent logging with automatic data masking | ✅ Production-ready |
|
|
91
|
+
| 🔑 **Magic Auth** | Complete JWT authentication system | ✅ Production-ready |
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## 🔐 AES Vault
|
|
96
|
+
|
|
97
|
+
**Advanced encryption with multiple algorithms and automatic key validation.**
|
|
98
|
+
|
|
99
|
+
Provides secure data encryption/decryption with support for AES-GCM (recommended), AES-CBC, and AES-CTR modes. Includes automatic key length validation and secure key generation.
|
|
100
|
+
|
|
101
|
+
### Usage
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
const cipherShield = require('cipher-shield');
|
|
105
|
+
|
|
106
|
+
// Enable encryption middleware
|
|
107
|
+
app.use(cipherShield({
|
|
108
|
+
encryption: {
|
|
109
|
+
enabled: true,
|
|
110
|
+
algorithm: 'aes-256-gcm', // Recommended for new projects
|
|
111
|
+
secretKey: process.env.ENCRYPTION_KEY
|
|
112
|
+
}
|
|
113
|
+
}));
|
|
114
|
+
|
|
115
|
+
// Client-side encryption
|
|
116
|
+
const { encrypt } = require('cipher-shield');
|
|
117
|
+
const encrypted = encrypt(JSON.stringify({ sensitive: 'data' }), key, 'aes-256-gcm');
|
|
118
|
+
|
|
119
|
+
// Server automatically decrypts req.body
|
|
120
|
+
app.post('/api/secure', (req, res) => {
|
|
121
|
+
console.log(req.body); // Automatically decrypted
|
|
122
|
+
res.json({ received: req.body });
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Supported Algorithms
|
|
127
|
+
|
|
128
|
+
| Algorithm | Key Length | Mode | Use Case |
|
|
129
|
+
|-----------|------------|------|----------|
|
|
130
|
+
| `aes-128-gcm` | 16 chars | GCM (AEAD) | **Balanced security/performance** |
|
|
131
|
+
| `aes-256-gcm` | 32 chars | GCM (AEAD) | **Maximum security** |
|
|
132
|
+
| `aes-128-cbc` | 16 chars | CBC | Legacy compatibility |
|
|
133
|
+
| `aes-256-cbc` | 32 chars | CBC | Legacy compatibility |
|
|
134
|
+
| `aes-128-ctr` | 16 chars | CTR | High performance |
|
|
135
|
+
| `aes-256-ctr` | 32 chars | CTR | High performance |
|
|
136
|
+
|
|
137
|
+
### Key Generation
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
const { generateKey } = require('cipher-shield');
|
|
141
|
+
|
|
142
|
+
// Generate a secure key for your algorithm
|
|
143
|
+
const key = generateKey('aes-256-gcm'); // Returns 32-character key
|
|
144
|
+
console.log('Your secure key:', key);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 👻 Ghost Routes
|
|
150
|
+
|
|
151
|
+
**Honeypot detection that identifies and blocks unauthorized route scanners.**
|
|
152
|
+
|
|
153
|
+
Automatically detects access to common attack vectors like admin panels, configuration files, and development endpoints. Uses pattern matching and wildcard support.
|
|
154
|
+
|
|
155
|
+
### Usage
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
app.use(cipherShield({
|
|
159
|
+
ghostRoutes: [
|
|
160
|
+
'/admin', // Direct match
|
|
161
|
+
'/wp-admin', // WordPress admin
|
|
162
|
+
'/phpmyadmin', // Database admin
|
|
163
|
+
'.env', // Environment file
|
|
164
|
+
'/config.json', // Config file
|
|
165
|
+
'/.git', // Git repository
|
|
166
|
+
'/backup.zip' // Backup file
|
|
167
|
+
]
|
|
168
|
+
}));
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Pattern Matching
|
|
172
|
+
|
|
173
|
+
- **Exact Match**: `/admin` matches exactly
|
|
174
|
+
- **Prefix Match**: `/api` matches `/api/users`, `/api/posts`
|
|
175
|
+
- **File Extension**: `.env` matches any `.env` file
|
|
176
|
+
- **Wildcard**: `/admin/*` matches `/admin/users`, `/admin/settings`
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## 🌑 Shadow Ban
|
|
181
|
+
|
|
182
|
+
**Time-wasting delays that frustrate malicious automated scanners.**
|
|
183
|
+
|
|
184
|
+
Applies configurable delays to blacklisted IPs, making automated attacks inefficient while maintaining normal user experience.
|
|
185
|
+
|
|
186
|
+
### Usage
|
|
187
|
+
|
|
188
|
+
```javascript
|
|
189
|
+
app.use(cipherShield({
|
|
190
|
+
shadowBan: {
|
|
191
|
+
enabled: true,
|
|
192
|
+
delayTime: 10000 // 10-second delay for blacklisted IPs
|
|
193
|
+
}
|
|
194
|
+
}));
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Configuration Options
|
|
198
|
+
|
|
199
|
+
| Option | Type | Default | Description |
|
|
200
|
+
|--------|------|---------|-------------|
|
|
201
|
+
| `enabled` | boolean | `true` | Enable shadow ban protection |
|
|
202
|
+
| `delayTime` | number | `5000` | Delay in milliseconds |
|
|
203
|
+
| `maxDelay` | number | `30000` | Maximum delay time |
|
|
204
|
+
| `whitelist` | string[] | `[]` | IPs to exclude from delays |
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## 🤖 AI Gate
|
|
209
|
+
|
|
210
|
+
**AI-powered threat detection using Gemini or OpenAI for payload analysis.**
|
|
211
|
+
|
|
212
|
+
Scans request payloads for malicious patterns, SQL injection, XSS attempts, and other threats using advanced AI models.
|
|
213
|
+
|
|
214
|
+
### Usage
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
app.use(cipherShield({
|
|
218
|
+
aiGate: {
|
|
219
|
+
enabled: true,
|
|
220
|
+
provider: 'gemini', // or 'openai'
|
|
221
|
+
gemini: {
|
|
222
|
+
apiKey: process.env.GEMINI_API_KEY,
|
|
223
|
+
model: 'gemini-pro'
|
|
224
|
+
},
|
|
225
|
+
scanRoutes: ['/api/*'], // Scan all API routes
|
|
226
|
+
failSafe: true // Continue if AI fails
|
|
227
|
+
}
|
|
228
|
+
}));
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Configuration Options
|
|
232
|
+
|
|
233
|
+
| Option | Type | Default | Description |
|
|
234
|
+
|--------|------|---------|-------------|
|
|
235
|
+
| `enabled` | boolean | `false` | Enable AI threat detection |
|
|
236
|
+
| `provider` | string | `'gemini'` | AI provider (`gemini` or `openai`) |
|
|
237
|
+
| `scanRoutes` | string[] | `['/api/*']` | Routes to scan |
|
|
238
|
+
| `failSafe` | boolean | `true` | Continue on AI failure |
|
|
239
|
+
| `cacheResults` | boolean | `true` | Cache scan results |
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## 🚨 DEFCON System
|
|
244
|
+
|
|
245
|
+
**Adaptive threat escalation that automatically adjusts security levels.**
|
|
246
|
+
|
|
247
|
+
Monitors threat patterns and escalates security measures from GREEN (normal) to RED (maximum security) based on detected threats.
|
|
248
|
+
|
|
249
|
+
### Usage
|
|
250
|
+
|
|
251
|
+
```javascript
|
|
252
|
+
app.use(cipherShield({
|
|
253
|
+
adaptiveDefcon: true // Enable automatic escalation
|
|
254
|
+
}));
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### DEFCON Levels
|
|
258
|
+
|
|
259
|
+
- **GREEN**: Normal operation, standard security
|
|
260
|
+
- **YELLOW**: Elevated threat, increased monitoring
|
|
261
|
+
- **ORANGE**: High threat, reduced AI scanning
|
|
262
|
+
- **RED**: Maximum threat, minimal processing
|
|
263
|
+
|
|
264
|
+
### Manual Control
|
|
265
|
+
|
|
266
|
+
```javascript
|
|
267
|
+
const { defconSystem } = require('cipher-shield');
|
|
268
|
+
|
|
269
|
+
// Check current status
|
|
270
|
+
const status = defconSystem.getStatus();
|
|
271
|
+
|
|
272
|
+
// Manual escalation
|
|
273
|
+
defconSystem.escalate('DDoS_ATTACK_DETECTED');
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## 📋 Smart Blacklist
|
|
279
|
+
|
|
280
|
+
**Intelligent IP management with automatic expiry and analytics.**
|
|
281
|
+
|
|
282
|
+
Maintains blacklists with automatic cleanup, reason tracking, and performance monitoring.
|
|
283
|
+
|
|
284
|
+
### Usage
|
|
285
|
+
|
|
286
|
+
```javascript
|
|
287
|
+
const { blacklistMem } = require('cipher-shield');
|
|
288
|
+
|
|
289
|
+
// Add IP to blacklist
|
|
290
|
+
blacklistMem.add('192.168.1.100', 'suspicious_activity', 3600000); // 1 hour
|
|
291
|
+
|
|
292
|
+
// Check if IP is blacklisted
|
|
293
|
+
if (blacklistMem.isBlacklisted('192.168.1.100')) {
|
|
294
|
+
// Apply shadow ban or block
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Features
|
|
299
|
+
|
|
300
|
+
- **Automatic Expiry**: IPs removed after specified time
|
|
301
|
+
- **Reason Tracking**: Log why IPs were blacklisted
|
|
302
|
+
- **Analytics**: Track blacklist hit rates
|
|
303
|
+
- **Memory Efficient**: Optimized for high-performance
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
## 🛡️ Helmet Security
|
|
308
|
+
|
|
309
|
+
**Comprehensive HTTP security headers for passive defense.**
|
|
310
|
+
|
|
311
|
+
Applies industry-standard security headers including CSP, HSTS, XSS protection, and more.
|
|
312
|
+
|
|
313
|
+
### Usage
|
|
314
|
+
|
|
315
|
+
```javascript
|
|
316
|
+
app.use(cipherShield({
|
|
317
|
+
headers: {
|
|
318
|
+
enabled: true,
|
|
319
|
+
security: 'standard' // 'standard' or 'strict'
|
|
320
|
+
}
|
|
321
|
+
}));
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### Headers Applied
|
|
325
|
+
|
|
326
|
+
**Standard Mode:**
|
|
327
|
+
- `X-Content-Type-Options: nosniff`
|
|
328
|
+
- `X-Frame-Options: SAMEORIGIN`
|
|
329
|
+
- `X-XSS-Protection: 1; mode=block`
|
|
330
|
+
- `Referrer-Policy: strict-origin-when-cross-origin`
|
|
331
|
+
|
|
332
|
+
**Strict Mode (adds):**
|
|
333
|
+
- `Content-Security-Policy: default-src 'self'`
|
|
334
|
+
- `Strict-Transport-Security: max-age=31536000`
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
## 🚦 Rate Limiting
|
|
339
|
+
|
|
340
|
+
**DDoS protection with configurable request limits.**
|
|
341
|
+
|
|
342
|
+
Prevents brute force attacks and flood attempts using express-rate-limit.
|
|
343
|
+
|
|
344
|
+
### Usage
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
app.use(cipherShield({
|
|
348
|
+
rateLimit: {
|
|
349
|
+
enabled: true,
|
|
350
|
+
windowMs: 900000, // 15 minutes
|
|
351
|
+
max: 100 // 100 requests per window
|
|
352
|
+
}
|
|
353
|
+
}));
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Configuration Options
|
|
357
|
+
|
|
358
|
+
| Option | Type | Default | Description |
|
|
359
|
+
|--------|------|---------|-------------|
|
|
360
|
+
| `windowMs` | number | `900000` | Time window (15 min) |
|
|
361
|
+
| `max` | number | `100` | Max requests per window |
|
|
362
|
+
| `message` | string | `'Too many requests'` | Error message |
|
|
363
|
+
| `skipSuccessfulRequests` | boolean | `false` | Count successful requests |
|
|
364
|
+
| `skipFailedRequests` | boolean | `false` | Count failed requests |
|
|
365
|
+
|
|
366
|
+
---
|
|
367
|
+
|
|
368
|
+
## 🔒 SSL Manager
|
|
369
|
+
|
|
370
|
+
**Automated SSL certificate management with Let's Encrypt ACME.**
|
|
371
|
+
|
|
372
|
+
> ⚠️ **Requires VPS Server**: SSL Manager needs full file system access and HTTP challenge capabilities. **Not compatible with shared hosting.**
|
|
373
|
+
|
|
374
|
+
### Usage
|
|
375
|
+
|
|
376
|
+
```javascript
|
|
377
|
+
const { CipherShield } = require('cipher-shield');
|
|
378
|
+
|
|
379
|
+
const shield = new CipherShield({
|
|
380
|
+
// Basic configuration
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
await shield.enableSSL({
|
|
384
|
+
email: 'admin@yourdomain.com',
|
|
385
|
+
domains: ['yourdomain.com', 'www.yourdomain.com'],
|
|
386
|
+
staging: true // Use false for production
|
|
387
|
+
});
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Requirements
|
|
391
|
+
|
|
392
|
+
- ✅ **VPS Server** with full file system access
|
|
393
|
+
- ✅ **Port 80** accessible for HTTP challenges
|
|
394
|
+
- ✅ **Domain ownership** verification
|
|
395
|
+
- ❌ **Shared hosting** (cPanel, Plesk, etc.)
|
|
396
|
+
|
|
397
|
+
### Features
|
|
398
|
+
|
|
399
|
+
- **Automatic Renewal**: Certificates renew before expiry
|
|
400
|
+
- **Staging Environment**: Safe testing environment
|
|
401
|
+
- **Multiple Domains**: SAN certificates supported
|
|
402
|
+
- **RSA/ECDSA Keys**: Flexible key types
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## 🏷️ Security Signature
|
|
407
|
+
|
|
408
|
+
**Customizable security headers with flexible branding.**
|
|
409
|
+
|
|
410
|
+
Adds identifiable headers to responses for security attribution and monitoring.
|
|
411
|
+
|
|
412
|
+
### Usage
|
|
413
|
+
|
|
414
|
+
```javascript
|
|
415
|
+
app.use(cipherShield({
|
|
416
|
+
signature: {
|
|
417
|
+
enabled: true,
|
|
418
|
+
headerValue: 'My Security System',
|
|
419
|
+
customHeaderName: 'X-Secured-By'
|
|
420
|
+
}
|
|
421
|
+
}));
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
### Configuration Options
|
|
425
|
+
|
|
426
|
+
| Option | Type | Default | Description |
|
|
427
|
+
|--------|------|---------|-------------|
|
|
428
|
+
| `enabled` | boolean | `true` | Enable signature headers |
|
|
429
|
+
| `headerValue` | string | `'CipherShield'` | Header value |
|
|
430
|
+
| `customHeaderName` | string | `'X-Protected-By'` | Header name |
|
|
431
|
+
| `randomize` | boolean | `false` | Randomize header values |
|
|
432
|
+
| `customServer` | string | `null` | Custom server header |
|
|
433
|
+
|
|
434
|
+
---
|
|
435
|
+
|
|
436
|
+
## 📝 Smart Logger
|
|
437
|
+
|
|
438
|
+
**Intelligent logging with automatic sensitive data masking.**
|
|
439
|
+
|
|
440
|
+
Prevents accidental exposure of passwords, tokens, and secrets in logs through recursive scanning.
|
|
441
|
+
|
|
442
|
+
### Usage
|
|
443
|
+
|
|
444
|
+
```javascript
|
|
445
|
+
const { SmartLogger } = require('cipher-shield');
|
|
446
|
+
|
|
447
|
+
const logger = new SmartLogger({
|
|
448
|
+
maskKeys: ['password', 'token', 'secret']
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// Automatically masks sensitive data
|
|
452
|
+
logger.info('User login', {
|
|
453
|
+
username: 'john_doe',
|
|
454
|
+
password: 'secret123', // → ********
|
|
455
|
+
token: 'jwt-token' // → ********
|
|
456
|
+
});
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### Configuration Options
|
|
460
|
+
|
|
461
|
+
| Option | Type | Default | Description |
|
|
462
|
+
|--------|------|---------|-------------|
|
|
463
|
+
| `maskKeys` | string[] | `['password', 'token', 'secret']` | Keys to mask |
|
|
464
|
+
| `maskValues` | string[] | `['Bearer ', 'Token ']` | Values to mask |
|
|
465
|
+
| `maskPatterns` | RegExp[] | `[credit card patterns]` | Regex patterns |
|
|
466
|
+
| `colors` | boolean | `true` | Enable colored output |
|
|
467
|
+
| `timestamps` | boolean | `true` | Include timestamps |
|
|
468
|
+
|
|
469
|
+
---
|
|
470
|
+
|
|
471
|
+
## 🔑 Magic Auth
|
|
472
|
+
|
|
473
|
+
**Complete JWT authentication system with secure defaults.**
|
|
474
|
+
|
|
475
|
+
Provides token generation, verification, middleware, and refresh token support.
|
|
476
|
+
|
|
477
|
+
### Usage
|
|
478
|
+
|
|
479
|
+
```javascript
|
|
480
|
+
const { MagicAuth } = require('cipher-shield');
|
|
481
|
+
|
|
482
|
+
const auth = new MagicAuth({
|
|
483
|
+
secret: process.env.JWT_SECRET,
|
|
484
|
+
expiresIn: '1h'
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// Generate token
|
|
488
|
+
const token = auth.sign({ userId: 123, role: 'admin' });
|
|
489
|
+
|
|
490
|
+
// Protect routes
|
|
491
|
+
app.get('/api/profile', auth.middleware(), (req, res) => {
|
|
492
|
+
res.json({ user: req.user });
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
### Configuration Options
|
|
497
|
+
|
|
498
|
+
| Option | Type | Default | Description |
|
|
499
|
+
|--------|------|---------|-------------|
|
|
500
|
+
| `secret` | string | **required** | JWT signing secret |
|
|
501
|
+
| `expiresIn` | string | `'1h'` | Token expiration |
|
|
502
|
+
| `algorithm` | string | `'HS256'` | Signing algorithm |
|
|
503
|
+
| `issuer` | string | `undefined` | Token issuer |
|
|
504
|
+
| `audience` | string | `undefined` | Token audience |
|
|
505
|
+
|
|
506
|
+
### Methods
|
|
507
|
+
|
|
508
|
+
- `sign(payload)` - Generate JWT token
|
|
509
|
+
- `verify(token)` - Verify and decode token
|
|
510
|
+
- `middleware(roles?)` - Express authentication middleware
|
|
511
|
+
- `generateTokens(payload)` - Create access + refresh tokens
|
|
512
|
+
- `refreshAccessToken(refreshToken)` - Refresh access token
|
|
513
|
+
|
|
514
|
+
---
|
|
515
|
+
|
|
516
|
+
## 📚 API Reference
|
|
517
|
+
|
|
518
|
+
### Core Functions
|
|
519
|
+
|
|
520
|
+
```javascript
|
|
521
|
+
const cipherShield = require('cipher-shield');
|
|
522
|
+
|
|
523
|
+
// Main middleware
|
|
524
|
+
const middleware = cipherShield(config);
|
|
525
|
+
|
|
526
|
+
// Encryption utilities
|
|
527
|
+
const { encrypt, decrypt, generateKey } = require('cipher-shield');
|
|
528
|
+
|
|
529
|
+
// Classes
|
|
530
|
+
const { SmartLogger, MagicAuth, CipherShield } = require('cipher-shield');
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
### Configuration Schema
|
|
534
|
+
|
|
535
|
+
```javascript
|
|
536
|
+
const config = {
|
|
537
|
+
// Core Security
|
|
538
|
+
ghostRoutes: ['/admin', '.env'],
|
|
539
|
+
shadowBan: { enabled: true, delayTime: 5000 },
|
|
540
|
+
encryption: {
|
|
541
|
+
enabled: true,
|
|
542
|
+
algorithm: 'aes-256-gcm',
|
|
543
|
+
secretKey: process.env.ENCRYPTION_KEY
|
|
544
|
+
},
|
|
545
|
+
|
|
546
|
+
// Advanced Features
|
|
547
|
+
aiGate: { enabled: false },
|
|
548
|
+
adaptiveDefcon: true,
|
|
549
|
+
|
|
550
|
+
// Passive Defense
|
|
551
|
+
headers: { enabled: true, security: 'standard' },
|
|
552
|
+
rateLimit: { enabled: true, windowMs: 900000, max: 100 },
|
|
553
|
+
signature: { enabled: true, headerValue: 'CipherShield' }
|
|
554
|
+
};
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
---
|
|
558
|
+
|
|
559
|
+
## ⚠️ Security Best Practices
|
|
560
|
+
|
|
561
|
+
### 🔐 Key Management
|
|
562
|
+
- Use environment variables for all secrets
|
|
563
|
+
- Generate keys with `generateKey()` function
|
|
564
|
+
- Rotate keys regularly in production
|
|
565
|
+
- Use different keys for different environments
|
|
566
|
+
|
|
567
|
+
### 🛡️ Production Setup
|
|
568
|
+
```javascript
|
|
569
|
+
// .env file
|
|
570
|
+
ENCRYPTION_KEY=your-32-character-secret-key!!!
|
|
571
|
+
JWT_SECRET=your-jwt-signing-secret-here
|
|
572
|
+
GEMINI_API_KEY=your-ai-api-key
|
|
573
|
+
NODE_ENV=production
|
|
574
|
+
|
|
575
|
+
// server.js
|
|
576
|
+
require('dotenv').config();
|
|
577
|
+
|
|
578
|
+
const shield = cipherShield({
|
|
579
|
+
encryption: { secretKey: process.env.ENCRYPTION_KEY },
|
|
580
|
+
aiGate: { gemini: { apiKey: process.env.GEMINI_API_KEY } }
|
|
581
|
+
});
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
### 📊 Monitoring
|
|
585
|
+
- Enable debug logging in development
|
|
586
|
+
- Monitor DEFCON status in production
|
|
587
|
+
- Log security events for analysis
|
|
588
|
+
- Set up alerts for threat escalation
|
|
589
|
+
|
|
590
|
+
---
|
|
591
|
+
|
|
592
|
+
## 🔍 Troubleshooting
|
|
593
|
+
|
|
594
|
+
### Common Issues
|
|
595
|
+
|
|
596
|
+
**"Key length mismatch"**
|
|
597
|
+
```javascript
|
|
598
|
+
// Use correct key length for algorithm
|
|
599
|
+
const key = generateKey('aes-256-gcm'); // 32 characters
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
**"AI Gate not working"**
|
|
603
|
+
```javascript
|
|
604
|
+
// Check API key and network
|
|
605
|
+
console.log('API Key:', !!process.env.GEMINI_API_KEY);
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
**"SSL Manager fails on shared hosting"**
|
|
609
|
+
```javascript
|
|
610
|
+
// SSL Manager requires VPS server
|
|
611
|
+
// Shared hosting doesn't support ACME challenges
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
**"MagicAuth tokens invalid"**
|
|
615
|
+
```javascript
|
|
616
|
+
// Ensure consistent secrets across restarts
|
|
617
|
+
const auth = new MagicAuth({
|
|
618
|
+
secret: process.env.JWT_SECRET // Same secret always
|
|
619
|
+
});
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
### Debug Mode
|
|
623
|
+
|
|
624
|
+
```javascript
|
|
625
|
+
const shield = cipherShield({
|
|
626
|
+
debug: true // Enable verbose logging
|
|
627
|
+
});
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
---
|
|
631
|
+
|
|
632
|
+
## 📞 Support
|
|
633
|
+
|
|
634
|
+
### 🆘 Getting Help
|
|
635
|
+
|
|
636
|
+
- 📧 **Email**: hellow@omindu.dev
|
|
637
|
+
- 🐛 **Issues**: [GitHub Issues](https://github.com/OminduDissanayaka/cipher-shield/issues)
|
|
638
|
+
- 📖 **Documentation**: [Website](https://github.com/OminduDissanayaka/cipher-shield)
|
|
639
|
+
|
|
640
|
+
### 🤝 Contributing
|
|
641
|
+
|
|
642
|
+
1. Fork the repository
|
|
643
|
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
644
|
+
3. Commit changes (`git commit -m 'Add amazing feature'`)
|
|
645
|
+
4. Push to branch (`git push origin feature/amazing-feature`)
|
|
646
|
+
5. Open a Pull Request
|
|
647
|
+
|
|
648
|
+
### 📄 License
|
|
649
|
+
|
|
650
|
+
**MIT License** - See [LICENSE](LICENSE) file for details.
|
|
651
|
+
|
|
652
|
+
---
|
|
653
|
+
|
|
654
|
+
## 🎯 Quick Checklist
|
|
655
|
+
|
|
656
|
+
- [ ] Node.js >= 14.0.0 installed
|
|
657
|
+
- [ ] `npm install cipher-shield` executed
|
|
658
|
+
- [ ] Environment variables configured
|
|
659
|
+
- [ ] Security configuration added
|
|
660
|
+
- [ ] Application tested with protection enabled
|
|
661
|
+
|
|
662
|
+
---
|
|
663
|
+
|
|
664
|
+
**Built with ❤️ by [Omindu Dissanayaka](https://omindu.dev)**
|
|
665
|
+
|
|
666
|
+
|