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
|
# File 'lib/nexpose_sccm.rb', line 44
def generate_updates(settings, input_file=nil)
scope = settings[:sccm][:software_update_group][:scope]
data_source = settings[:sccm][:data_source]
if settings[:sccm][:generate_unknown_device_report]
date = Time.now.strftime('%Y%m%d_%H%M%S')
report = "devices-not-found-sccm-#{date}.csv"
report_location = settings[:sccm][:report_location]
unless File.exist?(File.expand_path(report_location))
Dir.mkdir(report_location)
end
csv_report_contents = Set.new()
end
sccm_devices = @sccm.get_devices
collections = @sccm.get_collections(sccm_devices)
@logger.debug("Logging into Nexpose / DWH")
@nsc = NexposeSCCM::DataSource::Connection.new(data_source,
settings[:postgres],
settings[:nexpose])
@logger.info("Successfully connected to Data Source #{data_source}")
sup_data = []
begin
query = settings[:queries][scope.to_sym][data_source.to_sym]
match_for_prompt = query.scan(/'<.*?>'/)
unless match_for_prompt.nil? || match_for_prompt.empty?
if !input_file.nil? && File.exist?(input_file)
inputs = CSV.read(input_file, :headers=>true)
end
match_for_prompt.each do |match|
if !inputs.nil? && inputs..include?(match.gsub("'<",'').gsub(">'",''))
input = inputs[match.gsub("'<",'').gsub(">'",'')]
else
input = prompt("Please provide #{match} to be processed " \
"(comma separated for any lists): ").strip.split(',')
end
input = input.map{|e| "'#{e}'"}
query.gsub!(/#{match}/, "#{input.join(',')}")
end
end
@logger.debug("Retrieving Data")
sup_data = @nsc.fetch_data(query)
rescue => e
@logger.error("There was an error running the query. " \
"Check the scope and data source settings for validity: #{e}")
exit
end
unless sup_data.length > 0
@logger.debug("No updates returned using query: #{query}")
@logger.info('No updates to work on. Exiting...')
exit 0
end
@logger.info("Software Update Group scope: #{scope}")
sugs = {}
@logger.debug("Getting CI_IDS using scope: #{scope}")
@logger.info("Looping through output of length #{sup_data.length}")
sup_data.each do |o|
unless o.key?(:key)
@logger.error("Query to pull data from #{data_source} " \
"does not have the 'key' field, exiting...")
exit 1
end
key = o[settings[:sccm][:software_update_group_key].to_sym]
collection_key = o[settings[:sccm][:collection_key].to_sym]
key_ip = o[:ip_address].split(':')[0].strip
key_hostname = o[:host_name]
device = sccm_devices.select do |dev|
(dev.host_name.to_s.casecmp(key_hostname) == 0 unless key_hostname.nil?) ||
(dev.ip_address.to_a.include?(key_ip) unless key_ip.nil?)
end.first
if device.nil?
if settings[:sccm][:generate_unknown_device_report]
csv_report_contents << [key_ip, key_hostname]
end
else
collection_name = "Rapid7_#{collection_key}"
collection_match = collections.select do |collection|
collection.name.eql?(collection_name)
end.first
if collection_match.nil?
collection_match = NexposeSCCM::Collection.new(collection_name)
collection_match.members.add(device)
collections << collection_match
else
collection_match.members.add(device)
end
end
unless sugs.key?(key)
sugs[key] = {
:ci_ids => [],
:description => "Scope: #{scope}",
:name => "Rapid7_#{key}"
}
end
nexpose_id = o[:nexpose_id]
uuid = nil
begin
match = nexpose_id.match(/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/).captures
uuid = match[0]
rescue
@logger.debug('Must be no UUID available, going to next...')
end
next if uuid.nil?
ci_ids = @sccm.get_ci_id(uuid)
next unless ci_ids.length > 0
sugs[key][:ci_ids] += ci_ids
end
@logger.debug("Retrieving current Software Update Groups from SCCM")
groups = @sccm.get_software_update_groups
sug_objects=[]
sug_action_type = settings[:sccm][:software_update_group][:action]
sugs.each do |k,v|
@logger.info("Working on SUP: #{k}")
match = groups.select { |g| g.name.casecmp(v[:name]) == 0 }
if sug_action_type.casecmp('update') == 0
if match.length > 0
sug = match[0]
sug.ci_ids = v[:ci_ids]
sug.description = v[:description]
res = sug.save(@sccm)
sug_objects << sug
if res
msg = "Successfully updated Software Update Group: #{v[:name]}"
@logger.debug(msg)
else
@logger.error("Error updating Software Update Group: #{v[:name]}")
end
end
end
if sug_action_type.casecmp('create') == 0 || match.empty?
sug = NexposeSCCM::SoftwareUpdateGroup.new(v[:name],
v[:description],
v[:ci_ids])
sug_objects << sug
res = sug.save(@sccm)
if res
msg = "Successfully created Software Update Group: #{v[:name]}"
@logger.debug(msg)
else
@logger.error("Error creating Software Update Group: #{v[:name]}")
end
end
end
@logger.debug("Saving Collections")
collections.each do |collection|
collection.save(@sccm)
end
if settings[:sccm].key?(:create_deployment_package) &&
settings[:sccm][:create_deployment_package]
@logger.debug("Creating the Deployment Packages")
sug_objects.each do |sug_object|
name = sug_object.name
package_name = "Rapid7-Deployment-Package-#{name}"
package_path = "#{@sccm.staging}\\#{name}"
deployment_package = @sccm.get_deployment_package(package_name)
package_id =
if deployment_package.empty?
nil
else
deployment_package.first[:package_id]
end
package = NexposeSCCM::DeploymentPackage.new(package_name,
name,
package_path,
package_id)
unless sug_object.ci_ids.empty?
@logger.debug("Saving the Deployment Package: #{package_name}")
package.save(@sccm) if package.id.nil?
@logger.debug("Downloading the Deployment Package")
package.download_updates(@sccm) if settings[:sccm][:download_updates]
end
end
end
if settings[:sccm][:generate_unknown_device_report]
report_length = csv_report_contents.length
if report_length > 0
@logger.debug("Creating Unknown Device report")
CSV.open(report_location + report, 'ab') do |csv|
csv << ['IP Address','Hostname']
csv_report_contents.each do |entry|
csv << entry
end
end
end
@logger.debug("Unknown Device report length: #{report_length}")
end
@logger.info("Nexpose SCCM integration has finished.")
end
|