-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·176 lines (146 loc) · 7.94 KB
/
run.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This is the main driver file that the user runs. It takes command line
# arguments and calls all of the other modules. There is one positional
# argument that this file takes and one optional should follow -i
import json
import argparse
import process_dump
import email_results
import dump_plugin_output
from sys import exit
from base64 import b64decode
ASCII_ART = """ .,..,.,
.***//***/(//((/ _________ ______ ______
*,**,//((//(( .*., | _ _ |.' ____ \ .' ___ |
.,,./.,,****/***.,(((/, |_/ | | \_|| (___ \_|/ .' \_|
..,* . , , . ,/(/,, | | _.____`. | |
.,,. ./#,. _| |_ | \____) |\ `.___.'\\
. . . .,/*,./*,. |_____| \______.' `.____ .'
*(//.**,.,/(/%%%*,,,. ________ _
.*#&(**((*/#%&@&&%,.*. |_ __ | / |_
,... ,*//,*//*/%%%%%%*,/ | |_ \_|.---. _ .--. _ .--. .---.`| |-'
*,*,,. ,/(* ,,*/*/%&%./. | _| / /__\\\\[ `/'`\][ `/'`\]/ /__\\\\| |
./****, .* ..,*/,(((##(. _| |_ | \__., | | | | | \__.,| |,
./***/(*(,. ../...*%%%%%(* |_____| '.__.'[___] [___] '.__.'\__/
,//*/(/(((//,. ..,*%/%%&&&%(#,,,.. ..
.///((##(##(##(%&&&&%%%%&&&&&&&&%%##%%%%%%#/,
.(((((###/%%#%%&%#&%&&%&&&&@&&&&%%%#%%%%&&%%%#/.
.((###%%##%%%%%&&&&&%%&&&@@&&&&&%%%#&&&&&&&&&%#*
./#(#%%%#%%%%&&&&&&&&&&&&&@@@@@&&&%%%%%%&&&&&&&&%#/.
(#%%%%%%%%%&&&&&&@&@&&&&@@@@@@@&&%%%%%&&&&&&&&&&&%/.
,##%%%&&&&&&&&&&&&&&@@&@@@@@@@@@@&%%%%%%&&&&&&&&&&&&%/,,,.,,...
,#%%%%%&&&&&&&&&@@@@@@@@@@@@@@@@@&%%%&&&&&&&&&&&&&&&&&&&&&&&&&&&&%%#(*,.
*%%%%%&&&&&&@&@@@@@@@@@@@@@@@@@@@&&%%&&&&&&&@@@@@&&&&&@@@@@@@@@@@&&@&&&&&%*
,#%%&&&&&@&@@@@@@@&@@@@@@@@@@@@@@@&&%%&&&&&&@@@@@@&@@@@@@@@@@@@@@@@@@@@&&&%,
/%%&&&@&@@&@@@@@@@@@@@@@@@@@@@@@@@&%&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@@@@@@@&(
(%&&&@@@@@@&@@@&&@@@@@@&@@@@@@@@@&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@&@&@@@&/
,%%&&@@@@@@@@@@&&&@@@@@@@@@@@@@@@@&&&&&&&&&&&&&%%##((///**////(#&@@@@@@@@&/.
*%&@@@@@@/*(%&&@@&@@@@@&&@@@@@@@@@@@@@&&%%##((//**,... ..,,(&@@@/.
%&@@@&@@&//(#%&&&&@@@@@&&@@@@@@@&&&&&%%%%%%###((//
/&&(,@%,@&(#////#&@@@@&@@&&@%&&%###((//**,,,.
/#&/@@@*@@&&%&&%(*.
"""
# initiate_argparse()
#
# Function that initializes the argument parser, adding the necessary runtime arguments and parsing the user input
# Input - none
# Output - argparse Argument Parser object
def initiate_argparse():
parser = argparse.ArgumentParser(description='Helper script to retrieve plugin output from Security Center scans')
plugin_source = parser.add_mutually_exclusive_group(required=True)
plugin_source.add_argument('-P', '--plugin_id', dest='plugin_id', help='Plugin ID for the desired plugin output')
plugin_source.add_argument('-C', '--config', dest='config', help='Config file to load credentials and arguments')
parser.add_argument('-s', '--search_queries', dest='search_list', help='Input file for words to query output '
'(e.g. -s queries.txt)')
parser.add_argument('-R', '--repo_list', dest='repos', help='Input file for repositories to query (e.g. -R '
'repos.txt)')
parser.add_argument('-H', '--host_list', dest='hosts', help='Input file for hosts to query (e.g. -H hosts.txt)')
parser.add_argument('-i', '--ip_range', dest='ip_range', help='CIDR of IPs from which to gather data (e.g. '
'-i 130.0.0.0/24)')
parser.add_argument('-d', '--allow_duplicates', dest='duplicates', help='Change from default behavior of only '
'outputting latest scan results to show all results', default=False, action='store_true')
parser.add_argument('-e', '--email_results', dest='email_results', help='Email results of TSC Ferret to the given '
'recipients', default=False, action='store_true')
parser.add_argument('-o', '--output_type', dest='output', help='Change from default html output to a csv, pdf,'
' or json file', default='html')
parser.add_argument('-c', '--columns', dest='columns', help='Specify data columns to be included in the output. '
'Columns supported: \'IP\', \'DNS\', \'Repository\', '
'\'MAC\', \'L_SEEN\', \'CONTENT\'')
return parser.parse_args()
# check_valid_output_type()
#
# Ensures that the output file type specified by the user is supported by TSC Ferret
# Input - file_type: string containing the file type specified
# Output - none, will exit if unsupported
def check_valid_output_type(file_type):
if file_type.lower() not in process_dump.OUTPUT_TYPES:
print('Unsupported file type!\nSupported file types: %s' % ', '.join(process_dump.OUTPUT_TYPES))
exit(1)
return
# clean_columns()
#
# Function to remove all unknown column names from the user-specified columns list. Returns a list of columns that exist
# in the host data returned by SecurityCenter
def clean_columns(in_columns):
columns = in_columns.split(',')
temp_columns = []
for value in columns:
value = value.strip()
# If not a valid column to filter in
if value.upper() not in process_dump.HOST_VALUES + ['PLUGIN_INFO']:
print value + " column does not exist, removing from output columns"
continue
temp_columns.append(value)
if not temp_columns:
print "None of the specified columns exists. Showing all data instead"
return temp_columns
def main():
args = initiate_argparse()
print ASCII_ART
try:
if args.config:
f = open(args.config, 'r')
config = json.load(f)
f.close()
out_file_type = config['output']
to_email = config['email_results']
if config['columns']:
columns = clean_columns(config['columns'])
else:
columns = ''
dump_plugin_output.dump_plugin_data(config['plugin_id'], config['repo_list'], config['host_list'],
config['ip_range'], config['duplicates'], config['user'],
b64decode(config['pass']))
process_dump.create_table(out_file_type, columns, config['search_list'])
else:
out_file_type = args.output
to_email = args.email_results
if args.columns:
columns = clean_columns(args.columns)
else:
columns = ''
dump_plugin_output.dump_plugin_data(args.plugin_id, args.repos, args.hosts, args.ip_range, args.duplicates,
'', '')
process_dump.create_table(out_file_type, columns, args.search_list)
if to_email:
if args.config:
email_results.craft_and_send_message(config['plugin_id'], config['host_list'], config['repo_list'],
config['ip_range'], config['search_list'], config['duplicates'],
out_file_type)
else:
email_results.craft_and_send_message(args.plugin_id, args.hosts, args.repos, args.ip_range,
args.search_list, args.duplicates, out_file_type)
except Exception as e:
print '\n###### Runtime Error'
print 'Exception: [' + str(e) + ']:'
exit(1)
except KeyboardInterrupt:
print '\n###### Keyboard Interrupt'
print 'exiting...'
exit(1)
print "Done."
return 0
# # # # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
main()