@@ -41,19 +41,19 @@ def commands
4141 # Tab completion for the dns command
4242 #
4343 # @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
4646 def cmd_dns_tabs ( str , words )
4747 return if driver . framework . dns_resolver . nil?
4848
4949 if words . length == 1
50- options = [ 'add' , 'del' , 'remove' , 'flush ' , 'print' ]
50+ options = [ 'add' , 'del' , 'remove' , 'purge ' , 'print' ]
5151 return options . select { |opt | opt . start_with? ( str ) }
5252 end
5353
5454 cmd = words [ 1 ]
5555 case cmd
56- when 'flush ' , 'print'
56+ when 'purge ' , 'print'
5757 # These commands don't have any arguments
5858 return
5959 when 'add'
@@ -95,33 +95,41 @@ def cmd_dns_tabs(str, words)
9595 end
9696
9797 def cmd_dns_help
98- print_line 'Usage: dns'
99- print_line
10098 print_line "Manage Metasploit's DNS resolution behaviour"
10199 print_line
102100 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 ]"
106104 print_line " dns [print]"
107105 print_line
108106 print_line "Subcommands:"
109107 print_line " add - add a DNS resolution entry to resolve certain domain names through a particular DNS server"
110108 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"
112110 print_line " print - show all active DNS resolution entries"
113111 print_line
114112 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"
116118 print_line " route add --rule *.metasploit.com 192.168.1.10"
117119 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"
119124 print_line " route add --session 2 --rule *.metasploit.com 192.168.1.10"
120125 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 "
123128 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"
125133 print_line " route add 8.8.8.8 8.8.4.4"
126134 print_line
127135 end
@@ -217,7 +225,7 @@ def add_dns(*args)
217225 servers . each do |server |
218226 driver . framework . dns_resolver . add_nameserver ( rules , server , comm_obj )
219227 end
220- print_good ( "DNS #{ servers . length > 1 ? 'entries' : 'entry' } added" )
228+ print_good ( "#{ servers . length } DNS #{ servers . length > 1 ? 'entries' : 'entry' } added" )
221229 end
222230
223231 #
@@ -238,7 +246,7 @@ def remove_dns(*args)
238246 print_warning ( "Some entries were not removed: #{ difference . join ( ', ' ) } " ) unless difference . empty?
239247 if removed . length > 0
240248 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 )
242250 end
243251 end
244252
@@ -256,11 +264,13 @@ def purge_dns
256264 def print_dns
257265 results = driver . framework . dns_resolver . nameserver_entries
258266 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 ] )
260268
261269 # Default nameservers don't include a rule
262270 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
264274 end
265275
266276 private
@@ -285,16 +295,27 @@ def prettify_comm(comm, dns_server)
285295 end
286296 end
287297
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+
289306 tbl = Table . new (
290307 Table ::Style ::Default ,
291308 'Header' => heading ,
292309 'Prefix' => "\n " ,
293310 'Postfix' => "\n " ,
294311 'Columns' => columns
295312 )
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
298319 end
299320
300321 print ( tbl . to_s ) if tbl . rows . length > 0
0 commit comments