-
Notifications
You must be signed in to change notification settings - Fork 68
/
createdbstructure.sql
2611 lines (1889 loc) · 70.6 KB
/
createdbstructure.sql
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
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET search_path = public, pg_catalog;
--
-- Name: adddomain(integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION adddomain(_footprint_id integer, _domain_name character varying) RETURNS void
LANGUAGE plpgsql
AS $$
declare _new_domain_id bigint;
begin
if (select count(*) from domains where footprint_id = _footprint_id and upper(domain_name) = upper(_domain_name)) = 0 then
insert into domains (footprint_id, domain_name) values (_footprint_id, upper(_domain_name)) returning cast(id as bigint) into _new_domain_id;
execute executeTriggers(_footprint_id, _new_domain_id, 10, upper(_domain_name));
end if;
end;
$$;
ALTER FUNCTION public.adddomain(_footprint_id integer, _domain_name character varying) OWNER TO postgres;
--
-- Name: adddomaincreds(integer, integer, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION adddomaincreds(_footprint_id integer, _host_data_id integer, _domain character varying, _username character varying, _cleartext_password character varying, _lm_hash character varying, _ntlm_hash character varying) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from domain_credentials where footprint_id = _footprint_id and upper(domain) = upper(_domain) and upper(username) = upper(_username)) = 0 then
insert into domain_credentials
(footprint_id, domain, username, cleartext_password, lm_hash, ntlm_hash) values
(_footprint_id, _domain, _username, _cleartext_password, _lm_hash, _ntlm_hash);
elseif (_cleartext_password != '') then
update domain_credentials set cleartext_password = _cleartext_password where footprint_id = _footprint_id and upper(domain) = upper(_domain) and upper(username) = upper(_username);
elseif (_ntlm_hash != '') then
update domain_credentials set lm_hash = _lm_hash, ntlm_hash = _ntlm_hash where footprint_id = _footprint_id and upper(domain) = upper(_domain) and upper(username) = upper(_username);
end if;
execute addDomain(_footprint_id, _domain);
end;
$$;
ALTER FUNCTION public.adddomaincreds(_footprint_id integer, _host_data_id integer, _domain character varying, _username character varying, _cleartext_password character varying, _lm_hash character varying, _ntlm_hash character varying) OWNER TO postgres;
--
-- Name: adddomaingroup(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION adddomaingroup(_footprint_id integer, _domain_id integer, _group_name character varying) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from domain_groups where footprint_id = _footprint_id and domain_id = _domain_id and group_name = _group_name) = 0 then
insert into domain_groups (footprint_id, domain_id, group_name) values (_footprint_id, _domain_id, _group_name);
end if;
end;
$$;
ALTER FUNCTION public.adddomaingroup(_footprint_id integer, _domain_id integer, _group_name character varying) OWNER TO postgres;
--
-- Name: adddomainusertogroup(integer, integer, character varying, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION adddomainusertogroup(_footprint_id integer, _domain_id integer, _username character varying, _domain_group_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
declare _domain_credentials_id int;
declare _sub_group_id int;
declare _domain_name varchar;
begin
if (select count(*) from domain_groups where footprint_id = _footprint_id and domain_id = _domain_id and group_name = _username) = 0 then
-- if (select count(*) from domain_credentials dc join domains d on d.domain_name = dc.domain where dc.footprint_id = d.footprint_id and d.id = _domain_id and d.footprint_id = _footprint_id and dc.username = _username) = 0 then
-- insert into domain_credentials
-- (footprint_id, domain, username, cleartext_password) values
-- (_footprint_id, (select domain_name from domains where id = _domain_id), _username, '');
-- end if;
select domain_name into _domain_name from domains where id = _domain_id;
execute adddomaincreds(_footprint_id, 0, _domain_name, _username, '', '', '');
select dc.id into _domain_credentials_id from domain_credentials dc join domains d on dc.domain = d.domain_name where upper(username) = upper(_username) and d.id = _domain_id;
if (select count(*) from domain_user_group_map where domain_credentials_id = _domain_credentials_id and domain_group_id = _domain_group_id) = 0 then
insert into domain_user_group_map (domain_credentials_id, domain_group_id) values (_domain_credentials_id, _domain_group_id);
end if;
else
select id into _sub_group_id from domain_groups where footprint_id = _footprint_id and domain_id = _domain_id and group_name = _username;
if (select count(*) from domain_sub_groups where parent_group_id = _domain_group_id and child_group_id = _sub_group_id) = 0 then
insert into domain_sub_groups (parent_group_id, child_group_id) values (_domain_group_id, _sub_group_id);
end if;
end if;
end;
$$;
ALTER FUNCTION public.adddomainusertogroup(_footprint_id integer, _domain_id integer, _username character varying, _domain_group_id integer) OWNER TO postgres;
--
-- Name: addfulllistport(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addfulllistport(_port_number integer) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from ports_to_scan where type_id = 2 and port_number = _port_number) = 0 then
insert into ports_to_scan (type_id, port_number) values (2, _port_number);
end if;
end;
$$;
ALTER FUNCTION public.addfulllistport(_port_number integer) OWNER TO postgres;
--
-- Name: addhost(integer, character varying, character varying, boolean); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addhost(_footprint_id integer, _ip_address character varying, _host_name character varying, _is_dc boolean) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE _net_range VARCHAR(45);
BEGIN
_net_range := (select concat(SUBSTRING(_ip_address, 1, length(_ip_address) - position('.' in reverse(_ip_address))), '.0/24'));
if (select count(*) from host_data where footprint_id = _footprint_id and ip_address = _ip_address) = 0 then
insert into host_data (footprint_id, ip_address, host_name) values (_footprint_id, _ip_address, _host_name);
execute executeTriggers(_footprint_id, currval('host_data_id_seq'), 1, _ip_address);
end if;
if (select count(*) from net_ranges where footprint_id = _footprint_id and net_range = _net_range) = 0 then
insert into net_ranges (footprint_id, net_range) values (_footprint_id, _net_range);
execute executeTriggers(_footprint_id, currval('net_ranges_id_seq'), 3, _net_range);
end if;
if _host_name != '' then
update host_data set host_name = _host_name where footprint_id = _footprint_id and ip_address = _ip_address;
end if;
if (_is_dc = True) then
update host_data set is_dc = _is_dc where footprint_id = _footprint_id and ip_address = _ip_address;
end if;
END;
$$;
ALTER FUNCTION public.addhost(_footprint_id integer, _ip_address character varying, _host_name character varying, _is_dc boolean) OWNER TO postgres;
--
-- Name: addlocalcredentials(integer, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addlocalcredentials(_host_data_id integer, _username character varying, _cleartext_password character varying, _lm_hash character varying, _ntlm_hash character varying) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from local_credentials where host_data_id = _host_data_id and username = _username) = 0 then
insert into local_credentials (host_data_id, username) values (_host_data_id, _username);
end if;
if (_cleartext_password != '') then
update local_credentials set cleartext_password = _cleartext_password where host_data_id = _host_data_id and username = _username;
end if;
if (_lm_hash != '' and _ntlm_hash != '') then
update local_credentials set lm_hash = _lm_hash, ntlm_hash = _ntlm_hash where host_data_id = _host_data_id and username = _username;
end if;
if (select count(*) from local_credentials lc join local_credentials_map m on lc.id = m.local_credentials_id where lc.host_data_id = _host_data_id and lc.username = _username) = 0 then
insert into local_credentials_map (local_credentials_id, host_data_id, valid) select id, host_data_id, true from local_credentials where host_data_id = _host_data_id and username = _username;
end if;
end;
$$;
ALTER FUNCTION public.addlocalcredentials(_host_data_id integer, _username character varying, _cleartext_password character varying, _lm_hash character varying, _ntlm_hash character varying) OWNER TO postgres;
--
-- Name: addport(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addport(_footprint_id integer, _host_data_id integer, _port_num integer) RETURNS void
LANGUAGE plpgsql
AS $$
declare _new_port_id bigint;
begin
if (select count(*) from port_data where host_data_id = _host_data_id and port_number = _port_num) = 0 then
insert into port_data (host_data_id, port_number) values (_host_data_id, _port_num) returning cast(id as bigint) into _new_port_id;
--select executeTriggers(_footprint_id, currval('port_data_id_seq'), 2, _port_num );
execute executeTriggers(_footprint_id, _new_port_id, 2, _port_num::varchar );
end if;
end;
$$;
ALTER FUNCTION public.addport(_footprint_id integer, _host_data_id integer, _port_num integer) OWNER TO postgres;
--
-- Name: addscopeitem(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addscopeitem(_footprint_id integer, _item_type integer, _item_value character varying) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
if (select count(*) from scope where footprint_id = _footprint_id and item_type = _item_type and item_value = _item_value) = 0 then
insert into scope (footprint_id, item_type, item_value) values (_footprint_id, _item_type, _item_value);
if _item_type = 1 then
execute executeTriggers(_footprint_id, currval('scope_id_seq'), 6, _item_value);
end if;
if _item_type = 2 then
execute executeTriggers(_footprint_id, currval('scope_id_seq'), 7, _item_value);
end if;
end if;
END;
$$;
ALTER FUNCTION public.addscopeitem(_footprint_id integer, _item_type integer, _item_value character varying) OWNER TO postgres;
--
-- Name: addshortlistport(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addshortlistport(_port_number integer) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from ports_to_scan where type_id = 1 and port_number = _port_number) = 0 then
insert into ports_to_scan (type_id, port_number) values (1, _port_number);
end if;
end;
$$;
ALTER FUNCTION public.addshortlistport(_port_number integer) OWNER TO postgres;
--
-- Name: addtasklistitem(integer, integer, integer, boolean, boolean); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addtasklistitem(_footprint_id integer, task_descriptions_id integer, _item_identifier integer, _in_progress boolean, _completed boolean) RETURNS integer
LANGUAGE plpgsql
AS $$
begin
insert into task_list
(footprint_id, task_descriptions_id, item_identifier, in_progress, completed) values
(_footprint_id, task_descriptions_id, _item_identifier, _in_progress, _completed);
return(currval('task_list_id_seq'));
end;
$$;
ALTER FUNCTION public.addtasklistitem(_footprint_id integer, task_descriptions_id integer, _item_identifier integer, _in_progress boolean, _completed boolean) OWNER TO postgres;
--
-- Name: addtodomaincredentialsmap(integer, integer, integer, boolean); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addtodomaincredentialsmap(_footprint_id integer, _host_data_id integer, _domain_credentials_id integer, _valid boolean) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from domain_credentials_map where footprint_id = _footprint_id and host_data_id = _host_data_id and domain_credentials_id = _domain_credentials_id) = 0 then
insert into domain_credentials_map
(footprint_id, host_data_id, domain_credentials_id, valid) values
(_footprint_id, _host_data_id, _domain_credentials_id, _valid);
end if;
if (_valid = true) then
execute executeTriggers(_footprint_id, (select id from domain_credentials_map where footprint_id = _footprint_id and host_data_id = _host_data_id and domain_credentials_id = _domain_credentials_id), 9, '');
update domain_credentials_map set valid = _valid where footprint_id = _footprint_id and host_data_id = _host_data_id and domain_credentials_id = _domain_credentials_id;
end if;
end;
$$;
ALTER FUNCTION public.addtodomaincredentialsmap(_footprint_id integer, _host_data_id integer, _domain_credentials_id integer, _valid boolean) OWNER TO postgres;
--
-- Name: addtoken(integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addtoken(_host_id integer, _token character varying) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from tokens where host_id = _host_id and token = _token) = 0 then
insert into tokens (host_id, token) values (_host_id, _token);
end if;
end;
$$;
ALTER FUNCTION public.addtoken(_host_id integer, _token character varying) OWNER TO postgres;
--
-- Name: addtolocalcredentialsmap(integer, integer, boolean); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addtolocalcredentialsmap(_host_data_id integer, _local_credentials_id integer, _valid boolean) RETURNS void
LANGUAGE plpgsql
AS $$
declare _username varchar(45);
declare _cleartext_password varchar(45);
declare _lm_hash varchar(45);
declare _ntlm_hash varchar(45);
declare credentials_id int;
begin
if (_valid) = true then
select username, cleartext_password, lm_hash, ntlm_hash into _username, _cleartext_password, _lm_hash, _ntlm_hash from local_credentials where id = _local_credentials_id;
select addLocalCredentials(_host_data_id, _username, _cleartext_password, _lm_hash, _ntlm_hash);
select id into credentials_id from local_credentials where (host_data_id, username, cleartext_password) in (select hd.id, lc.username, lc.cleartext_password from host_data hd, local_credentials lc where hd.id = _host_data_id and lc.id = _local_credentials_id);
select executeTriggers((select footprint_id from host_data where id = _host_data_id), credentials_id, 5, '');
else
if (select count(*) from local_credentials_map where host_data_id = _host_data_id and local_credentials_id = _local_credentials_id) = 0 then
insert into local_credentials_map (host_data_id, local_credentials_id, valid) values (_host_data_id, _local_credentials_id, _valid);
end if;
end if;
end;
$$;
ALTER FUNCTION public.addtolocalcredentialsmap(_host_data_id integer, _local_credentials_id integer, _valid boolean) OWNER TO postgres;
--
-- Name: addvulnerability(integer, integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addvulnerability(_footprint_id integer, _port_data_id integer, _vulnerability_description_id integer, _details character varying) RETURNS void
LANGUAGE plpgsql
AS $$
declare _new_vuln_id bigint;
begin
if (select count(*) from vulnerabilities where port_data_id = _port_data_id and vulnerability_descriptions_id = _vulnerability_description_id) = 0 then
insert into vulnerabilities (port_data_id, vulnerability_descriptions_id, details) values (_port_data_id, _vulnerability_description_id, _details) returning cast(id as bigint) into _new_vuln_id;
execute executeTriggers(_footprint_id, _new_vuln_id, 4, (select description from vulnerability_descriptions where id = _vulnerability_description_id));
end if;
end;
$$;
ALTER FUNCTION public.addvulnerability(_footprint_id integer, _port_data_id integer, _vulnerability_description_id integer, _details character varying) OWNER TO postgres;
--
-- Name: addwebsite(integer, character varying, text, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addwebsite(_port_data_id integer, _html_title character varying, _html_body text, _screenshot text) RETURNS void
LANGUAGE plpgsql
AS $$
begin
if (select count(*) from websites where port_data_id = _port_data_id) = 0 then
insert into websites (port_data_id, html_title, html_body, screenshot) values (_port_data_id, _html_title, _html_body, _screenshot);
end if;
end;
$$;
ALTER FUNCTION public.addwebsite(_port_data_id integer, _html_title character varying, _html_body text, _screenshot text) OWNER TO postgres;
--
-- Name: countdomaingroupstoexpand(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION countdomaingroupstoexpand(_footprint_id integer) RETURNS integer
LANGUAGE plpgsql
AS $$
begin
return (
select
count(*)
from
domain_groups dg
join domains d on d.id = dg.domain_id and d.footprint_id = dg.footprint_id
join domain_credentials dc on d.domain_name = dc.domain
join domain_credentials_map m on m.domain_credentials_id = dc.id
join host_data hd on m.host_data_id = hd.id
where
d.footprint_id = dc.footprint_id and
d.footprint_id = hd.footprint_id and
d.footprint_id = m.footprint_id and
m.valid = true and
dg.users_gathered = false and
m.psexec_failed = false and
m.dgu_failed = false and
dg.id not in (select item_identifier from task_list where task_descriptions_id = 26 and footprint_id = _footprint_id and in_progress = true) and
hd.footprint_id = _footprint_id );
end;
$$;
ALTER FUNCTION public.countdomaingroupstoexpand(_footprint_id integer) OWNER TO postgres;
--
-- Name: createfootprint(character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION createfootprint(_footprint_name character varying) RETURNS integer
LANGUAGE plpgsql
AS $$
BEGIN
if (select count(*) from footprints where footprint_name = _footprint_name) = 0 then
insert into footprints (footprint_name) values (_footprint_name);
end if;
return (select id from footprints where footprint_name = _footprint_name);
END;
$$;
ALTER FUNCTION public.createfootprint(_footprint_name character varying) OWNER TO postgres;
--
-- Name: deleteportfromlist(integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION deleteportfromlist(_type_id integer, _port_number integer) RETURNS void
LANGUAGE plpgsql
AS $$
begin
delete from ports_to_scan where type_id = _type_id and port_number = _port_number;
end;
$$;
ALTER FUNCTION public.deleteportfromlist(_type_id integer, _port_number integer) OWNER TO postgres;
--
-- Name: executetriggers(integer, bigint, integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION executetriggers(_footprint_id integer, _item_identifier bigint, _trigger_event_id integer, _value character varying) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
insert into task_list (footprint_id, task_descriptions_id, item_identifier)
select
_footprint_id as footprint_id, td.id as task_descriptions_id, _item_identifier as item_identifier
from
task_categories tc
join task_descriptions td on tc.id = td.task_categories_id
join trigger_events te on te.task_descriptions_id = td.id
join trigger_descriptions trd on trd.id = te.trigger_descriptions_id
where
trd.id = _trigger_event_id and
_value like te.value_mask and
te.enabled = true;
END;
$$;
ALTER FUNCTION public.executetriggers(_footprint_id integer, _item_identifier bigint, _trigger_event_id integer, _value character varying) OWNER TO postgres;
--
-- Name: getdomaincredstoretry(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION getdomaincredstoretry(_footprint_id integer) RETURNS TABLE(host_data_id integer, ip_address character varying, domain_creds_id integer, domain character varying, username character varying, cleartext_password character varying)
LANGUAGE plpgsql
AS $$
declare _new_map_id bigint;
begin
insert into domain_credentials_map (footprint_id, host_data_id, domain_credentials_id)
(select
hd.footprint_id, hd.id, dc.id
from
domain_credentials dc
join host_data hd on hd.footprint_id = dc.footprint_id
join port_data pd on pd.host_data_id = hd.id and pd.port_number = 445 and
(hd.id, dc.id) not in (select m.host_data_id, m.domain_credentials_id from domain_credentials_map m)
where
dc.valid = true and
hd.footprint_id = _footprint_id and
hd.is_dc in (true)
limit 1) returning id into _new_map_id;
return query
select
hd.id, hd.ip_address, dc.id, dc.domain, dc.username, dc.cleartext_password
from
domain_credentials_map m
join host_data hd on hd.id = m.host_data_id
join domain_credentials dc on dc.id = m.domain_credentials_id
where
hd.footprint_id = _footprint_id and
m.id = _new_map_id;
end;
$$;
ALTER FUNCTION public.getdomaincredstoretry(_footprint_id integer) OWNER TO postgres;
--
-- Name: getdomaincredstoverify(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION getdomaincredstoverify(_footprint_id integer) RETURNS TABLE(domain_credentials_id integer, ip_address character varying, domain character varying, username character varying, cleartext_password character varying, host_data_id integer, task_list_id integer)
LANGUAGE plpgsql
AS $$
declare _new_task_id bigint;
begin
insert into task_list (footprint_id, task_descriptions_id, item_identifier, in_progress)
(select
_footprint_id, 19, dc.id, True
from
domain_credentials dc
join domains d on upper(d.domain_name) = upper(dc.domain) and dc.footprint_id = d.footprint_id
join host_data hd on upper(hd.domain) = upper(d.domain_name) and hd.footprint_id = dc.footprint_id
join port_data pd on pd.host_data_id = hd.id
where
hd.footprint_id = _footprint_id and
pd.port_number = 445 and
dc.cleartext_password != '' and
dc.verified = false and
dc.id not in (select m.domain_credentials_id from domain_credentials_map m where m.footprint_id = _footprint_id) and
dc.id not in (select tl.item_identifier from task_list tl where tl.footprint_id = _footprint_id and tl.task_descriptions_id = 19 and (tl.in_progress = true or tl.completed = true)) and
hd.is_dc = true limit 1) returning task_list.id into _new_task_id;
return query
select
dc.id, hd.ip_address, dc.domain, dc.username, dc.cleartext_password, hd.id, tl.id
from
domain_credentials dc
join domains d on upper(d.domain_name) = upper(dc.domain) and dc.footprint_id = d.footprint_id
join host_data hd on upper(hd.domain) = upper(d.domain_name) and hd.footprint_id = dc.footprint_id
join port_data pd on pd.host_data_id = hd.id
join task_list tl on tl.item_identifier = dc.id
where
tl.id = _new_task_id and pd.port_number = 445 and task_descriptions_id = 19 and hd.is_dc = true and hd.footprint_id = _footprint_id;
end;
$$;
ALTER FUNCTION public.getdomaincredstoverify(_footprint_id integer) OWNER TO postgres;
--
-- Name: getdomaingrouptoexpand(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION getdomaingrouptoexpand(_footprint_id integer) RETURNS TABLE(domain_id integer, ip_address character varying, domain_name character varying, username character varying, cleartext_password character varying, map_id integer, group_name character varying, group_id integer)
LANGUAGE plpgsql
AS $$
begin
return query
select
d.id,
hd.ip_address,
dc.domain, dc.username, dc.cleartext_password,
m.id,
dg.group_name, dg.id
from
domain_groups dg
join domains d on d.id = dg.domain_id and d.footprint_id = dg.footprint_id
join domain_credentials dc on d.domain_name = dc.domain
join domain_credentials_map m on m.domain_credentials_id = dc.id
join host_data hd on m.host_data_id = hd.id
where
d.footprint_id = dc.footprint_id and
d.footprint_id = hd.footprint_id and
d.footprint_id = m.footprint_id and
m.valid = true and
dg.users_gathered = false and
m.psexec_failed = false and
m.dgu_failed = false and
dg.id not in (select item_identifier from task_list where task_descriptions_id = 26 and footprint_id = _footprint_id and in_progress = true) and
hd.footprint_id = _footprint_id
order by dg.id
limit 1;
end;
$$;
ALTER FUNCTION public.getdomaingrouptoexpand(_footprint_id integer) OWNER TO postgres;
--
-- Name: getpendingtask(integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION getpendingtask(_footprint_id integer, _task_descriptions_id integer) RETURNS TABLE(task_id integer, item_identifier integer)
LANGUAGE plpgsql
AS $$
declare _task_id int;
declare _item_identifier int;
begin
UPDATE task_list s
SET in_progress = true
FROM (
SELECT tl.id, tl.item_identifier
FROM task_list tl
WHERE tl.in_progress = false AND
tl.completed = false AND
tl.task_descriptions_id = _task_descriptions_id AND
tl.footprint_id = _footprint_id AND
pg_try_advisory_xact_lock(tl.id)
ORDER BY id
LIMIT 1
FOR UPDATE
) sub
WHERE s.id = sub.id
RETURNING s.id, s.item_identifier into _task_id, _item_identifier;
return QUERY select _task_id, _item_identifier;
--select _task_id, _item_identifier;
end;
$$;
ALTER FUNCTION public.getpendingtask(_footprint_id integer, _task_descriptions_id integer) OWNER TO postgres;
--
-- Name: setdomaincredsverified(integer, integer, boolean); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION setdomaincredsverified(_footprint_id integer, _domain_creds_id integer, _valid boolean) RETURNS void
LANGUAGE plpgsql
AS $$
begin
update domain_credentials set verified = true, valid = _valid where id = _domain_creds_id;
end;
$$;
ALTER FUNCTION public.setdomaincredsverified(_footprint_id integer, _domain_creds_id integer, _valid boolean) OWNER TO postgres;
--
-- Name: test(character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION test(_ip_address character varying) RETURNS TABLE(a character varying, b character varying)
LANGUAGE plpgsql
AS $$
DECLARE _net_range VARCHAR(45);
begin
_net_range := 'test';
raise notice 'hello %', 'abc';
return query select _ip_address,_ip_address;
end;
$$;
ALTER FUNCTION public.test(_ip_address character varying) OWNER TO postgres;
--
-- Name: updatetaskstatus(integer, boolean, boolean, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION updatetaskstatus(_task_id integer, _in_progress boolean, _completed boolean, _log text) RETURNS void
LANGUAGE plpgsql
AS $$
begin
update task_list set in_progress = _in_progress, completed = _completed, log = _log where id = _task_id;
end;
$$;
ALTER FUNCTION public.updatetaskstatus(_task_id integer, _in_progress boolean, _completed boolean, _log text) OWNER TO postgres;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: domain_credentials; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE domain_credentials (
id integer NOT NULL,
footprint_id integer NOT NULL,
domain character varying(45) NOT NULL,
username character varying(45) NOT NULL,
cleartext_password character varying(45) DEFAULT ''::character varying NOT NULL,
verified boolean DEFAULT false NOT NULL,
lm_hash character varying(45) DEFAULT ''::character varying NOT NULL,
ntlm_hash character varying(45) DEFAULT ''::character varying NOT NULL,
valid boolean DEFAULT false NOT NULL
);
ALTER TABLE domain_credentials OWNER TO postgres;
--
-- Name: domain_credentials_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE domain_credentials_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE domain_credentials_id_seq OWNER TO postgres;
--
-- Name: domain_credentials_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE domain_credentials_id_seq OWNED BY domain_credentials.id;
--
-- Name: domain_credentials_map; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE domain_credentials_map (
id integer NOT NULL,
footprint_id integer NOT NULL,
host_data_id integer NOT NULL,
domain_credentials_id integer NOT NULL,
valid boolean DEFAULT false NOT NULL,
psexec_failed boolean DEFAULT false NOT NULL,
dgu_failed boolean DEFAULT false NOT NULL
);
ALTER TABLE domain_credentials_map OWNER TO postgres;
--
-- Name: domain_credentials_map_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE domain_credentials_map_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE domain_credentials_map_id_seq OWNER TO postgres;
--
-- Name: domain_credentials_map_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE domain_credentials_map_id_seq OWNED BY domain_credentials_map.id;
--
-- Name: domain_groups; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE domain_groups (
id integer NOT NULL,
footprint_id integer NOT NULL,
domain_id integer NOT NULL,
group_name character varying(45) NOT NULL,
users_gathered boolean DEFAULT false
);
ALTER TABLE domain_groups OWNER TO postgres;
--
-- Name: domain_groups_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE domain_groups_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE domain_groups_id_seq OWNER TO postgres;
--
-- Name: domain_groups_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE domain_groups_id_seq OWNED BY domain_groups.id;
--
-- Name: domain_sub_groups; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE domain_sub_groups (
id integer NOT NULL,
parent_group_id integer,
child_group_id integer
);
ALTER TABLE domain_sub_groups OWNER TO postgres;
--
-- Name: domain_sub_groups_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE domain_sub_groups_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE domain_sub_groups_id_seq OWNER TO postgres;
--
-- Name: domain_sub_groups_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE domain_sub_groups_id_seq OWNED BY domain_sub_groups.id;
--
-- Name: domain_user_group_map; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE domain_user_group_map (
id integer NOT NULL,
domain_credentials_id integer,
domain_group_id integer
);
ALTER TABLE domain_user_group_map OWNER TO postgres;
--
-- Name: domain_user_group_map_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE domain_user_group_map_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE domain_user_group_map_id_seq OWNER TO postgres;
--
-- Name: domain_user_group_map_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE domain_user_group_map_id_seq OWNED BY domain_user_group_map.id;
--
-- Name: domains; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE domains (
id integer NOT NULL,
footprint_id integer NOT NULL,
domain_name character varying(45) NOT NULL,
info_gathered boolean DEFAULT false NOT NULL,
hashes_extracted boolean DEFAULT false NOT NULL
);
ALTER TABLE domains OWNER TO postgres;
--
-- Name: domains_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE domains_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE domains_id_seq OWNER TO postgres;
--
-- Name: domains_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE domains_id_seq OWNED BY domains.id;
--
-- Name: exploit_logs; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE exploit_logs (
id integer NOT NULL,
host_data_id integer NOT NULL,
vulnerability_description_id integer NOT NULL,
log text NOT NULL
);
ALTER TABLE exploit_logs OWNER TO postgres;
--
-- Name: exploit_logs_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE exploit_logs_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE exploit_logs_id_seq OWNER TO postgres;
--
-- Name: exploit_logs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE exploit_logs_id_seq OWNED BY exploit_logs.id;
--
-- Name: footprints; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE footprints (
id integer NOT NULL,
footprint_name character varying(45) NOT NULL
);
ALTER TABLE footprints OWNER TO postgres;
--
-- Name: footprints_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE footprints_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE footprints_id_seq OWNER TO postgres;
--
-- Name: footprints_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE footprints_id_seq OWNED BY footprints.id;
--
-- Name: host_data; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE host_data (
id integer NOT NULL,
footprint_id integer NOT NULL,
ip_address character varying(45) NOT NULL,
host_name character varying(45) DEFAULT ''::character varying NOT NULL,
computer_name character varying(45) DEFAULT ''::character varying NOT NULL,
os character varying(100) DEFAULT ''::character varying NOT NULL,
architecture character varying(45) DEFAULT ''::character varying NOT NULL,
system_language character varying(45) DEFAULT ''::character varying NOT NULL,
domain character varying(45) DEFAULT ''::character varying NOT NULL,
is_dc boolean DEFAULT false NOT NULL,
successful_info_gather boolean DEFAULT false NOT NULL