-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnvtengine.cc
547 lines (510 loc) · 17.9 KB
/
nvtengine.cc
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <dirent.h>
#include <stdio.h>
#ifdef _WIN32
#else
#include <unistd.h>
#endif // _WIN32
#include <fstream>
extern "C" {
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "thirdpart/masscan/hostscan.h"
}
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#ifdef __linux__
#include <signal.h>
#endif
#include "./modules/net/socket.hpp"
#include "./modules/openvas/api.hpp"
#include "./modules/openvas/support/sconfdb.hpp"
#include "codecache.hpp"
#include "engine/vm.hpp"
#include "jsonrpc.hpp"
#include "manger.hpp"
#include "modules/modules.h"
#include "modules/openvas/support/nvtidb.hpp"
#include "ntvpref.hpp"
#include "parseargs.hpp"
#include "scriptloader.hpp"
#include "taskmgr.hpp"
#include "testoids.hpp"
#include "vfsfileio.hpp"
#ifndef PRODUCT_NAME
#define PRODUCT_NAME "nvtstudio"
#endif
void CollectAllScript(FilePath path, FilePath relative_path, std::list<std::string>& result) {
struct dirent* entry = NULL;
DIR* dir = opendir(std::string(path).c_str());
if (dir == NULL) {
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type & DT_DIR) {
if (entry->d_name[0] != '.') {
CollectAllScript(path + entry->d_name, relative_path + entry->d_name, result);
}
continue;
}
FilePath short_path = relative_path;
short_path += entry->d_name;
std::string element = short_path;
if (element.find(".sc") != std::string::npos) {
result.push_back(element);
}
}
closedir(dir);
}
void WinPathToLinuxPath(std::string& path) {
for (size_t i = 0; i < path.size(); i++) {
if (path[i] == '\\') {
path[i] = '/';
}
}
}
void UpdateNVTIFromLocalFS(NVTPref& helper) {
std::list<std::string> result;
StdFileIO scriptIO(helper.script_folder());
StdFileIO builtinIO(helper.builtin_script_path());
DefaultScriptLoader scriptLoader(&scriptIO, false);
DefaultScriptLoader builtinLoader(&builtinIO, false);
ScriptLoaderImplement loader(&scriptLoader, &builtinLoader);
std::list<std::string> files;
CollectAllScript(helper.builtin_script_path(), "", files);
loader.AddBuiltinScriptFiles(files);
CollectAllScript(helper.script_folder(), "", result);
Value ret = Value::MakeArray();
ScriptCache* cachePtr = ScriptCacheImplement::Shared();
CodeCacheWriter cache2(FilePath(helper.app_data_folder()) + FilePath("code_cache.bin"));
if (helper.enable_code_cache()) {
cachePtr = &cache2;
}
DefaultExecutorCallback callback;
callback.mDescription = true;
Interpreter::Executor exe(&callback, &loader);
RegisgerModulesBuiltinMethod(&exe);
exe.SetScriptCacheProvider(cachePtr);
for (auto iter : result) {
if (iter.find(".inc.") != std::string::npos) {
continue;
}
#ifdef _WIN32
WinPathToLinuxPath(iter);
#endif
OVAContext context(iter, Value::MakeMap(), Value::MakeMap(), new support::ScriptStorage());
exe.SetUserContext(&context);
bool ok = exe.Execute(iter.c_str(), 5, helper.log_engine_warning());
if (ok) {
ret._array().push_back(context.Nvti);
if (ret.Length() > 5000) {
support::NVTIDataBase db(FilePath(helper.app_data_folder()) + "attributes.db");
db.UpdateAll(ret);
ret._array().clear();
}
}
if (callback.mSyntaxError) {
break;
}
}
{
support::NVTIDataBase db(FilePath(helper.app_data_folder()) + "attributes.db");
db.UpdateAll(ret);
ret._array().clear();
}
if (!helper.enable_code_cache()) {
return;
}
//compire all left inc file
for (auto iter : result) {
if (iter.find(".inc.") == std::string::npos) {
continue;
}
#ifdef _WIN32
WinPathToLinuxPath(iter);
#endif
OVAContext context(iter, Value::MakeMap(), Value::MakeMap(), new support::ScriptStorage());
exe.SetUserContext(&context);
bool ok = exe.Execute(iter.c_str(), 5, helper.log_engine_warning(), true);
if (callback.mSyntaxError) {
break;
}
}
}
void UpdateNVTIFromVFS(NVTPref& helper) {
std::list<std::string> result;
VFSFileIO scriptIO(helper.script_package());
StdFileIO builtinIO(helper.builtin_script_path());
DefaultScriptLoader scriptLoader(&scriptIO, false);
DefaultScriptLoader builtinLoader(&builtinIO, false);
ScriptLoaderImplement loader(&scriptLoader, &builtinLoader);
std::list<std::string> files;
CollectAllScript(helper.builtin_script_path(), "", files);
loader.AddBuiltinScriptFiles(files);
scriptIO.EnumFile(result);
Value ret = Value::MakeArray();
ScriptCache* cachePtr = ScriptCacheImplement::Shared();
CodeCacheWriter cache2(FilePath(helper.app_data_folder()) + FilePath("code_cache.bin"));
if (helper.enable_code_cache()) {
cachePtr = &cache2;
}
DefaultExecutorCallback callback;
callback.mDescription = true;
Interpreter::Executor exe(&callback, &loader);
RegisgerModulesBuiltinMethod(&exe);
exe.SetScriptCacheProvider(cachePtr);
for (auto iter : result) {
if (iter.find(".inc.") != std::string::npos) {
continue;
}
#ifdef _WIN32
WinPathToLinuxPath(iter);
#endif
OVAContext context(iter, Value::MakeMap(), Value::MakeMap(), new support::ScriptStorage());
exe.SetUserContext(&context);
bool ok = exe.Execute(iter.c_str(), 5, helper.log_engine_warning());
if (ok) {
ret._array().push_back(context.Nvti);
if (ret.Length() > 5000) {
support::NVTIDataBase db(FilePath(helper.app_data_folder()) + "attributes.db");
db.UpdateAll(ret);
ret._array().clear();
}
}
if (callback.mSyntaxError) {
break;
}
}
{
support::NVTIDataBase db(FilePath(helper.app_data_folder()) + "attributes.db");
db.UpdateAll(ret);
ret._array().clear();
}
if (!helper.enable_code_cache()) {
return;
}
//compire all left inc file
for (auto iter : result) {
if (iter.find(".inc.") == std::string::npos) {
continue;
}
#ifdef _WIN32
WinPathToLinuxPath(iter);
#endif
OVAContext context(iter, Value::MakeMap(), Value::MakeMap(), new support::ScriptStorage());
exe.SetUserContext(&context);
bool ok = exe.Execute(iter.c_str(), 5, helper.log_engine_warning(), true);
if (callback.mSyntaxError) {
break;
}
}
}
void UpdateNVTI(Interpreter::Value& pref) {
NVTPref helper(pref);
if (helper.script_folder().size() == 0 && helper.script_package().size() == 0) {
std::cout << "You must provider script_folder or script_package in you config file"
<< std::endl;
return;
}
if (helper.script_package().size()) {
UpdateNVTIFromVFS(helper);
} else {
UpdateNVTIFromLocalFS(helper);
}
}
void LoadOidList(ParseArgs& option, Interpreter::Value& pref, std::list<std::string>& result) {
NVTPref helper(pref);
//load from db
if (option.GetOption("--filter").size()) {
support::ScanConfig db(FilePath(helper.app_data_folder()) + "scanconfig.db");
NVT_LOG_DEBUG("load oid list from database with filter:", option.GetOption("--filter"));
return db.Get(option.GetOption("--filter"), result);
}
//load from file
{
StdFileIO IO("");
size_t size = 0;
std::string rule;
char* data = (char*)IO.Read("./rule.txt", size);
if (data) {
rule.assign(data, size);
free(data);
result = Interpreter::split(rule, ';');
NVT_LOG_DEBUG("load oid list from ./rule.txt");
return;
}
}
NVT_LOG_DEBUG("load oid list from builtin list");
result = Interpreter::split(test_oids, ';');
}
FileIO* CreateBuiltinFileIO(NVTPref& helper) {
return new StdFileIO(helper.builtin_script_path());
}
std::list<std::string> GetBuiltinFileList(NVTPref& helper) {
std::list<std::string> files;
CollectAllScript(helper.builtin_script_path(), "", files);
return files;
}
FileIO* CreateFileIO(NVTPref& helper, bool& Encoded) {
BlockFile* block = NULL;
if (helper.enable_code_cache()) {
block = BlockFile::NewReader((FilePath)helper.app_data_folder() + "code_cache.bin");
}
if (block != NULL) {
Encoded = true;
return block;
}
if (helper.script_package().size()) {
return new VFSFileIO(helper.script_package());
}
if (helper.script_folder().size()) {
return new StdFileIO(helper.script_folder());
}
return NULL;
}
void ServeDaemon(ParseArgs& options, Interpreter::Value& pref) {
NVTPref helper(pref);
bool Encode = false;
FileIO* pBuiltinFileIO = CreateBuiltinFileIO(helper);
FileIO* pFileIO = CreateFileIO(helper, Encode);
if (pBuiltinFileIO == NULL || pFileIO == NULL) {
NVT_LOG_ERROR("Init FileIO Failed");
return;
}
DefaultScriptLoader scriptLoader(pFileIO, Encode);
DefaultScriptLoader builtinLoader(pBuiltinFileIO, false);
ScriptLoaderImplement loader(&scriptLoader, &builtinLoader);
std::list<std::string> list = GetBuiltinFileList(helper);
loader.AddBuiltinScriptFiles(list);
std::string address = options.GetOption("--address");
std::string port = options.GetOption("--port");
RPCServer srv(pref, &loader);
if (address.size() == 0) {
address = "localhost";
}
ServeTCP(&srv, port, address);
delete pFileIO;
delete pBuiltinFileIO;
}
void DoNVTTest(Interpreter::Value& pref, ParseArgs& option) {
NVTPref helper(pref);
if (helper.script_folder().size() == 0 && helper.script_package().size() == 0) {
std::cout << "You must provider script_folder or script_package in you config file"
<< std::endl;
return;
}
std::string hostList = option.GetOption("--hosts");
std::string portList = option.GetOption("--ports");
if (hostList.size() == 0 || hostList.size() == 0) {
std::cout << "You must provider target hosts and ports" << std::endl;
return;
}
if (helper.enable_code_cache()) {
std::list<std::string> oidList;
LoadOidList(option, pref, oidList);
BlockFile* block =
BlockFile::NewReader((FilePath)helper.app_data_folder() + "code_cache.bin");
if (block != NULL) {
StdFileIO builtinIO(helper.builtin_script_path());
DefaultScriptLoader scriptLoader(block, true);
DefaultScriptLoader builtinLoader(&builtinIO, false);
ScriptLoaderImplement loader(&scriptLoader, &builtinLoader);
std::list<std::string> files;
CollectAllScript(helper.builtin_script_path(), "", files);
loader.AddBuiltinScriptFiles(files);
HostsTask task(hostList, portList, pref, &loader);
task.Start(oidList);
task.Join();
delete block;
return;
}
}
if (helper.script_package().size()) {
std::list<std::string> oidList;
LoadOidList(option, pref, oidList);
VFSFileIO scriptIO(helper.script_package());
StdFileIO builtinIO(helper.builtin_script_path());
std::list<std::string> files;
CollectAllScript(helper.builtin_script_path(), "", files);
DefaultScriptLoader scriptLoader(&scriptIO, false);
DefaultScriptLoader builtinLoader(&builtinIO, false);
ScriptLoaderImplement loader(&scriptLoader, &builtinLoader);
loader.AddBuiltinScriptFiles(files);
HostsTask task(hostList, portList, pref, &loader);
task.Start(oidList);
task.Join();
} else {
std::list<std::string> oidList;
LoadOidList(option, pref, oidList);
StdFileIO scriptIO(helper.script_folder());
StdFileIO builtinIO(helper.builtin_script_path());
std::list<std::string> files;
CollectAllScript(helper.builtin_script_path(), "", files);
DefaultScriptLoader scriptLoader(&scriptIO, false);
DefaultScriptLoader builtinLoader(&builtinIO, false);
ScriptLoaderImplement loader(&scriptLoader, &builtinLoader);
loader.AddBuiltinScriptFiles(files);
HostsTask task(hostList, portList, pref, &loader);
task.Start(oidList);
task.Join();
}
}
bool LoadJsonFromFile(std::string path, Interpreter::Value& pref) {
StdFileIO IO("");
size_t length = 0;
void* data = IO.Read(std::string(path), length);
if (data != NULL) {
std::string text;
text.assign((char*)data, length);
pref = ParseJSON(text, true);
free(data);
std::cout << "current config file location:" << path << std::endl;
return true;
}
return false;
}
bool LoadDefaultConfig(Interpreter::Value& pref) {
std::string search = "";
#ifdef _WIN32
search = getenv("AppData");
FilePath path = search;
path += PRODUCT_NAME;
path += "nvtengine.conf";
if (LoadJsonFromFile(path, pref)) {
return true;
}
if (LoadJsonFromFile(".\\etc\\nvtengine.conf", pref)) {
return true;
}
if (LoadJsonFromFile("..\\etc\\nvtengine.conf", pref)) {
return true;
}
return false;
#else
char _default_path[][120] = {"/usr/local/etc/", "/etc/"};
for (int i = 0; i < sizeof(_default_path) / sizeof(_default_path[0]); i++) {
FilePath path = _default_path[i];
path += PRODUCT_NAME;
path += "nvtengine.conf";
if (LoadJsonFromFile(path, pref)) {
return true;
}
}
if (LoadJsonFromFile("./nvtengine.conf", pref)) {
return true;
}
if (LoadJsonFromFile("./etc/nvtengine.conf", pref)) {
return true;
}
if (LoadJsonFromFile("../etc/nvtengine.conf", pref)) {
return true;
}
return false;
#endif
}
void ImportTextOidListToDatabase(std::string file, std::string dbFile, std::string name) {
StdFileIO IO("");
size_t size = 0;
std::string rule;
char* data = (char*)IO.Read(file, size);
if (!data) {
NVT_LOG_ERROR("read oid file failed");
return;
}
rule.assign(data, size);
free(data);
std::list<std::string> result = Interpreter::split(rule, ';');
support::ScanConfig db(dbFile);
db.BeginTransaction();
for (auto iter : result) {
db.Add(name, iter);
}
db.CommitTransaction();
}
void init() {
masscan_init();
Interpreter::InitializeLibray();
net::Socket::InitLibrary();
ssh_init();
SSL_load_error_strings();
SSL_library_init();
}
//nvtengine daemon -a localhost -p 100 -c
//nvtengine scan -h -p -c -f
//nvtengine update -c
int main(int argc, char* argv[]) {
Command daemonCmd("daemon", "run jsonrpc server", std::list<Option>());
daemonCmd.options.push_back(Option("-c", "--config", "config file path", false));
daemonCmd.options.push_back(
Option("-a", "--address", "listen address,default localhost", false));
daemonCmd.options.push_back(Option("-p", "--port", "listen port", true));
Command scanCmd("scan", "start cli to scan target", std::list<Option>());
scanCmd.options.push_back(Option("-c", "--config", "config file path", false));
scanCmd.options.push_back(Option("-h", "--hosts", "target hosts list", true));
scanCmd.options.push_back(Option("-p", "--ports", "port range", true));
scanCmd.options.push_back(Option("-f", "--filter", "filter to select scripts", false));
Command updateCmd("update", "update NVTI database", std::list<Option>());
updateCmd.options.push_back(Option("-c", "--config", "config file path", false));
Command importCmd("import", "import text oid list to database", std::list<Option>());
importCmd.options.push_back(Option("-c", "--config", "config file path", false));
importCmd.options.push_back(Option("-n", "--name", "filter name", true));
importCmd.options.push_back(Option("-t", "--textfile", "oid list file,split with';", true));
Command version("version", "get version of nvtengine", std::list<Option>());
std::list<Command> allCommands;
allCommands.push_back(daemonCmd);
allCommands.push_back(scanCmd);
allCommands.push_back(updateCmd);
allCommands.push_back(importCmd);
allCommands.push_back(version);
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif
init();
ParseArgs options(argc, argv, allCommands);
if (!options.IsValid()) {
options.PrintHelp("nvtengine");
return -1;
}
if (options.GetCommand() == version.strCmd) {
std::cout << VERSION_STR << std::endl;
return -1;
}
std::string configFile = options.GetOption("--config");
Interpreter::Value pref;
if (configFile.size() == 0) {
if (!LoadDefaultConfig(pref)) {
options.PrintHelp("nvtengine");
NVT_LOG_ERROR("can't load default config file");
return -1;
}
} else {
if (!LoadJsonFromFile(configFile, pref)) {
NVT_LOG_ERROR("can't load config file");
return -1;
}
}
g_LogLevel = NVTPref(pref).log_level();
NVT_LOG_DEBUG("Preferences ", pref.ToString());
if (options.GetCommand() == daemonCmd.strCmd) {
ServeDaemon(options, pref);
return 0;
}
if (options.GetCommand() == scanCmd.strCmd) {
DoNVTTest(pref, options);
pref = Value();
std::cout << Interpreter::Status::ToString() << std::endl;
return 0;
}
if (options.GetCommand() == updateCmd.strCmd) {
UpdateNVTI(pref);
return 0;
}
if (options.GetCommand() == importCmd.strCmd) {
FilePath dbFile = FilePath(NVTPref(pref).app_data_folder()) + "scanconfig.db";
ImportTextOidListToDatabase(options.GetOption("--textfile"), dbFile,
options.GetOption("--name"));
return 0;
}
options.PrintHelp("nvtengine");
return -1;
}