-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·181 lines (164 loc) · 4 KB
/
main.js
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
#!/usr/bin/env node
"use strict";
const meow = require("meow");
const surify = require("./surify.js");
const fs = require("fs");
const parse = require("csv-parse");
const readline = require("readline");
let stdin = "";
const main = async () => {
try {
let config = JSON.parse(fs.readFileSync(cli.flags.config));
surify.setConfig(config);
} catch (e) {
if (e.path == "config.json") {
console.error(`
No configuration file!
Set with flag -c <file>
or --config <file>
`);
process.exit();
}
}
let vars = [];
if (cli.flags.hasOwnProperty("json")) {
if (cli.flags.json !== true) {
try {
vars = JSON.parse(fs.readFileSync(cli.flags.json));
} catch (e) {
console.error(`
File ${cli.flags.json} is not a valid JSON.
`);
process.exit();
}
} else {
try {
vars = JSON.parse(stdin);
} catch (e) {
console.error(`
STDIN is not a valid JSON.
`);
process.exit();
}
}
} else if (cli.flags.hasOwnProperty("jsonl")) {
let data;
if (cli.flags.jsonl !== true) {
let buffer = await fs.readFileSync(cli.flags.jsonl);
data = await Buffer.from(buffer)
.toString()
.split("\n");
} else {
data = stdin.split("\n");
}
for (let d of data) {
if (d.length > 0) {
vars.push(JSON.parse(d));
}
}
} else if (cli.flags.hasOwnProperty("csv")) {
if (stdin.length > 0) {
console.error(`
STDIN is recently for JSON (--json) / JSON Lines (--jsonl).
`);
process.exit();
}
parse(
fs.readFileSync(cli.flags.csv),
{ delimiter: cli.flags.delimiter },
(err, data) => {
let header = data[0];
data = data.splice(1);
for (let entry in data) {
let varsEntry = {};
for (let col in data[entry]) {
varsEntry[header[col]] = data[entry][col];
}
vars.push(varsEntry);
}
}
);
} else {
vars = cli.flags;
}
surify.setSID(cli.flags.sid);
let newRules = [];
for (let template in surify.getTemplates()) {
let rules = await surify.readTemplate(template);
newRules.push(surify.setVars(rules, vars, surify.getSID()));
}
if (cli.flags.output.toLowerCase() === "log") {
console.log(surify.writeRules(newRules));
} else {
console.log(surify.writeRules(newRules, cli.flags.output));
}
};
const cli = meow(
`usage:
$ surify -c config.json [OPTIONS]
OPTIONS
=======
-v, --version show package version
-c, --config <file> set config file
-o, --output <file> set output file
log write to stdout
--json <file> parse JSON-file
--jsonl <file> parse JSONL-file
--csv <file> parse CSV-file
-d, --delimiter delimiter in CSV-file
--sid <number> set the first SID for the generated ruleset
EXAMPLES
=========
$ surify -c config.json --test1 1.2.3.4 --test2 9.9.9.9 --sid 120000 -o log
Set all test1 to "1.2.3.4" and test2 to "9.9.9.9" and start ruleset with sid 120000 and print them to stdout.
$ surify -c config.json --json example.json -o suri.rules
$ surify -c config.json --csv example.csv -d ";" -o suri_csv.rules --sid 1234
`,
{
flags: {
version: {
alias: "v",
type: "boolean"
},
config: {
alias: "c",
type: "string",
default: "config.json"
},
output: {
alias: "o",
type: "string",
default: "suricata.rules"
},
json: {
type: "string"
},
csv: {
type: "string"
},
delimiter: {
alias: "d",
type: "string"
},
sid: {
type: "string"
}
}
}
);
if (process.stdin.isTTY) {
main();
} else {
let rl = readline.createInterface({
input: process.stdin
});
rl.on("line", function(cmd) {
stdin += cmd + "\n";
});
rl.on("close", () => {
stdin = stdin.slice(0, stdin.length - 1);
main().then(() => {
process.exit();
});
});
}