Skip to content

Commit 722f86f

Browse files
jvazquez-r7Brent Cook
authored andcommitted
Try to guess TMPDIR folder
1 parent 7596d21 commit 722f86f

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/rex/post/meterpreter/client_core.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def use(mod, opts = { })
183183
# Migrates the meterpreter instance to the process specified
184184
# by pid. The connection to the server remains established.
185185
#
186-
def migrate(pid, writable_dir="/tmp/")
186+
def migrate(pid, writable_dir = nil)
187187
keepalive = client.send_keepalives
188188
client.send_keepalives = false
189189
process = nil
@@ -207,20 +207,22 @@ def migrate(pid, writable_dir="/tmp/")
207207
raise RuntimeError, "Cannot migrate into non existent process", caller
208208
end
209209

210-
# We can't migrate into a process that we are unable to open
210+
# We cannot migrate into a process that we are unable to open
211211
# On linux, arch is empty even if we can access the process
212-
if client.platform =~ /win/ && (process['arch'] == nil || process['arch'].empty?)
213-
raise RuntimeError, "Cannot migrate into this process (insufficient privileges)", caller
212+
if client.platform =~ /win/
213+
if process['arch'] == nil || process['arch'].empty?
214+
raise RuntimeError, "Cannot migrate into this process (insufficient privileges)", caller
215+
end
214216
end
215217

216-
# And we also cant migrate into our own current process...
218+
# And we also cannot migrate into our own current process...
217219
if process['pid'] == client.sys.process.getpid
218220
raise RuntimeError, "Cannot migrate into current process", caller
219221
end
220222

221223
if client.platform =~ /linux/
222224
if writable_dir.blank?
223-
writable_dir = "/tmp/"
225+
writable_dir = tmp_folder
224226
end
225227

226228
stat_dir = client.fs.filestat.new(writable_dir)
@@ -231,7 +233,7 @@ def migrate(pid, writable_dir="/tmp/")
231233
# Rex::Post::FileStat#writable? isn't available
232234
end
233235

234-
blob = generate_payload_stub(client, process)
236+
blob = generate_payload_stub(process)
235237

236238
# Build the migration request
237239
request = Packet.create_request( 'core_migrate' )
@@ -361,10 +363,10 @@ def shutdown
361363

362364
private
363365

364-
def generate_payload_stub(client, process)
366+
def generate_payload_stub(process)
365367
case client.platform
366368
when /win/i
367-
blob = generate_windows_stub(client, process)
369+
blob = generate_windows_stub(process)
368370
when /linux/i
369371
blob = generate_linux_stub
370372
else
@@ -374,7 +376,7 @@ def generate_payload_stub(client, process)
374376
blob
375377
end
376378

377-
def generate_windows_stub(client, process)
379+
def generate_windows_stub(process)
378380
c = Class.new( ::Msf::Payload )
379381
c.include( ::Msf::Payload::Stager )
380382

@@ -437,6 +439,16 @@ def elf_ep(payload)
437439
return ep
438440
end
439441

442+
def tmp_folder
443+
tmp = client.sys.config.getenv('TMPDIR')
444+
445+
if tmp.blank?
446+
tmp = '/tmp'
447+
end
448+
449+
tmp
450+
end
451+
440452
end
441453

442454
end; end; end

lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def cmd_migrate(*args)
351351
end
352352

353353
if client.platform =~ /linux/
354-
writable_dir = (args.length >= 2) ? args[1] : "/tmp/"
354+
writable_dir = (args.length >= 2) ? args[1] : nil
355355
end
356356

357357
begin

0 commit comments

Comments
 (0)