Module: Msf::Exploit::Local::Ansible

Defined in:
lib/msf/core/exploit/local/ansible.rb

Instance Method Summary collapse

Instance Method Details

#ansible_exe(suggestion = ) ⇒ String?

Attempts to find the ansible executable. Verifies the executable is executable by the user as well. Defaults to looking in standard locations for Ubuntu and Docker: ('/usr/local/bin/ansible')

Parameters:

  • suggestion (String) (defaults to: )

    The location of the ansible executable if not in a standard location

Returns:

  • (String, nil)

    The executable location or nil if not found



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/msf/core/exploit/local/ansible.rb', line 72

def ansible_exe(suggestion = datastore['ANSIBLE'])
  return @ansible if @ansible

  [suggestion, '/usr/local/bin/ansible'].each do |exec|
    next if exec.blank?
    next unless executable?(exec)

    @ansible = exec
    return @ansible
  end
  @ansible
end

#ansible_playbook_exe(suggestion = ) ⇒ String?

Attempts to find the ansible-playbook executable. Verifies the executable is executable by the user as well. Defaults to looking in standard locations for Ubuntu and Docker: ('/usr/local/bin/ansible-playbook', '/usr/bin/ansible-playbook')

Parameters:

  • suggestion (String) (defaults to: )

    The location of the ansible-playbook executable if not in a standard location

Returns:

  • (String, nil)

    The executable location or nil if not found



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/msf/core/exploit/local/ansible.rb', line 49

def ansible_playbook_exe(suggestion = datastore['ANSIBLEPLAYBOOK'])
  return @ansible_playbook if @ansible_playbook

  [suggestion, '/usr/local/bin/ansible-playbook', '/usr/bin/ansible-playbook'].each do |exec|
    next if exec.blank?
    next unless executable?(exec)

    @ansible_playbook = exec
    return @ansible_playbook
  end
  @ansible_playbook
end

#initialize(info = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/msf/core/exploit/local/ansible.rb', line 5

def initialize(info = {})
  super

  register_advanced_options([
    Msf::OptString.new('ANSIBLE', [false, 'Ansible executable location', '']),
    Msf::OptString.new('ANSIBLEPLAYBOOK', [false, 'Ansible-playbook executable location', '']),
  ])
end

#ping_hosts(hosts = 'all') ⇒ Array?

Uses the ansible command to ping hosts, returns an array of hashes

Parameters:

  • ansible_exe (String)

    The name location of the ansible executable

  • hosts (String) (defaults to: 'all')

    The host string to use, defaults to 'all'

Returns:

  • (Array, nil)

    containing a hash for each host. Each has consists of the following parameters: host, status, ping, changed. nil on error.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/msf/core/exploit/local/ansible.rb', line 23

def ping_hosts(hosts = 'all')
  results = cmd_exec("#{ansible_exe} #{hosts} -m ping -o")
  # here's a regex with test: https://rubular.com/r/FMHhWx8QlVnidA
  regex = /(\S+)\s+\|\s+([A-Z]+)\s+=>\s+({.+})$/
  matches = results.scan(regex)

  hosts = []
  matches.each do |match|
    match[2] = JSON.parse(match[2])
    hosts << { 'host' => match[0], 'status' => match[1], 'ping' => match[2]['ping'], 'changed' => match[2]['changed'] }
  rescue JSON::ParserError
    return nil
  end
  hosts
end