Skip to content

Commit da0e728

Browse files
committed
Replace some unnecessary eval action.
Metaprogramming should be reserved for when you don't know things. Here we're making methods from literal strings, so replace the metaprogramming with much easier to understand regular programming. Also has the benefit that yard can parse it.
1 parent 2a3ed89 commit da0e728

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

lib/msf/core/opt.rb

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,73 @@
22

33
module Msf
44

5-
#
6-
# Builtin framework options with shortcut methods
7-
#
8-
module Opt
9-
10-
@@builtin_opts =
11-
{
12-
'RHOST' => [ Msf::OptAddress, 'nil', true, '"The target address"' ],
13-
'RPORT' => [ Msf::OptPort, 'nil', true, '"The target port"' ],
14-
'LHOST' => [ Msf::OptAddress, 'nil', true, '"The listen address"' ],
15-
'LPORT' => [ Msf::OptPort, 'nil', true, '"The listen port"' ],
16-
'CPORT' => [ Msf::OptPort, 'nil', false, '"The local client port"' ],
17-
'CHOST' => [ Msf::OptAddress, 'nil', false, '"The local client address"' ],
18-
'Proxies' => [ Msf::OptString, 'nil', 'false', '"A proxy chain of format type:host:port[,type:host:port][...]"']
19-
}
20-
21-
#
22-
# Build the builtin_xyz methods on the fly using the type information for each
23-
# of the builtin framework options, such as RHOST.
24-
#
25-
class <<self
26-
@@builtin_opts.each_pair { |opt, info|
27-
eval(
28-
"
29-
def builtin_#{opt.downcase}(default = #{info[1]}, required = #{info[2]}, desc = #{info[3]})
30-
#{info[0]}.new('#{opt}', [ required, desc, default ])
31-
end
32-
33-
alias #{opt} builtin_#{opt.downcase}
34-
")
35-
}
36-
end
5+
#
6+
# Builtin framework options with shortcut methods
7+
#
8+
# @example
9+
# register_options(
10+
# [
11+
# Opt::RHOST,
12+
# Opt::RPORT(21),
13+
# ]
14+
# )
15+
# register_advanced_options([Opt::Proxies])
16+
#
17+
module Opt
3718

38-
#
39-
# Define the constant versions of the options which are merely redirections to
40-
# the class methods.
41-
#
42-
@@builtin_opts.each_pair { |opt, info|
43-
eval("#{opt} = Msf::Opt::builtin_#{opt.downcase}")
44-
}
19+
# @return [OptAddress]
20+
def self.CHOST(default=nil, required=false, desc="The local client address")
21+
Msf::OptAddress.new(__method__.to_s, [ required, desc, default ])
22+
end
4523

46-
end
24+
# @return [OptPort]
25+
def self.CPORT(default=nil, required=false, desc="The local client port")
26+
Msf::OptPort.new(__method__.to_s, [ required, desc, default ])
27+
end
28+
29+
# @return [OptAddress]
30+
def self.LHOST(default=nil, required=true, desc="The listen address")
31+
Msf::OptAddress.new(__method__.to_s, [ required, desc, default ])
32+
end
33+
34+
# @return [OptPort]
35+
def self.LPORT(default=nil, required=true, desc="The listen port")
36+
Msf::OptPort.new(__method__.to_s, [ required, desc, default ])
37+
end
38+
39+
# @return [OptString]
40+
def self.Proxies(default=nil, required=false, desc="A proxy chain of format type:host:port[,type:host:port][...]")
41+
Msf::OptString.new(__method__.to_s, [ required, desc, default ])
42+
end
43+
44+
# @return [OptAddress]
45+
def self.RHOST(default=nil, required=true, desc="The target address")
46+
Msf::OptAddress.new(__method__.to_s, [ required, desc, default ])
47+
end
48+
49+
# @return [OptPort]
50+
def self.RPORT(default=nil, required=true, desc="The target port")
51+
Msf::OptPort.new(__method__.to_s, [ required, desc, default ])
52+
end
53+
54+
# These are unused but remain for historical reasons
55+
class << self
56+
alias builtin_chost CHOST
57+
alias builtin_cport CPORT
58+
alias builtin_lhost LHOST
59+
alias builtin_lport LPORT
60+
alias builtin_proxies Proxies
61+
alias builtin_rhost RHOST
62+
alias builtin_rport RPORT
63+
end
64+
65+
CHOST = CHOST()
66+
CPORT = CPORT()
67+
LHOST = LHOST()
68+
LPORT = LPORT()
69+
Proxies = Proxies()
70+
RHOST = RHOST()
71+
RPORT = RPORT()
72+
end
4773

4874
end

0 commit comments

Comments
 (0)