@dimzxzzx07/file-watcher
v1.1.0
Published
Enterprise-grade secure file watcher with military-grade integrity checking
Maintainers
Readme
Secure File Watcher Enterprise v1.0
Table of Contents
- Overview
- Features
- Quick Start
- Installation
- Configuration
- Usage Examples
- Project Structure
- API Reference
- Event System
- Security Architecture
- Performance Metrics
- Testing
- Troubleshooting
- License
Overview
Secure File Watcher Enterprise is a military-grade file integrity monitoring system that provides real-time protection against unauthorized file modifications, code injections, malware, and various security threats. Built with zero-trust architecture, it validates every file from multiple aspects including hash verification, digital signatures, metadata analysis, and behavioral patterns.
This system is designed for enterprise environments where security is paramount. It operates at multiple levels - from file system monitoring to memory protection - ensuring comprehensive coverage against both external attacks and internal threats.
Features
Core Security
| Feature | Description | |---------|-------------| | Real-time Monitoring | Detects file changes in milliseconds using optimized file system watchers | | Multi-algorithm Hashing | Combines SHA512 and BLAKE2b with unique salts for maximum integrity | | Digital Signatures | RSA-2048 signature verification for critical files | | Auto Rollback | Automatically restores files from verified backups when tampering detected | | Quarantine System | Isolates suspicious files with AES-256 encryption |
Advanced Protection
| Protection | Capability | |------------|------------| | Injection Detection | AST analysis for JavaScript/TypeScript, pattern matching for malicious code | | Memory Guard | Prevents debugger attachment, memory tampering, and heap inspection | | Process Guard | Monitors for process hijacking, fork bombs, and abnormal behavior | | File Guard | Implements file locking, permission monitoring, and backup management | | Anomaly Detection | Analyzes file entropy, timing patterns, and structural anomalies |
Monitoring & Logging
| Component | Description | |-----------|-------------| | Comprehensive Logging | Multiple log levels with rotation and retention policies | | Security Alerts | Real-time notifications for critical security events | | Performance Metrics | CPU, memory, and disk usage monitoring | | Audit Trail | Complete history of file changes and security events |
Quick Start
Get up and running in minutes with these simple steps:
# Install the package
npm install @secure/file-watcher-enterprise
# Create a basic configuration file
echo 'WATCH_DIR=./src
INTEGRITY_LEVEL=advanced' > .env
# Start watching
npx secure-watchThat's it! The system will now monitor your files and alert you of any unauthorized changes.
Installation
From NPM
# Install as dependency
npm install @secure/file-watcher-enterprise
# Install globally
npm install -g @secure/file-watcher-enterpriseSystem Requirements
Requirement Minimum Recommended Node.js 18.0.0 20.0.0 or higher RAM 1 GB 2 GB Disk Space 1 GB 5 GB CPU 1 core 2 cores OS Linux, macOS, Windows Linux (production)
Configuration
Environment Variables (.env)
Create a .env file in your project root:
# Core Configuration
WATCH_DIR=./src
HASH_ALGORITHM=sha512
INTEGRITY_LEVEL=paranoid
AUTO_ROLLBACK=true
SIGNATURE_VERIFICATION=true
# Monitoring Settings
SCAN_INTERVAL=30
REALTIME_MONITORING=true
ALERT_THRESHOLD=medium
# File Handling
MAX_FILE_SIZE=10485760
ALLOWED_EXTENSIONS=.js,.ts,.json,.txt,.md
QUARANTINE_ENABLED=true
# Security Guards
ENABLE_MEMORY_GUARD=true
ENABLE_PROCESS_GUARD=true
ENABLE_FILE_GUARD=true
# Logging
LOG_LEVEL=info
LOG_RETENTION_DAYS=30
MAX_LOG_SIZE=10485760Configuration Object
interface SecurityConfig {
watchDir: string;
hashAlgorithm: 'sha256' | 'sha384' | 'sha512' | 'blake2b';
backupDir: string;
quarantineDir: string;
maxFileSize: number;
scanInterval: number;
realtimeMonitoring: boolean;
autoRollback: boolean;
quarantineEnabled: boolean;
signatureVerification: boolean;
integrityLevel: 'basic' | 'advanced' | 'paranoid';
alertThreshold: 'low' | 'medium' | 'high' | 'critical';
allowedExtensions: string[];
blockedPatterns: RegExp[];
trustedSigners: string[];
}Configuration Levels
Level Description basic Standard file monitoring with hash verification advanced Enhanced security with pattern detection and anomaly analysis paranoid Maximum security with memory guards, process monitoring, and aggressive scanning
Usage Examples
Basic Implementation
import { SecureFileWatcher } from '@secure/file-watcher-enterprise';
// Initialize with default configuration
const watcher = new SecureFileWatcher();
// Start monitoring
watcher.startWatching();Custom Configuration
import { SecureFileWatcher } from '@secure/file-watcher-enterprise';
const watcher = new SecureFileWatcher({
watchDir: './src',
hashAlgorithm: 'sha512',
integrityLevel: 'paranoid',
autoRollback: true,
maxFileSize: 10 * 1024 * 1024, // 10MB
allowedExtensions: ['.js', '.ts', '.json']
});
watcher.startWatching();Event Handling
import { SecureFileWatcher } from '@secure/file-watcher-enterprise';
const watcher = new SecureFileWatcher();
// File events
watcher.on('file:added', (data) => {
console.log(`File added: ${data.filePath}`);
console.log(`File size: ${data.metadata.size} bytes`);
console.log(`File hash: ${data.metadata.hash}`);
});
watcher.on('file:changed', (data) => {
console.log(`File modified: ${data.filePath}`);
console.log(`Old hash: ${data.oldMetadata.hash}`);
console.log(`New hash: ${data.newMetadata.hash}`);
});
watcher.on('file:deleted', (data) => {
console.log(`File deleted: ${data.filePath}`);
});
// Security events
watcher.on('integrity:violation', (violation) => {
console.error('Security violation detected!');
console.error(`Type: ${violation.violationType}`);
console.error(`Severity: ${violation.severity}`);
console.error(`File: ${violation.filePath}`);
console.error(`Action taken: ${violation.actionTaken}`);
});
watcher.on('security:alert', (alert) => {
console.warn(`Security alert: ${alert.message}`);
console.warn(`Level: ${alert.level}`);
console.warn(`Source: ${alert.source}`);
});
// Start watching
watcher.startWatching();Advanced Implementation
import {
WatcherEngine,
SecurityManager,
BackupManager,
IntegrityValidator
} from '@secure/file-watcher-enterprise';
import * as path from 'path';
import * as fs from 'fs/promises';
class CustomSecurityMonitor {
private watcher: WatcherEngine;
private security: SecurityManager;
private backup: BackupManager;
private validator: IntegrityValidator;
constructor(config: any) {
this.watcher = new WatcherEngine(config);
this.security = SecurityManager.getInstance(config);
this.backup = new BackupManager(config);
this.validator = new IntegrityValidator(config);
this.setupEventHandlers();
}
private setupEventHandlers(): void {
this.watcher.on('file:added', async (file) => {
// Scan new file
const isClean = await this.security.scanFile(file.filePath);
if (!isClean) {
console.log(`Malware detected in ${file.filePath}`);
// Quarantine file
await this.backup.quarantineFile(file.filePath, 'malware_detected');
// Log incident
this.security.logAlert({
id: crypto.randomUUID(),
timestamp: new Date(),
level: 'critical',
source: 'scanner',
message: 'Malware detected in new file',
details: { filePath: file.filePath }
});
} else {
// Create backup of clean file
await this.backup.createBackup(file.filePath, file.metadata);
}
});
this.watcher.on('file:changed', async (file) => {
// Validate change
const isValid = await this.validator.validateFile(
file.filePath,
file.newMetadata
);
if (!isValid) {
console.log(`Invalid file change detected: ${file.filePath}`);
// Rollback to previous version
const restored = await this.backup.rollbackFile(file.filePath);
if (restored) {
console.log(`File restored from backup`);
} else {
console.error(`Failed to restore file`);
}
}
});
this.watcher.on('integrity:violation', async (violation) => {
// Log to file
await fs.appendFile(
'security-incidents.log',
JSON.stringify(violation) + '\n'
);
// Take action based on severity
if (violation.severity === 'critical') {
// Emergency shutdown
process.exit(1);
}
});
}
public start(): void {
this.watcher.startWatching();
console.log('Custom security monitor started');
}
public async getStatus(): Promise<any> {
return {
watcher: this.watcher.getStatus(),
security: this.security.getStatus(),
backup: this.backup.getStatus()
};
}
}
// Usage
const monitor = new CustomSecurityMonitor({
watchDir: './secure-files',
integrityLevel: 'paranoid'
});
monitor.start();
// Check status after 1 minute
setTimeout(async () => {
const status = await monitor.getStatus();
console.log('Monitor status:', status);
}, 60000);Express.js Integration
import express from 'express';
import { SecureFileWatcher, SecurityManager } from '@secure/file-watcher-enterprise';
const app = express();
const watcher = new SecureFileWatcher({
watchDir: './public',
autoRollback: true
});
// Middleware for security headers
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-XSS-Protection', '1; mode=block');
next();
});
// API endpoints
app.get('/api/status', (req, res) => {
res.json({
status: 'running',
watcher: watcher.getStatus(),
uptime: process.uptime()
});
});
app.get('/api/alerts', (req, res) => {
const security = SecurityManager.getInstance();
const alerts = security.getAlerts();
res.json(alerts);
});
app.get('/api/files', (req, res) => {
const watched = Array.from(watcher['fileCache'].keys());
res.json({ watchedFiles: watched });
});
app.post('/api/scan', async (req, res) => {
const security = SecurityManager.getInstance();
const results = [];
for (const [filePath] of watcher['fileCache']) {
const isClean = await security.scanFile(filePath);
results.push({ filePath, isClean });
}
res.json({ scanResults: results });
});
// Start server
app.listen(3000, () => {
console.log('Server running on port 3000');
watcher.startWatching();
console.log('File watcher started');
});CLI Usage
# Basic usage
secure-watch
# Watch specific directory
secure-watch --watch /path/to/project
# Set integrity level
secure-watch --level paranoid
# Custom configuration file
secure-watch --config ./config.json
# Generate security report
secure-watch --report --output report.json
# Validate specific file
secure-watch --validate ./file.js
# Restore from backup
secure-watch --restore ./file.js --version 3
# Show status
secure-watch --status
# Run in daemon mode
secure-watch --daemonProgrammatic CLI
import { program } from 'commander';
import { SecureFileWatcher } from '@secure/file-watcher-enterprise';
program
.version('2.0.0')
.description('Secure File Watcher CLI')
.option('-w, --watch <path>', 'Directory to watch', process.cwd())
.option('-l, --level <level>', 'Integrity level (basic|advanced|paranoid)', 'advanced')
.option('-c, --config <path>', 'Configuration file path')
.option('-r, --report', 'Generate security report')
.option('-o, --output <file>', 'Output file for report')
.option('-v, --validate <file>', 'Validate specific file')
.option('--restore <file>', 'Restore file from backup')
.option('--version <number>', 'Version to restore')
.option('--status', 'Show watcher status')
.option('--daemon', 'Run as daemon')
.parse(process.argv);
const options = program.opts();
if (options.validate) {
// Validate single file
const validator = new IntegrityValidator({});
const result = validator.validateFile(options.validate);
console.log('Validation result:', result);
process.exit(0);
}
if (options.restore) {
// Restore file
const backup = new BackupManager({});
const version = options.version ? parseInt(options.version) : undefined;
backup.rollbackFile(options.restore, version)
.then(success => {
console.log(success ? 'Restore successful' : 'Restore failed');
process.exit(0);
});
return;
}
if (options.status) {
// Show status
const watcher = new SecureFileWatcher({ watchDir: options.watch });
console.log('Watcher status:', watcher.getStatus());
process.exit(0);
}
// Start watcher
const config = {
watchDir: options.watch,
integrityLevel: options.level
};
const watcher = new SecureFileWatcher(config);
if (options.daemon) {
// Run as daemon
process.on('SIGINT', () => {
watcher.stopWatching();
process.exit(0);
});
}
watcher.startWatching();
console.log(`Watching directory: ${options.watch}`);
console.log(`Integrity level: ${options.level}`);
if (options.report) {
// Generate report after 10 seconds
setTimeout(async () => {
const status = watcher.getStatus();
const output = options.output || 'security-report.json';
await fs.writeFile(output, JSON.stringify(status, null, 2));
console.log(`Report saved to ${output}`);
}, 10000);
}Project Structure
secure-file-watcher/
├── src/
│ ├── core/
│ │ ├── WatcherEngine.ts
│ │ ├── IntegrityValidator.ts
│ │ ├── SecurityManager.ts
│ │ └── BackupManager.ts
│ ├── detectors/
│ │ ├── InjectionDetector.ts
│ │ ├── AnomalyDetector.ts
│ │ └── PatternMatcher.ts
│ ├── crypto/
│ │ ├── HashGenerator.ts
│ │ ├── SignatureValidator.ts
│ │ └── KeyManager.ts
│ ├── guards/
│ │ ├── ProcessGuard.ts
│ │ ├── MemoryGuard.ts
│ │ └── FileGuard.ts
│ ├── utils/
│ │ ├── Logger.ts
│ │ ├── Validator.ts
│ │ └── Constants.ts
│ └── index.ts
├── tests/
│ ├── unit/
│ │ ├── core/
│ │ ├── detectors/
│ │ └── crypto/
│ ├── integration/
│ │ └── workflows.test.ts
│ └── security/
│ └── penetration.test.ts
├── dist/
├── logs/
├── backups/
├── quarantine/
├── .env
├── .eslintrc.json
├── .prettierrc
├── jest.config.js
├── tsconfig.json
├── package.json
└── README.mdAPI Reference
SecureFileWatcher Class
class SecureFileWatcher extends EventEmitter {
constructor(config?: Partial<SecurityConfig>);
// Methods
public startWatching(): void;
public stopWatching(): void;
public getStatus(): WatcherStatus;
public validateFile(filePath: string): Promise<boolean>;
public scanFile(filePath: string): Promise<ScanResult>;
// Events
on(event: 'file:added', listener: (data: FileEvent) => void): this;
on(event: 'file:changed', listener: (data: FileChangeEvent) => void): this;
on(event: 'file:deleted', listener: (data: FileEvent) => void): this;
on(event: 'file:restored', listener: (data: FileRestoreEvent) => void): this;
on(event: 'integrity:violation', listener: (violation: IntegrityViolation) => void): this;
on(event: 'security:alert', listener: (alert: SecurityAlert) => void): this;
on(event: 'security:critical', listener: (alert: SecurityAlert) => void): this;
on(event: 'watcher:error', listener: (error: Error) => void): this;
}SecurityManager Class
class SecurityManager {
static getInstance(config?: SecurityConfig): SecurityManager;
public scanFile(filePath: string): Promise<boolean>;
public validateMemoryIntegrity(): void;
public isAuthorizedChange(filePath: string, oldMeta: FileMetadata, newMeta: FileMetadata): Promise<boolean>;
public encryptFile(filePath: string): Promise<void>;
public decryptFile(filePath: string): Promise<void>;
public logAlert(alert: SecurityAlert): void;
public getAlerts(): SecurityAlert[];
public getStatus(): SecurityStatus;
}BackupManager Class
class BackupManager {
constructor(config: SecurityConfig);
public createBackup(filePath: string, metadata: FileMetadata): Promise<string>;
public rollbackFile(filePath: string, version?: number): Promise<boolean>;
public restoreFile(filePath: string): Promise<boolean>;
public quarantineFile(filePath: string, reason: string): Promise<string>;
public getBackups(filePath: string): BackupEntry[];
public getStatus(): BackupStatus;
}IntegrityValidator Class
class IntegrityValidator {
constructor(config: SecurityConfig);
public generateFileMetadata(filePath: string): Promise<FileMetadata>;
public validateFile(filePath: string, metadata: FileMetadata): Promise<boolean>;
public verifySignature(filePath: string, metadata: FileMetadata): Promise<boolean>;
public calculateHash(data: Buffer): string;
public calculateEntropy(data: Buffer): number;
}Types
interface FileMetadata {
path: string;
hash: string;
signature?: string;
size: number;
created: Date;
modified: Date;
accessed: Date;
permissions: number;
owner: string;
group: string;
inode: number;
checksum: string;
version: number;
encryptedHash?: string;
entropy: number;
magicBytes?: string;
}
interface IntegrityViolation {
id: string;
timestamp: Date;
filePath: string;
violationType: 'hash_mismatch' | 'signature_invalid' | 'injection_detected' | 'permission_changed' | 'metadata_modified' | 'quarantine_triggered' | 'self_modification' | 'memory_tamper' | 'process_hijack' | 'unexpected_file';
severity: 'low' | 'medium' | 'high' | 'critical';
oldHash?: string;
newHash?: string;
details: any;
actionTaken: 'rollback' | 'quarantine' | 'delete' | 'alert' | 'terminate' | 'ignore';
backupPath?: string;
}
interface SecurityAlert {
id: string;
timestamp: Date;
level: 'info' | 'warning' | 'error' | 'critical';
source: string;
message: string;
details: any;
stackTrace?: string;
memoryDump?: string;
}
interface FileEvent {
filePath: string;
metadata: FileMetadata;
timestamp: Date;
}
interface FileChangeEvent {
filePath: string;
oldMetadata: FileMetadata;
newMetadata: FileMetadata;
timestamp: Date;
}Event System
File Events
Event Description Payload file:added New file detected { filePath, metadata } file:changed Existing file modified { filePath, oldMetadata, newMetadata } file:deleted File removed { filePath, metadata } file:restored File restored from backup { filePath, metadata } file:quarantined File moved to quarantine { filePath, quarantinePath, reason }
Security Events
Event Description Payload integrity:violation Integrity check failed IntegrityViolation integrity:pass Integrity check passed { filePath, metadata } security:alert Security alert SecurityAlert security:critical Critical security event SecurityAlert security:debugger Debugger detected { details }
System Events
Event Description Payload watcher:start Watcher started { timestamp } watcher:stop Watcher stopped { timestamp } watcher:error Watcher error { error } scan:start Security scan started { timestamp } scan:complete Security scan completed { filesScanned, threatsFound }
Security Architecture
Layer 1: File System Monitoring
· Real-time file change detection · Metadata tracking (inode, permissions, timestamps) · File locking and permission enforcement
Layer 2: Integrity Validation
· Multi-algorithm hashing (SHA512 + BLAKE2b) · Digital signature verification · Entropy analysis · Magic byte verification
Layer 3: Threat Detection
· AST analysis for code injection · Pattern matching for malware signatures · Behavioral anomaly detection · Obfuscation detection
Layer 4: Process Protection
· Anti-debugger mechanisms · Memory integrity checking · Process hijacking prevention · Environment variable freezing
Layer 5: Response System
· Automatic file quarantine · Versioned backup restoration · Security alert generation · Emergency shutdown protocols
Performance Metrics
Benchmark Results
Operation Average Time 95th Percentile File Detection 15 ms 30 ms Hash Calculation (1MB) 8 ms 12 ms Integrity Validation 25 ms 45 ms Backup Creation 35 ms 60 ms File Restoration 40 ms 75 ms Full Directory Scan (1000 files) 3.2 s 4.5 s
Resource Usage
Resource Idle Active Peak CPU 2-5% 10-15% 25% Memory 80 MB 150 MB 250 MB Disk I/O 0.1 MB/s 1-2 MB/s 5 MB/s
Testing
Running Tests
# Run all tests
npm test
# Run unit tests
npm run test:unit
# Run integration tests
npm run test:integration
# Run security tests
npm run test:security
# Run with coverage
npm run test:coverage
# Run in watch mode
npm run test:watchTest Structure
tests/
├── unit/
│ ├── core/
│ │ ├── WatcherEngine.test.ts
│ │ ├── IntegrityValidator.test.ts
│ │ ├── SecurityManager.test.ts
│ │ └── BackupManager.test.ts
│ ├── detectors/
│ │ ├── InjectionDetector.test.ts
│ │ ├── AnomalyDetector.test.ts
│ │ └── PatternMatcher.test.ts
│ └── crypto/
│ ├── HashGenerator.test.ts
│ ├── SignatureValidator.test.ts
│ └── KeyManager.test.ts
├── integration/
│ ├── workflow.test.ts
│ ├── recovery.test.ts
│ └── performance.test.ts
└── security/
├── penetration.test.ts
├── injection.test.ts
└── stress.test.tsExample Test
import { WatcherEngine } from '../src/core/WatcherEngine';
import { createTestFile, cleanupTestFiles } from './setup';
describe('WatcherEngine', () => {
let watcher: WatcherEngine;
beforeEach(() => {
watcher = new WatcherEngine({
watchDir: './test-temp',
hashAlgorithm: 'sha512'
});
});
afterEach(async () => {
await cleanupTestFiles();
});
it('should detect file additions', (done) => {
watcher.on('file:added', (data) => {
expect(data.filePath).toContain('test.txt');
done();
});
watcher.startWatching();
createTestFile('test.txt', 'hello world');
});
it('should detect file changes', (done) => {
watcher.on('file:changed', (data) => {
expect(data.oldMetadata.hash).not.toBe(data.newMetadata.hash);
done();
});
watcher.startWatching();
createTestFile('test.txt', 'initial');
setTimeout(() => {
createTestFile('test.txt', 'modified');
}, 100);
});
});Troubleshooting
Common Issues
Issue Cause Solution High CPU usage Too many files watched Increase scan interval, add exclusions Memory leaks Large file cache Adjust max file size, enable garbage collection False positives Overly aggressive patterns Tune blocked patterns, adjust threshold Permission denied Insufficient file permissions Run with appropriate user, check file modes Backup failures Disk space full Free disk space, reduce backup retention Watcher crashes Node.js version incompatible Upgrade to Node.js 18+
Debug Mode
// Enable debug logging
process.env.DEBUG = 'secure-watcher:*';
// Or in code
const watcher = new SecureFileWatcher({
debugMode: true
});Logs Location
logs/
├── app.log # Application logs
├── error.log # Error logs only
├── security.log # Security events
└── debug.log # Debug information (when enabled)Recovery Procedures
- Stop the watcher
secure-watch --stop - Check logs for issues
tail -f logs/error.log - Restore from backups
secure-watch --restore ./corrupted-file.js --version 3 - Reset configuration
mv .env .env.backup secure-watch --init - Restart with clean state
secure-watch --reset secure-watch --start
License
UNLICENSED - Proprietary software. All rights reserved.
This software and its source code are the exclusive property of Dimzxzzx07. Unauthorized copying, modification, distribution, or use of this software is strictly prohibited without express written permission.
