Repository navigation
Expand file tree
/
Copy pathSentinel.js
More file actions
166 lines (127 loc) · 4.23 KB
/
Copy pathSentinel.js
File metadata and controls
166 lines (127 loc) · 4.23 KB
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
"use strict";
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
// ============================================================
// CONFIGURATION
// ============================================================
const WATCH_PATH = path.resolve("./target-file.bin");
const LOG_FILE = path.resolve("./sentinel.log");
const INTERVAL = 2000;
// ============================================================
// STATE
// ============================================================
let previous = {
exists: false,
hash: null,
size: null,
modified: null
};
// ============================================================
// LOGGING
// ============================================================
function log(level, message) {
const timestamp = new Date().toISOString();
const line = `[${timestamp}] [${level}] ${message}\n`;
process.stdout.write(line);
fs.appendFileSync(LOG_FILE, line);
}
// ============================================================
// SHA-256
// ============================================================
function sha256(file) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash("sha256");
const stream = fs.createReadStream(file);
stream.on("data", chunk => hash.update(chunk));
stream.on("end", () => {
resolve(hash.digest("hex"));
});
stream.on("error", reject);
});
}
// ============================================================
// FILE STATUS
// ============================================================
async function getStatus() {
try {
const stat = await fs.promises.stat(WATCH_PATH);
const hash = await sha256(WATCH_PATH);
return {
exists: true,
hash,
size: stat.size,
modified: stat.mtimeMs
};
} catch (error) {
if (error.code === "ENOENT") {
return {
exists: false,
hash: null,
size: null,
modified: null
};
}
throw error;
}
}
// ============================================================
// SENTINEL CHECK
// ============================================================
async function sentinelCheck() {
try {
const current = await getStatus();
// First observation
if (!previous.exists && current.exists) {
log("INFO", `File detected: ${WATCH_PATH}`);
log("BASELINE", `SHA-256: ${current.hash}`);
previous = current;
return;
}
// File deleted
if (previous.exists && !current.exists) {
log("ALERT", `FILE DELETED: ${WATCH_PATH}`);
previous = current;
return;
}
// Nothing to compare
if (!current.exists) {
return;
}
// Hash changed
if (current.hash !== previous.hash) {
log("ALERT", `POSSIBLE ALTERATION: ${WATCH_PATH}`);
log("CHANGE", `Old SHA-256: ${previous.hash}`);
log("CHANGE", `New SHA-256: ${current.hash}`);
log("CHANGE", `Old size: ${previous.size}`);
log("CHANGE", `New size: ${current.size}`);
}
// Metadata changed
if (current.modified !== previous.modified) {
log("INFO", `Modification timestamp changed: ${WATCH_PATH}`);
}
previous = current;
} catch (error) {
log("ERROR", error.message);
}
}
// ============================================================
// START
// ============================================================
log("INFO", "======================================");
log("INFO", "SENTINEL FILE INTEGRITY MONITOR");
log("INFO", "======================================");
log("INFO", `Monitoring: ${WATCH_PATH}`);
log("INFO", `Interval: ${INTERVAL} ms`);
sentinelCheck();
const timer = setInterval(sentinelCheck, INTERVAL);
// ============================================================
// SHUTDOWN
// ============================================================
function shutdown() {
clearInterval(timer);
log("INFO", "Sentinel stopped.");
process.exit(0);
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);