@@ -41,19 +41,19 @@ def commands
41
41
# Tab completion for the dns command
42
42
#
43
43
# @param str [String] the string currently being typed before tab was hit
44
- # @param words [Array<String>] the previously completed words on the command line. words is always
45
- # at least 1 when tab completion has reached this stage since the command itself has been completed
44
+ # @param words [Array<String>] the previously completed words on the command line. The array
45
+ # contains at least one entry when tab completion has reached this stage since the command itself has been completed
46
46
def cmd_dns_tabs ( str , words )
47
47
return if driver . framework . dns_resolver . nil?
48
48
49
49
if words . length == 1
50
- options = [ 'add' , 'del' , 'remove' , 'flush ' , 'print' ]
50
+ options = [ 'add' , 'del' , 'remove' , 'purge ' , 'print' ]
51
51
return options . select { |opt | opt . start_with? ( str ) }
52
52
end
53
53
54
54
cmd = words [ 1 ]
55
55
case cmd
56
- when 'flush ' , 'print'
56
+ when 'purge ' , 'print'
57
57
# These commands don't have any arguments
58
58
return
59
59
when 'add'
@@ -95,33 +95,41 @@ def cmd_dns_tabs(str, words)
95
95
end
96
96
97
97
def cmd_dns_help
98
- print_line 'Usage: dns'
99
- print_line
100
98
print_line "Manage Metasploit's DNS resolution behaviour"
101
99
print_line
102
100
print_line "Usage:"
103
- print_line " dns [add/remove ] [--session <session_id>] [--rule <wildcard DNS entry>] <IP Address> <IP Address> ..."
104
- print_line " dns [get] <hostname> "
105
- print_line " dns [flush ]"
101
+ print_line " dns [add] [--session <session_id>] [--rule <wildcard DNS entry>] <IP Address> <IP Address> ..."
102
+ print_line " dns [remove/del] -i <entry id> [-i <entry id> ...] "
103
+ print_line " dns [purge ]"
106
104
print_line " dns [print]"
107
105
print_line
108
106
print_line "Subcommands:"
109
107
print_line " add - add a DNS resolution entry to resolve certain domain names through a particular DNS server"
110
108
print_line " remove - delete a DNS resolution entry; 'del' is an alias"
111
- print_line " flush - remove all DNS resolution entries"
109
+ print_line " purge - remove all DNS resolution entries"
112
110
print_line " print - show all active DNS resolution entries"
113
111
print_line
114
112
print_line "Examples:"
115
- print_line " Set the DNS server to be used for *.metasploit.com to 192.168.1.10"
113
+ print_line " Display all current DNS nameserver entries"
114
+ print_line " dns"
115
+ print_line " dns print"
116
+ print_line
117
+ print_line " Set the DNS server(s) to be used for *.metasploit.com to 192.168.1.10"
116
118
print_line " route add --rule *.metasploit.com 192.168.1.10"
117
119
print_line
118
- print_line " Set the DNS server to be used for *.metasploit.com to 192.168.1.10, but specifically to go through session 2"
120
+ print_line " Add multiple entries at once"
121
+ print_line " route add --rule *.metasploit.com --rule *.google.com 192.168.1.10 192.168.1.11"
122
+ print_line
123
+ print_line " Set the DNS server(s) to be used for *.metasploit.com to 192.168.1.10, but specifically to go through session 2"
119
124
print_line " route add --session 2 --rule *.metasploit.com 192.168.1.10"
120
125
print_line
121
- print_line " Delete the above DNS resolution rule"
122
- print_line " route remove --session 2 --rule *.metasploit.com 192.168.1.10 "
126
+ print_line " Delete the DNS resolution rule with ID 3 "
127
+ print_line " route remove -i 3 "
123
128
print_line
124
- print_line " Set the DNS server to be used for all requests that match no rules"
129
+ print_line " Delete multiple entries in one command"
130
+ print_line " route remove -i 3 -i 4 -i 5"
131
+ print_line
132
+ print_line " Set the DNS server(s) to be used for all requests that match no rules"
125
133
print_line " route add 8.8.8.8 8.8.4.4"
126
134
print_line
127
135
end
@@ -217,7 +225,7 @@ def add_dns(*args)
217
225
servers . each do |server |
218
226
driver . framework . dns_resolver . add_nameserver ( rules , server , comm_obj )
219
227
end
220
- print_good ( "DNS #{ servers . length > 1 ? 'entries' : 'entry' } added" )
228
+ print_good ( "#{ servers . length } DNS #{ servers . length > 1 ? 'entries' : 'entry' } added" )
221
229
end
222
230
223
231
#
@@ -238,7 +246,7 @@ def remove_dns(*args)
238
246
print_warning ( "Some entries were not removed: #{ difference . join ( ', ' ) } " ) unless difference . empty?
239
247
if removed . length > 0
240
248
print_good ( "#{ removed . length } DNS #{ removed . length > 1 ? 'entries' : 'entry' } removed" )
241
- print_dns_set ( 'Deleted entries' , [ 'ID' , 'Rules(s)' , 'DNS Server' , 'Commm channel' ] , removed . map { | hash | [ hash [ :id ] , hash [ :wildcard_rules ] . join ( ',' ) , hash [ :dns_server ] , prettify_comm ( hash [ :comm ] , hash [ :dns_server ] ) ] } )
249
+ print_dns_set ( 'Deleted entries' , removed )
242
250
end
243
251
end
244
252
@@ -256,11 +264,13 @@ def purge_dns
256
264
def print_dns
257
265
results = driver . framework . dns_resolver . nameserver_entries
258
266
columns = [ 'ID' , 'Rule(s)' , 'DNS Server' , 'Comm channel' ]
259
- print_dns_set ( 'Custom nameserver rules' , columns , results [ 0 ] . map { | hash | [ hash [ :id ] , hash [ :wildcard_rules ] . join ( ',' ) , hash [ :dns_server ] , prettify_comm ( hash [ :comm ] , hash [ :dns_server ] ) ] } )
267
+ print_dns_set ( 'Custom nameserver rules' , results [ 0 ] )
260
268
261
269
# Default nameservers don't include a rule
262
270
columns = [ 'ID' , 'DNS Server' , 'Comm channel' ]
263
- print_dns_set ( 'Default nameservers' , columns , results [ 1 ] . map { |hash | [ hash [ :id ] , hash [ :dns_server ] , prettify_comm ( hash [ :comm ] , hash [ :dns_server ] ) ] } )
271
+ print_dns_set ( 'Default nameservers' , results [ 1 ] )
272
+
273
+ print_line ( 'No custom DNS nameserver entries configured' ) if results [ 0 ] . length + results [ 1 ] . length == 0
264
274
end
265
275
266
276
private
@@ -285,16 +295,27 @@ def prettify_comm(comm, dns_server)
285
295
end
286
296
end
287
297
288
- def print_dns_set ( heading , columns , result_set )
298
+ def print_dns_set ( heading , result_set )
299
+ return if result_set . length == 0
300
+ if result_set [ 0 ] [ :wildcard_rules ] . any?
301
+ columns = [ 'ID' , 'Rules(s)' , 'DNS Server' , 'Commm channel' ]
302
+ else
303
+ columns = [ 'ID' , 'DNS Server' , 'Commm channel' ]
304
+ end
305
+
289
306
tbl = Table . new (
290
307
Table ::Style ::Default ,
291
308
'Header' => heading ,
292
309
'Prefix' => "\n " ,
293
310
'Postfix' => "\n " ,
294
311
'Columns' => columns
295
312
)
296
- result_set . each do |row |
297
- tbl << row
313
+ result_set . each do |hash |
314
+ if columns . size == 4
315
+ tbl << [ hash [ :id ] , hash [ :wildcard_rules ] . join ( ',' ) , hash [ :dns_server ] , prettify_comm ( hash [ :comm ] , hash [ :dns_server ] ) ]
316
+ else
317
+ tbl << [ hash [ :id ] , hash [ :dns_server ] , prettify_comm ( hash [ :comm ] , hash [ :dns_server ] ) ]
318
+ end
298
319
end
299
320
300
321
print ( tbl . to_s ) if tbl . rows . length > 0
0 commit comments