-
Notifications
You must be signed in to change notification settings - Fork 13
/
ajax.php
2121 lines (1924 loc) · 84.1 KB
/
ajax.php
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File: ajax.php
* Author: Ryan Prather
* Purpose: For AJAX queries from the UI
* Created: Mar 9, 2015
*
* Portions Copyright 2016-2018: Cyber Perspectives, LLC, All rights reserved
* Released under the Apache v2.0 License
*
* Portions Copyright (c) 2012-2015, Salient Federal Solutions
* Portions Copyright (c) 2008-2011, Science Applications International Corporation (SAIC)
* Released under Modified BSD License
*
* See license.txt for details
*
* Change Log:
* - Mar 9, 2015 - File created
* - Sep 1, 2016 - Updated Copyright and mass conversion of old PERL CGI script to PHP
* - Oct 10, 2016 - Converted target_filter function to output JSON instead of XML
* - Oct 24, 2016 - Converted update_script_status and get_hosts function to output JSON instead of XML
* Added add_scan action to start /exec/background_results.php script in background
* - Nov 7, 2016 - Moved a couple lines around for readability
* - Dec 7, 2016 - Fixed bug in update_script_status function that retrieved all scans even if a filter was turned on
* - Jan 30, 2017 - Formatting, added auto-categorization, and category deletion
* - Fed 15, 2017 - Formatting
* - Mar 4, 2017 - Moved from /cgi-bin to /
* - Mar 20, 2017 - Added functionality to delete result file after uploading.
* - Apr 5, 2017 - Fixed error in get_category_details
* - Apr 7, 2017 - Fixed errors in get_hosts with scan missing icons
* - May 13, 2017 - Added export-ckl.php threading from category header or target details page
* Generate message when target searching yields no results
* Added functionality for user to add or remove software from checklist and edit checklist specifics
* - May 19, 2017 - Added delete-host functionality for STE Ops page
* - May 20, 2017 - Added source icon to more quickly identify sources and change start time format
* - May 22, 2017 - Fixed update_script_status method to account for DataTables library
* - May 25, 2017 - Remove search method in favor of creating /search.php file to perform all search functions
* - Sep 28, 2017 - Fixed export-ckl to work on Windows system, removing single quotes from commandline strings - jao
* - Dec 27, 2017 - Merged database schemas into a single schema, also syntax updates
* - Jan 10, 2018 - Formatting and added calls for /ste/stats.php
* - Jan 15, 2018 - Updated to get formatted target notes
* - Jan 16, 2018 - Added ajax to auto update the cpe, cve, stig, and nasl loading progress.
Moved scan deletion here
* - May 2, 2018 - Added save_checklist else block to support save functionality on Catalog Mgmt page
* - May 10, 2018 - Added more fields when getting system updates
* - May 31, 2018 - Added data point for "isScanError" when getting target scan data
* - Jun 2, 2018 - Added nvd_year for status update AJAX
*/
set_time_limit(0);
include_once 'vendor/autoload.php';
include_once 'config.inc';
include_once 'import.inc';
include_once 'helper.inc';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
chdir(dirname(__FILE__));
$db = new db();
$conn = new mysqli(DB_SERVER, "web", db::decrypt_pwd(), 'sagacity');
$action = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_STRING);
$ste = filter_input(INPUT_COOKIE, 'ste', FILTER_VALIDATE_INT);
if (!$ste) {
$ste = filter_input(INPUT_POST, 'ste', FILTER_VALIDATE_INT);
}
if (!$action) {
die();
}
if ($action == 'update_notes') {
print update_tgt_notes();
}
elseif ($action == 'search') {
print search();
}
elseif ($action == 'chk_filter') {
print header(JSON) . chk_filter();
}
elseif ($action == 'sw_filter') {
print sw_filter();
}
elseif ($action == 'os_filter') {
print sw_filter(true);
}
elseif ($action == 'update_proc_status') {
print update_proc_status();
}
elseif ($action == 'update_proc_notes') {
print update_proc_notes();
}
elseif ($action == 'update_script_status') {
print update_script_status();
}
elseif ($action == 'update_finding_status') {
print "<root>" . update_finding_status() . "</root>";
}
elseif ($action == 'update_finding_ia_controls') {
print "<root>" . update_finding_ia_controls() . "</root>";
}
elseif ($action == 'update_finding_notes') {
print "<root>" . update_finding_notes() . "</root>";
}
elseif ($action == 'update_risk_status') {
print update_risk_status();
}
elseif ($action == 'update_risk_analysis') {
print update_risk_analysis();
}
elseif ($action == 'update_control_completion') {
print update_control_completion();
}
elseif ($action == 'update_stig_control') {
print update_stig_control();
}
elseif ($action == 'refresh_counts') {
print "<root>" . refresh_counts() . "</root>";
}
elseif ($action == 'get_control_details') {
if ($_REQUEST['id'] == 'overall') {
print get_STE_details();
}
else {
print get_control_details();
}
}
elseif ($action == 'update_STE') {
print update_STE_details();
}
elseif ($action == 'update_STE_risk') {
$conn->real_query(
"UPDATE `sagacity`.`ste` SET `risk_status`='" .
strtolower($conn->real_escape_string($_REQUEST['status'])) .
"' WHERE `id`=" . $conn->real_escape_string($ste));
}
elseif ($action == 'get_hosts') {
$cat_id = filter_input(INPUT_POST, 'cat_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if (is_numeric($cat_id)) {
print get_hosts($cat_id);
}
else {
print json_encode(['error' => 'Invalid category ID']);
}
}
elseif ($action == 'new-get-hosts') {
$cat_id = filter_input(INPUT_POST, 'cat-id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if (is_numeric($cat_id)) {
print new_get_hosts($cat_id);
}
else {
print json_encode(['error' => 'Invalid category ID']);
}
}
elseif ($action == 'get_target_data') {
print get_target_data($_REQUEST['type']);
}
elseif ($action == 'save_filter') {
print $db->save_Filter($_REQUEST['type'], $_REQUEST['name'], $_REQUEST['criteria']);
}
elseif ($action == 'target-filter') {
print header(JSON) . target_filter($ste, $_REQUEST['criteria']);
}
elseif ($action == 'scan-filter') {
print scan_filter($ste, $_REQUEST['criteria']);
}
elseif ($action == 'finding-filter') {
print finding_filter($ste, $_REQUEST['criteria']);
}
elseif ($action == 'reference-filter') {
print reference_filter($ste, $_REQUEST['criteria']);
}
elseif ($action == 'get-saved-filter') {
print get_saved_filter($_REQUEST['type'], $_REQUEST['name']);
}
elseif ($action == 'update-target-field') {
print update_target_field($_REQUEST['field'], $_REQUEST['data']);
}
elseif ($action == 'get_category_details') {
$cat_id = filter_input(INPUT_POST, 'cat_id', FILTER_VALIDATE_INT);
print header(JSON) . get_category_details($cat_id);
}
elseif ($action == 'add_scans') {
$import = new import();
$import->scan_Result_Files(false);
print header(JSON) . json_encode(array(
'success' => 'Thread running'
));
}
elseif ($action == 'auto-categorize') {
$db->auto_Catorgize_Targets($ste);
print header(JSON) . json_encode([
'success' => 'Categorized Targets'
]);
}
elseif ($action == 'delete-cat') {
$cat_id = filter_input(INPUT_POST, 'cat_id', FILTER_VALIDATE_INT);
if ($db->delete_Cat($cat_id)) {
print header(JSON) . json_encode([
'success' => 'Successfully deleted category'
]);
}
}
elseif ($action == 'delete-file') {
$file = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);
$file = realpath($file);
if ($file && preg_match("/^" . preg_quote(TMP, '/') . "/", $file)) {
if (unlink($file)) {
print header(JSON) . json_encode([
'success' => 'Deleted file'
]);
}
else {
print header(JSON) . json_encode([
'error' => "Failed to delete $file"
]);
}
}
else {
$file = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);
print header(JSON) . json_encode([
'error' => "$file does not exist"
]);
}
}
elseif ($action == 'get-cat-data') {
$fname = filter_input(INPUT_POST, 'fname', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
$checklist = $db->get_Checklist_By_File($fname);
if (isset($checklist[0])) {
$chk = $checklist[0];
$chk->type = ucfirst($chk->type);
print header(JSON) . json_encode($chk);
}
else {
print header(JSON) . json_encode(array('error' => 'Error finding checklist'));
}
}
elseif ($action == 'checklist-remove-software') {
$chk_id = filter_input(INPUT_POST, 'chk_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sw_id = filter_input(INPUT_POST, 'sw_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$db->help->delete("sagacity.checklist_software_lookup", null, array(
array(
'field' => 'chk_id',
'op' => '=',
'value' => $chk_id
),
array(
'field' => 'sw_id',
'op' => '=',
'value' => $sw_id,
'sql_op' => 'AND'
)
));
if ($db->help->execute()) {
print header(JSON) . json_encode(array('success' => 'Relationship deleted'));
}
else {
print header(JSON) . json_encode(array('error' => 'Failed to delete relationship'));
}
}
elseif ($action == 'checklist-add-software') {
$sw_id = filter_input(INPUT_POST, 'sw_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$chk_id = filter_input(INPUT_POST, 'chk_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$db->help->insert("checklist_software_lookup", array(
'sw_id' => $sw_id,
'chk_id' => $chk_id
), true);
if (!$db->help->execute()) {
print header(JSON) . json_encode(array('status' => 'Error adding the software to the checklist'));
}
else {
print header(JSON) . json_encode(array('status' => 'Successfully added the software'));
}
}
elseif ($action == 'save-checklist') {
$rel_date = new DateTime(filter_input(INPUT_POST, 'rel-date', FILTER_SANITIZE_STRING));
$db->help->update("sagacity.checklist", [
'name' => filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING),
'description' => filter_input(INPUT_POST, 'desc', FILTER_SANITIZE_STRING),
'icon' => filter_input(INPUT_POST, 'icon', FILTER_SANITIZE_STRING),
'date' => (is_a($rel_date, 'DateTime') ? $rel_date->format(MYSQL_D_FORMAT) : (new DateTime())->format(MYSQL_D_FORMAT))
], [
[
'field' => 'id',
'op' => '=',
'value' => filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT)
]
]);
if($db->help->execute()) {
print json_encode(['success' => 'Successfully updated checklist']);
}
else {
print json_encode(['error' => 'Error updating checklist']);
}
}
elseif ($action == 'export-ckl') {
$cat_id = filter_input(INPUT_POST, 'cat', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$tgt_id = filter_input(INPUT_POST, 'tgt', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$script = null;
if (!is_numeric($ste)) {
die;
}
if ($cat_id && is_numeric($cat_id)) {
$script = (defined('PHP_BIN') ? realpath(PHP_BIN) : realpath(PHP)) .
" -c " . realpath(PHP_CONF) .
" -f " . realpath(DOC_ROOT . "/exec/export-ckl.php") . " --" .
" -s=$ste" .
" -c=$cat_id";
}
elseif ($tgt_id && is_numeric($tgt_id)) {
$script = (defined('PHP_BIN') ? realpath(PHP_BIN) : realpath(PHP)) .
" -c " . realpath(PHP_CONF) .
" -f " . realpath(DOC_ROOT . "/exec/export-ckl.php") . " --" .
" -s=$ste" .
" -t=$tgt_id";
}
if (!is_null($script)) {
if (strtolower(substr(PHP_OS, 0, 3)) == "win") {
$shell = new COM("WScript.Shell");
$shell->CurrentDirectory = DOC_ROOT . "/exec";
$shell->run($script, 0, false);
}
elseif (strtolower(substr(PHP_OS, 0, 3)) == 'lin') {
exec("cd " . realpath(DOC_ROOT . "/exec") . " && {$script} > /dev/null &");
}
}
}
elseif ($action == 'delete-host') {
$sel_tgts = json_decode(html_entity_decode(filter_input(INPUT_POST, 'selected_tgts', FILTER_SANITIZE_STRING)));
if (is_array($sel_tgts) && count($sel_tgts)) {
foreach ($sel_tgts as $tgt_id) {
if (!$db->delete_Target($tgt_id)) {
print header(JSON) . json_encode(array('error' => "Failed to delete target ID $tgt_id"));
break;
}
}
}
elseif (is_numeric($sel_tgts)) {
if (!$db->delete_Target($sel_tgts)) {
print header(JSON) . json_encode(array('error' => "Failed to delete target ID $sel_tgts"));
}
}
print header(JSON) . json_encode(['success' => "Deleted all selected target(s)"]);
}
elseif ($action == 'get-target-notes') {
$tgt_id = filter_input(INPUT_POST, 'tgt-id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if (is_numeric($tgt_id) && $tgt_id > 0) {
$db->help->select("target", ['notes'], [
[
'field' => 'id',
'op' => '=',
'value' => $tgt_id
]
]);
$row = $db->help->execute();
if (is_array($row) && count($row) && isset($row['notes'])) {
print header(JSON) . json_encode(['notes' => $row['notes']]);
}
}
}
elseif ($action == 'save-target-notes') {
$tgt_id = filter_input(INPUT_POST, 'tgt-id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$notes = filter_input(INPUT_POST, 'notes', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
if (is_numeric($tgt_id) && $tgt_id > 0) {
$db->help->update("target", [
'notes' => htmlentities($notes)
], [
[
'field' => 'id',
'op' => '=',
'value' => $tgt_id
]
]);
if ($db->help->execute()) {
print header(JSON) . json_encode(['success' => 'Updated target notes']);
}
else {
print header(JSON) . json_encode(['error' => $db->help->c->error]);
}
}
}
elseif ($action == 'get-load-status') {
$set = $db->get_Settings([
'cpe-count', 'cpe-dl-progress', 'cpe-progress',
'cve-count', 'cve-dl-progress', 'cve-progress',
'nvd-cve-count', 'nvd-cve-dl-progress', 'nvd-cve-progress', 'nvd-year',
'stig-count', 'stig-dl-progress', 'stig-progress',
'nasl-count', 'nasl-dl-progress', 'nasl-progress'
]);
print json_encode($set);
}
elseif ($action == 'delete-scan') {
$scan_id = filter_input(INPUT_POST, 'scan-id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$del_tgts = (bool) filter_input(INPUT_POST, 'delete-targets', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($scan_id) {
if ($db->delete_Scan($ste, $scan_id, $del_tgts)) {
print json_encode(['success' => 'Deleted Scan']);
}
else {
print json_encode(['error' => 'Error deleting scan']);
}
}
}
function update_tgt_notes()
{
global $db;
$notes = str_replace(" ", "", filter_input(INPUT_POST, 'notes', FILTER_SANITIZE_STRING));
$tgt = filter_input(INPUT_POST, 'tgt', FILTER_VALIDATE_INT);
$db->help->update("sagacity.target", array(
'notes' => $notes
), array(
array(
'field' => 'id',
'op' => '=',
'value' => $tgt
)
));
if (!$db->help->execute()) {
return "failure";
}
else {
return "success";
}
}
function chk_filter()
{
global $db;
$tgt_id = filter_input(INPUT_POST, 'tgt_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$filter = filter_input(INPUT_POST, 'filter', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
$hide_old = (boolean) filter_input(INPUT_POST, 'hide_old', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
$ret = array();
$db->help->create_table("c", true, $db->help->select("sagacity.checklist", null, array(), array(
'order' => '`ver` DESC, CONVERT(`release`, DECIMAL(4,2)) DESC'
)));
if (!$db->help->execute()) {
return $ret;
}
$where = array();
$flags = array();
if (!empty($filter)) {
$where = array(
array(
'field' => 'c.name',
'op' => LIKE,
'value' => "'%{$filter}%'"
)
);
}
if (!empty($tgt_id)) {
$where[] = array(
'field' => 'tc.chk_id',
'op' => IS,
'value' => null,
'sql_op' => 'AND'
);
$flags['table_joins'] = array(
"LEFT JOIN sagacity.target_checklist tc ON tc.chk_id = c.id AND tc.tgt_id = $tgt_id"
);
$flags['order'] = 'c.name';
}
if ($hide_old) {
$flags['group'] = 'c.name, c.type, c.id';
}
$db->help->select("c", array('c.id'), $where, $flags);
$rows = $db->help->execute();
if (is_array($rows) && count($rows) && isset($rows['id'])) {
$rows = array(0 => $rows);
}
if (is_array($rows) && count($rows) && isset($rows[0])) {
foreach ($rows as $row) {
$chk = $db->get_Checklist($row['id']);
if (is_array($chk) && count($chk) && isset($chk[0]) && is_a($chk[0], 'checklist')) {
$ret[] = $chk[0];
}
}
}
return json_encode($ret);
}
function sw_filter($is_os = false)
{
global $db;
$ret = [];
$filter = "'%" . filter_input(INPUT_POST, 'filter', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE) . "%'";
$tgt_id = filter_input(INPUT_POST, 'tgt_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$db->help->select("sagacity.software s", ['s.id', 's.cpe', 's.sw_string'], [
[
'field' => 's.cpe',
'op' => LIKE,
'value' => $filter,
'open-paren' => true
],
[
'field' => 's.sw_string',
'op' => LIKE,
'value' => $filter,
'sql_op' => 'OR'
],
[
'field' => 's.short_sw_string',
'op' => LIKE,
'value' => $filter,
'sql_op' => 'OR',
'close-paren' => true
],
[
'field' => 'ts.sft_id',
'op' => IS,
'value' => null,
'sql_op' => 'AND'
],
[
'field' => 's.cpe',
'op' => LIKE,
'value' => ($is_os ? "'%/o%'" : "'%/a%'"),
'sql_op' => 'AND'
]
], [
'table_joins' => [
"LEFT JOIN `sagacity`.`target_software` ts ON ts.`sft_id` = s.`id`" . ($tgt_id ? " AND ts.`tgt_id` = $tgt_id" : "")
],
'order' => 's.cpe'
]);
$sw = $db->help->execute();
if (is_array($sw) && count($sw) && isset($sw['id'])) {
$sw = [0 => $sw];
}
if (is_array($sw) && count($sw) && isset($sw[0])) {
foreach ($sw as $row) {
$ret[] = [
'sw_id' => $row['id'],
'cpe' => $row['cpe'],
'sw_string' => $row['sw_string']
];
}
}
return header(JSON) . json_encode($ret);
}
function update_proc_status()
{
global $conn, $ste;
$control_id = str_replace("_", "-", substr(param('control'), 0, -7));
if (preg_match("/[A-Z]{4}\-\d\-\d/", $control_id)) {
$proc_id = $control_id;
$sql = "SELECT `ctrl_id` " .
"FROM `sagacity`.`proc_findings` " .
"WHERE " .
"`ste_id`=" . $conn->real_escape_string($ste) . " AND " .
"`proc_id`='" . $conn->real_escape_string($control_id) . "'";
if ($res = $conn->query($sql)) {
if ($res->num_rows) {
$row = $res->fetch_array(MYSQLI_ASSOC);
$sql = "UPDATE `sagacity`.`proc_findings` " .
"SET `status`='" . $conn->real_escape_string($_REQUEST['status']) . " " .
"WHERE `ste_id`=" . $conn->real_escape_string($ste) . " AND " .
"`proc_id`='" . $conn->real_escape_string($row['ctrl_id']) . "'";
}
else {
$sql = "INSERT INTO `sagacity`.`proc_findings` (`ste_id`,`ctrl_id`,`proc_id`,`status`) VALUES (" .
$_REQUEST['ste'] . "," .
"'" . $conn->real_escape_string(substr($proc_id, 0, 6)) . "'" .
"'" . $conn->real_escape_string($proc_id) . "'" .
"'" . $conn->real_escape_string($_REQUEST['status']) . "')";
}
if (!$conn->real_query($sql)) {
error_log($conn->error);
Sagacity_Error::sql_handler($sql);
}
}
}
else {
$sql = "SELECT `sub_control_id` FROM `sagacity`.`proc_ia_sub_controls` WHERE `parent_control_id`=?";
$sub_ctrls = db_helper::selectrow_array($conn, db_helper::mysql_escape_string($conn, $sql, $control_id));
foreach ($sub_ctrl as $proc_id) {
$sql = "SELECT COUNT(1) FROM `sagacity`.`proc_findings` WHERE `ste_id`=? AND `proc_id`=?";
$sql = db_helper::mysql_escape_string($conn, $sql, $_REQUEST['ste'], $proc_id);
list($cnt) = db_helper::selectrow_array($conn, $sql);
if ($cnt) {
db_helper::run($conn, "UPDATE `sagacity`.`proc_findings` SET `status`=? WHERE `ste_id`=? AND `proc_id`=?", $_REQUEST['status'], $_REQUEST['ste'], $proc_id);
}
else {
db_helper::run($conn, "INSERT INTO `sagacity`.`proc_findings` (`ste_id`,`ctrl_id`,`proc_id`,`status`) VALUES (?,?,?,?)", $_REQUEST['ste'], $control_id, $proc_id, $_REQUEST['status']);
}
}
}
}
function update_proc_notes()
{
$control_id = $field = $_REQUEST['control'];
$match = array();
if (preg_match("/([A-Z]{4}\_\d\_\d)/", $control_id, $match)) {
$control_id = str_replace("_", "-", $match[1]);
$sql = "SELECT COUNT(1) FROM `sagacity`.`proc_findings` WHERE `ste_id`=? AND `proc_id`=?";
switch ($field) {
case (preg_match("/_test_result/", $field) ? true : false):
$field = "`test_results`";
break;
case (preg_match("/_mit/", $field) ? true : false):
$field = "`mitigations`";
break;
case (preg_match("/_milestone/", $field) ? true : false):
$field = "`milestones`";
break;
case (preg_match("/_ref/", $field) ? true : false):
$field = "`ref`";
break;
case (preg_match("/_notes/", $field) ? true : false):
$field = "`notes`";
break;
default:
$field = "";
}
list($cnt) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste'], $control_id);
if ($cnt) {
$sql = "UPDATE `sagacity`.`proc_findings` SET=? WHERE `ste_id`=? AND `proc_id`=?";
db_helper::run($conn, $sql, $_REQUEST['notes'], $_REQUEST['ste'], $control_id);
}
else {
$sql = "INSERT INTO `sagacity`.`proc_findings` (`ste_id`,`ctrl_id`,`proc_id`,`status`,$field) VALUES (?,?,?,?,?)";
db_helper::run($conn, $sql, $_REQUEST['ste'], substr($control_id, 0, 6), $control_id, "Not Reviewed", $_REQUEST['notes']);
}
}
elseif (preg_match("/([A-Z]{4}\_\d)/", $control_id, $match)) {
$control_id = str_replace("_", "-", $match[1]);
$sql = "SELECT COUNT(1) FROM `sagacity`.`control_findings` WHERE `ste_id`=? AND `control_id`=?";
list($cnt) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste'], $control_id);
switch ($field) {
case (preg_match("/_vul_desc/", $field) ? true : false):
$field = "`vul_desc`";
break;
case (preg_match("/_mit/", $field) ? true : false):
$field = "`mitigations`";
break;
case (preg_match("/_ref/", $field) ? true : false):
$field = "`ref`";
break;
case (preg_match("/_notes/", $field) ? true : false):
$field = "`notes`";
break;
default:
$field = "";
}
if ($cnt) {
$sql = "UPDATE `sagacity`.`control_findings` SET $field=? WHERE `ste_id`=? AND `control_id`=?";
db_helper::run($conn, $sql, $_REQUEST['notes'], $_REQUEST['ste'], $control_id);
}
else {
$sql = "INSERT INTO `sagacity`.`control_findings` (`control_id`,`ste_id`,$field,`risk_status`) " .
"VALUES (?,?,?,(SELECT LOWER(`impact`) FROM `sagacity`.`proc_ia_controls` WHERE `control_id`=?))";
db_helper::run($conn, $sql, $control_id, $_REQUEST['ste'], $_REQUEST['notes'], $control_id);
}
}
return true;
}
function refresh_counts()
{
$ret = '';
$sql = "SELECT `id`,`name` FROM `ste_cat` WHERE `ste_id`=?";
$cats = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id']);
foreach ($cats as $key => $cat) {
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `sagacity`.`target` t " .
"LEFT JOIN `target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .
"fs.`status`='Open' AND " .
"f.`cat`=?) + " .
"(SELECT COUNT(1) AS 'total' " .
"FROM `checklist` c " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=c.`id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` " .
"LEFT JOIN `findings_status` fs ON f.`findings_status_id`=fs.`id` " .
"JOIN `target` t ON t.`id`=f.`tgt_id` " .
"WHERE t.`cat_id`=? AND " .
"c.`name`='Orphan' AND " .
"fs.`status`='Open' AND " .
"f.`cat`=?) AS 'sum_total'";
list($open_cat_1) = db_helper::selectrow_array($conn, $sql2, $row['id'], '1', $row['id'], '1');
list($open_cat_2) = db_helper::selectrow_array($conn, $sql2, $row['id'], '2', $row['id'], '2');
list($open_cat_3) = db_helper::selectrow_array($conn, $sql2, $row['id'], '3', $row['id'], '3');
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `target` t " .
"LEFT JOIN `target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .
"(fs.`status`='Not Reviewed' OR fs.`status` IS NULL)) + " .
"(SELECT COUNT(1) AS 'total' " .
"FROM `checklist` c " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=c.`id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` " .
"LEFT JOIN `findings_status` fs ON f.`findings_status_id`=fs.`id` " .
"JOIN `target` t ON t.`id`=f.`tgt_id` " .
"WHERE t.`cat_id`=? AND " .
"c.`name`='Orphan' AND " .
"(fs.`status`='Not Reviewed' OR fs.`status` IS NULL)) AS 'sum_total'";
list($not_reviewed) = db_helper::selectrow_array($conn, $sql2, $row['id'], $row['id']);
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `target` t " .
"LEFT JOIN `target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .
"fs.`status`='Exception') + " .
"(SELECT COUNT(1) AS 'total' " .
"FROM `checklist` c " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=c.`id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` " .
"LEFT JOIN `findings_status` fs ON f.`findings_status_id`=fs.`id` " .
"JOIN `target` t ON t.`id`=f.`tgt_id` " .
"WHERE t.`cat_id`=? AND " .
"c.`name`='Orphan' AND " .
"fs.`status`='Exception') AS 'sum_total'";
list($exception) = db_helper::selectrow_array($conn, $sql2, $row['id'], $row['id']);
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `target` t " .
"LEFT JOIN `target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .
"fs.`status`='False Positive') + " .
"(SELECT COUNT(1) AS 'total' " .
"FROM `checklist` c " .
"LEFT JOIN `pdi_checklist_lookup` pcl ON pcl.`checklist_id`=c.`id` " .
"LEFT JOIN `findings` f ON f.`pdi_id`=pcl.`pdi_id` " .
"LEFT JOIN `findings_status` fs ON f.`findings_status_id`=fs.`id` " .
"JOIN `target` t ON t.`id`=f.`tgt_id` " .
"WHERE t.`cat_id`=? AND " .
"c.`name`='Orphan' AND " .
"fs.`status`='False Positive') AS 'sum_total'";
list($false_positive) = db_helper::selectrow_array($conn, $sql2, $row['id'], $row['id']);
$row['name'] = str_replace(array(".", "-", " "), "", $row['name']);
$ret .= "<cat name='" . $row['name'] . "' oc1='$open_cat_1' oc2='$open_cat_2' oc3='$open_cat_3' nr='$not_reviewed' ex='$exception' fp='$false_positive' />";
}
return $ret;
}
function update_finding_status()
{
global $conn;
$sql = "UPDATE `findings` SET " .
"`findings_status_id`=? " .
"WHERE " .
"`tgt_id`=? AND `pdi_id`=?";
db_helper::run($conn, $sql, $_REQUEST['status'], $_REQUEST['host_id'], $_REQUEST['pdi_id']);
return true;
}
function update_finding_ia_controls()
{
$controls = explode(" ", $_REQUEST['ia_controls']);
$host_ids = explode(",", $_REQUEST['host_id']);
return true;
}
function update_finding_notes()
{
global $conn;
$host_ids = explode(",", $_REQUEST['host_id']);
$sql = "UPDATE `sagacity`.`findings` SET " .
"`notes`=? " .
"WHERE " .
"`tgt_id` IN (" . implode(",", $host_ids) . ") AND `pdi_id`=?";
db_helper::run($conn, $sql, $_REQUEST['notes'], $_REQUEST['pdi_id']);
return true;
}
/**
* Function to update the result script parsing status
*
* @global db $db
* @global int $ste
*
* @return array
*/
function update_script_status()
{
global $db, $ste;
$ret = [];
$type = filter_input(INPUT_POST, 'type', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
$status = filter_input(INPUT_POST, 'status', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
if (!empty($type) && !empty($status)) {
$scans = $db->get_ScanData($ste, null, $status, $type);
}
elseif (!empty($type)) {
$scans = $db->get_ScanData($ste, null, null, $type);
}
elseif (!empty($status)) {
$scans = $db->get_ScanData($ste, null, $status);
}
else {
$scans = $db->get_ScanData($ste);
}
foreach ($scans as $scan) {
$file_name = str_replace(["(", ")"], "", str_replace(" ", "_", $scan->get_File_Name()));
$diff = $scan->get_Last_Update()->diff($scan->get_Start_Time());
$ret[] = [
"scan_id" => $scan->get_ID(),
"file_name" => $scan->get_File_Name(),
"id" => $file_name,
"file_date" => $scan->get_File_DateTime()->format("Y-m-d"),
"pid" => $scan->get_PID(),
"source" => $scan->get_Source()->get_Name(),
'source_img' => $scan->get_Source()->get_Icon(),
"status" => $scan->get_Status(),
"perc_comp" => $scan->get_Percentage_Complete(),
"last_host" => $scan->get_Last_Host(),
"start_time" => $scan->get_Start_Time()->format("Y-m-d H:i:s"),
"update" => $scan->get_Last_Update()->format("Y-m-d H:i:s"),
"host_count" => $scan->get_Total_Host_Count(),
"error" => $scan->isScanError(),
"run_time" => $diff->format("%H:%I:%S")
];
}
return json_encode(['success' => 1, 'results' => $ret]);
}
/**
*
* @global mysqli $conn
* @global db $db
*/
function get_STE_details()
{
global $conn, $db;
$ret = '';
$open_high = $open_med = $open_low = $proc_na = $proc_c = $proc_total = $open_cat_1 = $open_cat_2 = $open_cat_3 = $tech_na = $tech_nf = $tech_total = 0;
list($tech_total) = db_helper::selectrow_array($conn, "SELECT COUNT(1) FROM `sagacity`.`findings` f JOIN `sagacity`.`target` t ON t.`id`=f.`tgt_id` WHERE t.`ste_id`=?", $_REQUEST['ste_id']);
list($proc_total) = db_helper::selectrow_array($conn, "SELECT COUNT(1) FROM `sagacity`.`proc_findings` WHERE `ste_id`=?", $_REQUEST['ste_id']);
$sql = "SELECT COUNT(1) " .
"FROM `sagacity`.`proc_findings` pf " .
"JOIN `sagacity`.`control_findings` cf ON pf.`ctrl_id`=cf.`control_id` " .
"WHERE pf.`ste_id`=? " .
"AND pf.`status`=? " .
"AND cf.`risk_status`=? "
;
list($open_high) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id'], 'Non-Compliant', 'high');
list($open_med) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id'], 'Non-Compliant', 'medium');
list($open_low) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id'], 'Non-Compliant', 'low');
$sql = "SELECT COUNT(1) " .
"FROM `sagacity`.`proc_findings` pf " .
"JOIN `sagacity`.`control_findings` cf ON pf.`ctrl_id`=cf.`control_id` " .
"WHERE pf.`ste_id`=? " .
"AND pf.`status`=? "
;
list($proc_na) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id'], 'Not Applicable');
list($proc_c) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id'], 'Compliant');
$sql = "SELECT `ste`.`deviations`,`ste`.`recommendations`,`ste`.`residual_risk`," .
"`ste`.`conclusion`,`ste`.`risk_status`,sys.`mitigations`,sys.`executive_summary` " .
"FROM `sagacity`.`ste`,`sagacity`.`system` sys " .
"WHERE `ste`.`system_id`=sys.`id` AND " .
"`ste`.`id`=?";
list($dev, $rec, $res, $con, $status, $mit, $exec) = db_helper::selectrow_array($conn, $sql, $_REQUEST['ste_id']);
$sql = "SELECT `id`,`name` FROM `sagacity`.`ste_cat` WHERE `ste_id`=?";
$cats = $db->get_STE_Cat_List($_REQUEST['ste_id']);
foreach ($cats as $cat) {
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `sagacity`.`target` t " .
"LEFT JOIN `sagacity`.`target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `sagacity`.`pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `sagacity`.`findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `sagacity`.`findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .
"(fs.`status`='Open' OR fs.`status`='Exception') AND " .
"f.`cat`=?) + " .
"(SELECT COUNT(1) AS 'total' " .
"FROM `sagacity`.`checklist` c " .
"LEFT JOIN `sagacity`.`pdi_checklist_lookup` pcl ON pcl.`checklist_id`=c.`id` " .
"LEFT JOIN `sagacity`.`findings` f ON f.`pdi_id`=pcl.`pdi_id` " .
"LEFT JOIN `sagacity`.`findings_status` fs ON f.`findings_status_id`=fs.`id` " .
"JOIN `sagacity`.`target` t ON t.`id`=f.`tgt_id` " .
"WHERE t.`cat_id`=? AND " .
"c.`name`='Orphan' AND " .
"(fs.`status`='Open' OR fs.`status`='Exception') AND " .
"f.`cat`=?) AS 'sum_total'";
list($tmp) = db_helper::selectrow_array($conn, $sql2, $cat->get_ID(), '1', $cat->get_ID(), '1');
$open_cat_1 += $tmp;
list($tmp) = db_helper::selectrow_array($conn, $sql2, $cat->get_ID(), '2', $cat->get_ID(), '2');
$open_cat_2 += $tmp;
list($tmp) = db_helper::selectrow_array($conn, $sql2, $cat->get_ID(), '3', $cat->get_ID(), '3');
$open_cat_3 += $tmp;
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `sagacity`.`target` t " .
"LEFT JOIN `sagacity`.`target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `sagacity`.`pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `sagacity`.`findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `sagacity`.`findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .
"fs.`status`='Not Applicable') + " .
"(SELECT COUNT(1) AS 'total' " .
"FROM `sagacity`.`checklist` c " .
"LEFT JOIN `sagacity`.`pdi_checklist_lookup` pcl ON pcl.`checklist_id`=c.`id` " .
"LEFT JOIN `sagacity`.`findings` f ON f.`pdi_id`=pcl.`pdi_id` " .
"LEFT JOIN `sagacity`.`findings_status` fs ON f.`findings_status_id`=fs.`id` " .
"JOIN `sagacity`.`target` t ON t.`id`=f.`tgt_id` " .
"WHERE t.`cat_id`=? AND " .
"c.`name`='Orphan' AND " .
"fs.`status`='Not Applicable') AS 'sum_total'";
list($tmp) = db_helper::selectrow_array($conn, $sql2, $cat->get_ID(), $cat->get_ID());
$tech_na += $tmp;
$sql2 = "SELECT (SELECT COUNT(1) " .
"FROM `sagacity`.`target` t " .
"LEFT JOIN `sagacity`.`target_checklist` tc ON t.`id`=tc.`tgt_id` " .
"LEFT JOIN `sagacity`.`pdi_checklist_lookup` pcl ON pcl.`checklist_id`=tc.`chk_id` " .
"LEFT JOIN `sagacity`.`findings` f ON f.`pdi_id`=pcl.`pdi_id` AND t.`id` = f.`tgt_id` " .
"LEFT JOIN `sagacity`.`findings_status` fs ON fs.`id`=f.`findings_status_id` " .
"WHERE t.`cat_id`=? AND " .