Skip to content

PSExec Classic Module #2001

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

Closed
wants to merge 14 commits into from
Closed

PSExec Classic Module #2001

wants to merge 14 commits into from

Conversation

jtesta
Copy link
Contributor

@jtesta jtesta commented Jun 21, 2013

This module mimics the authentic PsExec program from Microsoft Sysinternals. It extracts the PSEXESVC.EXE code from the authentic PsExec.exe program, sends it to the target, then interacts with it just as PsExec does. The main reasons this module is useful are:

1.) modules/exploits/windows/smb/psexec tends to get caught by AV. This has a better chance of working because PSEXESVC.EXE has a Microsoft signature; blocking it would block Microsoft's PsExec too.

2.) Unlike the authentic PsExec, this lets us take advantage of pass-the-hash, along with running on platforms other than Windows.

3.) Its very convenient for executing interactive processes (like cmd.exe).

At this time, this module does not adhere to the Metasploit's HACKING guide because it reads user commands from $stdin. I tried asking in the IRC channel for pointers on how to fix this, but no one had any suggestions. I'd be happy to revise this module and re-submit once I have sample code to work with!

@wvu
Copy link
Contributor

wvu commented Jun 21, 2013

Hey, @jtesta. Could you please fix the msftidy warnings? Thanks.

@kernelsmith
Copy link
Contributor

@jtesta I might have some ideas for the stdin problem. I'll look at the code, and try to catch me IRC as kernelsmith. I haven't been on there much recently but I am more available now

@jvazquez-r7
Copy link
Contributor

@jtesta , @kernelsmith please feel free to update us if you were able to discuss the problem? thanks!

@kernelsmith
Copy link
Contributor

I'm in the middle of switching to a new development machine so I haven't had a chance to look at it yet

@jtesta
Copy link
Contributor Author

jtesta commented Jun 26, 2013

I ran mfstidy and fixed all the warnings. I'll drop by the IRC channel and we can work out the $stdin issue. Or, if its easier, I'd be happy to work on it myself if someone were to point me to some example code to use as a reference.

@kernelsmith
Copy link
Contributor

I haven't checked out your code yet, but the grep command in msfconsole might help as it redirects console output temporarily. To find it just grep for cmd_grep

I'll be on IRC in the afternoon and evening US time

-Josh

On Jun 25, 2013, at 19:11, jtesta [email protected] wrote:

I ran mfstidy and fixed all the warnings. I'll drop by the IRC channel and we can work out the $stdin issue. Or, if its easier, I'd be happy to work on it myself if someone were to point me to some example code to use as a reference.


Reply to this email directly or view it on GitHub.

@limhoff-r7
Copy link
Contributor

@jtesta
Copy link
Contributor Author

jtesta commented Jun 26, 2013

I took a look at cmd_grep as suggested, but I'm not sure what to do next. Is the idea to somehow read input using the Msf::Ui::Console::Driver object? I don't know how to obtain a reference to that object, either.

@jlee-r7
Copy link
Contributor

jlee-r7 commented Jun 27, 2013

Regardless of its other merits, this simply can't be merged with the massive amount of copy-paste from modules/exploits/windows/smb/psexec.rb. The Smb::Psexec mixin was created just for this reason.

@kernelsmith
Copy link
Contributor

@jtesta yeah, you'll definitely have to switch to using the Smb::Psexec mixin, but I'm looking now at your stdin issue. My dev env isn't quite back up and running yet, but I can still look at the code. I'll add comments if I see anything.

@kernelsmith
Copy link
Contributor

I would also recommend pulling the psexecsvc stuff out into a mixin. There's some redundant code that could be refactored, and having a module would allow others to easily re-use your code as well as it seems you did some rev engr of the psexec svc and it would be great to have it available to others. I'm not sure of the namespace, but maybe Smb::PsexecSvc or does it belong with DCERPC? I'll look at the code again on that, but it would be very cool if someone could call something like send_dash_s to send the bytes corresponding to the -s switch, and you could have send_magic_ping etc. The whole thing would become much more readable. I will start a gist we can use to discuss the idea if it interests you: https://gist.github.com/kernelsmith/5874810. We can look at the stdin issue that way as well. That issue looks a little more complicated than I was originally thinking it might be. Ideally we could reuse existing code like a multi-handler.

@jtesta
Copy link
Contributor Author

jtesta commented Jun 27, 2013

@jlee-r7: do you mean split off the MSRPC calls and put them into lib/msf/core/exploit/smb/psexec.rb? I originally wanted to do that, but I figured that for my first module, I should refrain from making architectural changes. Also note that the "psexec" function in that file would need to be split up too, since it makes the same MSRPC calls. I don't have a problem doing and testing this now... I just want to make sure we're on the same page.

@kernelsmith
Copy link
Contributor

If they are generic enough I would create a new file for the msrpc methods. They shouldn't be specific to psexec and they technically aren't SMB right? @jlee-r7 thoughts?

-Josh

On Jun 27, 2013, at 14:00, jtesta [email protected] wrote:

@jlee-r7: do you mean split off the MSRPC calls and put them into lib/msf/core/exploit/smb/psexec.rb? I originally wanted to do that, but I figured that for my first module, I should refrain from making architectural changes. Also note that the "psexec" function in that file would need to be split up too, since it makes the same MSRPC calls. I don't have a problem doing and testing this now... I just want to make sure we're on the same page.


Reply to this email directly or view it on GitHub.

@jtesta
Copy link
Contributor Author

jtesta commented Jun 27, 2013

@kernelsmith: Regarding splitting code into Smb::PsexecSvc: sure, we can do that.

About the $stdin issue: it would be great if we could make a session background-able, kinda like Meterpreter sessions That would let someone run multiple cmd.exe's (or whatever) across many machines at the same time. No idea how to do this, though...

@kernelsmith
Copy link
Contributor

I think we might need to look at multi/handler to see if we can bridge into that cuz we'd like to make it into a real session so it can be handled like any other session which would give it the functionality u mention

-Josh

On Jun 27, 2013, at 14:27, jtesta [email protected] wrote:

@kernelsmith: Regarding splitting code into Smb::PsexecSvc: sure, we can do that.

About the $stdin issue: it would be great if we could make a session background-able, kinda like Meterpreter sessions That would let someone run multiple cmd.exe's (or whatever) across many machines at the same time. No idea how to do this, though...


Reply to this email directly or view it on GitHub.

@wvu
Copy link
Contributor

wvu commented Jul 11, 2013

So, what do you want to do with this PR, @jtesta?

@jtesta
Copy link
Contributor Author

jtesta commented Jul 11, 2013

No one has pointed me to working example code that substitutes for reading from $stdin, so I can't fix that. However, I can split off the MSRPC calls into a new into lib/msf/core/exploit/smb/psexec.rb file. I was waiting for someone to say this was OK, but I suppose I'll just go ahead and do it. I'll have it ready in a few days!

@Meatballs1
Copy link
Contributor

The psexec library already exists!

…reated new Exploit::Remote::SMB::PsexecSvc mixin as well.
@jtesta
Copy link
Contributor Author

jtesta commented Jul 15, 2013

I've split the MSRPC service calls into its own module. If this PR is accepted, we can also modify modules/exploits/windows/smb/psexec.rb, lib/msf/core/exploit/smb/psexec.rb, and modules/auxiliary/scanner/smb/psexec_loggedin_users.rb to use these new functions to cut out duplicate code from the codebase.

tabassassin added a commit to tabassassin/metasploit-framework that referenced this pull request Sep 5, 2013
@wvu
Copy link
Contributor

wvu commented Oct 10, 2013

@jtesta: Ping.

@jtesta
Copy link
Contributor Author

jtesta commented Oct 10, 2013

Pong. Sorry for going MIA for so long. I'll update my code within the
next few days.

On 10/10/2013 03:27 PM, wvu-r7 wrote:

@jtesta https://github.com/jtesta: Ping.


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

@wvu
Copy link
Contributor

wvu commented Jan 2, 2014

@jtesta: How's it going?

@jtesta
Copy link
Contributor Author

jtesta commented Jan 2, 2014

Its going well! How are you?

I suppose this is my ping to get moving on my PR, huh? I'll get this
going again. I noticed Microsoft released a new version of PsExec.exe,
so I'll have to test with it and possibly update the code. I'll try to
get this started today!

On 01/02/2014 12:43 AM, wvu-r7 wrote:

@jtesta https://github.com/jtesta: How's it going?


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

@wvu
Copy link
Contributor

wvu commented Jan 2, 2014

No worries, mate. There are conflicts, btw. Thanks!

Conflicts:
	lib/msf/core/exploit/dcerpc.rb
	lib/rex/proto/smb/client.rb
@Meatballs1
Copy link
Contributor

Have you tried vprint_status? It should be picked up in Msf:: mixins afaik as datastore['VERBOSE'] is a hidden option thingy!

@Meatballs1
Copy link
Contributor

Also dcerpc_services could probably be pushed into Rex::Proto::Dcerpc rather than Msf library as it doesn't have any Msf dependencies?

@Meatballs1
Copy link
Contributor

This isn't working for me in Windows 7 at the moment:

[] Creating a new service (PSEXECSVC - "PsExec")...
[
] Opening service...
[] Starting the service...
[
] Connecting to \PSEXESVC pipe...
[*] Instructing service to execute cmd.exe /c ipconfig...
[-] Failed to connect to \PSEXESVC-TkynAfPyqrRR-36030-stdin!
[-] Failed to connect to \PSEXESVC-TkynAfPyqrRR-36030-stdout!
[-] Failed to connect to \PSEXESVC-TkynAfPyqrRR-36030-stderr!
[-] Auxiliary failed: NoMethodError undefined method `file_id' for nil:NilClass

I can see the Named Pipes exist in Process Explorer, but the 'Create AndX Request' for those Paths gives me a STATUS_ACCESS_DENIED

PSExec works with same credentials over both SMBv1 and SMBv2

@jtesta
Copy link
Contributor Author

jtesta commented Jan 16, 2014

Hmm! Interesting. I'll take a closer look next chance I get. Thanks
for the feedback!

On 01/16/2014 12:15 PM, Meatballs1 wrote:

This isn't working for me in Windows 7 at the moment:

[] Creating a new service (PSEXECSVC - "PsExec")...
[
] Opening service...
[] Starting the service...
[
] Connecting to \PSEXESVC pipe...
[*] Instructing service to execute cmd.exe /c ipconfig...
[-] Failed to connect to \PSEXESVC-TkynAfPyqrRR-36030-stdin!
[-] Failed to connect to \PSEXESVC-TkynAfPyqrRR-36030-stdout!
[-] Failed to connect to \PSEXESVC-TkynAfPyqrRR-36030-stderr!
[-] Auxiliary failed: NoMethodError undefined method `file_id' for
nil:NilClass

I can see the Named Pipes exist in Process Explorer, but the 'Create
AndX Request' for those Paths gives me a STATUS_ACCESS_DENIED

PSExec works with same credentials over both SMBv1 and SMBv2


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

@Meatballs1 Meatballs1 mentioned this pull request Apr 5, 2014
9 tasks
@jlee-r7
Copy link
Contributor

jlee-r7 commented May 21, 2014

Is there any way to pull out the necessary stuff generically without having to store a hash of supported files? This is going to be a maintenance nightmare.

…-server communication. The changes to the code don't look too signficant, but don't let that fool you; getting OpenSSL to play nice with Microsoft's CryptoAPI was HARD. Many, many hours of bit-twiddling were spent.
@jtesta
Copy link
Contributor Author

jtesta commented Jun 26, 2014

@jlee-r7: I know of no way to generically pull the version info out. As you can see from my latest update, there can be very significant changes in the way PSEXESVC.exe works, necessitating major client updates.

Its going to be a maintenance nightmare either way. That's ok, though... I volunteer to be the maintainer for the foreseeable future.

It seems like this PR is dependent upon getting #3144 accepted first. Anyone know the status of it?

@hdm
Copy link
Contributor

hdm commented Dec 12, 2014

Looks like #3144 is now closed, this is good for next steps, whatever those may be.

@jtesta
Copy link
Contributor Author

jtesta commented Dec 12, 2014

There's a small quirk that prevents it from working on Windows 8, so I
need to put in a little more work. I was hoping to get to that soon,
actually...

On 12/12/2014 02:51 PM, HD Moore wrote:

Looks like #3144
#3144 is now
closed, this is good for next steps, whatever those may be.


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

@bcook-r7
Copy link
Contributor

Now that the psexec refactor is complete, let's look at landing this since it doesn't get caught by AV.

@todb-r7 todb-r7 self-assigned this Jan 21, 2015
@jtesta
Copy link
Contributor Author

jtesta commented Jan 21, 2015

I keep saying on here (and to myself) that I'll fix it up "soon", but
work keeps popping up. If you or anyone else would like to pick up
where I left off, please do!

Otherwise, I'm now looking at getting to it in early February.

On 01/21/2015 04:26 PM, Brent Cook wrote:

Now that the psexec refactor is complete, let's look at landing this
since it doesn't get caught by AV.


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

@scriptjunkie
Copy link
Contributor

jtesta, I don't know if you ever reached a conclusion on stdin, but I would look at subclassing Msf::Sessions::CommandShell for your session type. Implement read and write with that.

A different but hacky way to do it would be to simply spawn off a generic reverse shell handler listening on localhost and create a socket to connect to it (like Rex's tcp_socket_pair), and spawn a couple threads to forward communications from the remote psexec to your socket and back.

But either would be better than listening on stdin, both for everybody who uses other frontends to Metasploit and the ability to background sessions, etc.

@todb-r7
Copy link

todb-r7 commented Feb 13, 2015

Hi @jtesta. First, congrats for having the oldest open PR on Metasploit, now.

Sadly, I'm not able to get your module to function. Compare:

psexec_command

[*] 192.168.145.129:445 - Executing the command...
[*] 192.168.145.129:445 - Binding to 367abb81-9844-35f1-ad32-98f038001003:2.0@ncacn_np:192.168.145.129[\svcctl] ...
[*] 192.168.145.129:445 - Bound to 367abb81-9844-35f1-ad32-98f038001003:2.0@ncacn_np:192.168.145.129[\svcctl] ...
[*] 192.168.145.129:445 - Obtaining a service manager handle...
[*] 192.168.145.129:445 - Creating the service...
[+] 192.168.145.129:445 - Successfully created the service
[*] 192.168.145.129:445 - Starting the service...
[+] 192.168.145.129:445 - Service start timed out, OK if running a command or non-service executable...
[*] 192.168.145.129:445 - Removing the service...
[+] 192.168.145.129:445 - Successfully removed the sevice
[*] 192.168.145.129:445 - Closing service handle...
[*] checking if the file is unlocked
[*] 192.168.145.129:445 - Unable to get handle: The server responded with error: STATUS_SHARING_VIOLATION (Command=45 WordCount=0)
[-] Command seems to still be executing. Try increasing RETRY and DELAY
[*] 192.168.145.129:445 - Getting the command output...
[+] 192.168.145.129:445 - Command completed successfuly!
[*] Output for "cmd.exe":
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>
[*] 192.168.145.129:445 - Executing cleanup...
[-] 192.168.145.129:445 - Unable to cleanup \WINDOWS\Temp\mTmCuMdLenAVenwb.txt. Error: The server responded with error: STATUS_SHARING_VIOLATION (Command=6 WordCount=0)
[-] 192.168.145.129:445 - Unable to cleanup. Maybe you'll need to manually remove true, false from the target.
[*] Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed
msf auxiliary(psexec_command) > 

psexec_classic

msf auxiliary(psexec_classic) > run

[*] Calculating SHA-256 hash of /home/todb/PSTools/PsExec.exe...
[*] File hash verified.  PsExec v2.11 detected.  Extracting PSEXESVC.EXE code from /home/todb/PSTools/PsExec.exe...
[*] Connecting to 192.168.145.129...
[*] Authenticating to 192.168.145.129:445|WORKGROUP as user 'msfadmin'...
[*] Uploading PSEXESVC.EXE...
[*] Created \PSEXESVC.EXE in ADMIN$ share.
[*] Connecting to IPC$...
[*] Binding to DCERPC handle 367abb81-9844-35f1-ad32-98f038001003:2.0@ncacn_np:192.168.145.129[\svcctl]...
[*] Successfully bound to 367abb81-9844-35f1-ad32-98f038001003:2.0@ncacn_np:192.168.145.129[\svcctl] ...
[*] Obtaining a service control manager handle...
[*] Creating a new service (PSEXECSVC - "PsExec")...
[*] Opening service...
[*] Starting the service...
[*] Connecting to \PSEXESVC pipe...
[*] Instructing service to execute cmd.exe...
[-] Failed to connect to \PSEXESVC-UHVyQqhxunNh-9913-stdin!
[-] Failed to connect to \PSEXESVC-UHVyQqhxunNh-9913-stdout!
[-] Failed to connect to \PSEXESVC-UHVyQqhxunNh-9913-stderr!
[-] Auxiliary failed: NoMethodError undefined method `file_id' for nil:NilClass
[-] Call stack:
[-]   /home/todb/git/rapid7/metasploit-framework/modules/auxiliary/admin/smb/psexec_classic.rb:405:in `run'
[*] Auxiliary module execution completed
msf auxiliary(psexec_classic) > show options

Module options (auxiliary/admin/smb/psexec_classic):

   Name         Current Setting                Required  Description
   ----         ---------------                --------  -----------
   COMMAND      cmd.exe                        yes       The program to execute with SYSTEM privileges.
   PSEXEC_PATH  /home/todb/PSTools/PsExec.exe  yes       The local path to the authentic PsExec.exe
   RHOST        192.168.145.129                yes       The target address
   RPORT        445                            yes       Set the SMB service port
   SMBDomain    WORKGROUP                      no        The Windows domain to use for authentication
   SMBPass      msfadmin                       no        The password for the specified username
   SMBUser      msfadmin                       no        The username to authenticate as

msf auxiliary(psexec_classic) > 

This is using the unmodified version of your module. I have a PR to normalize some of the Ruby, too, with the same results.

Care to check it out? I love the idea and want it to work. What can I do to help this along?

Target is Windows 7 Professional, SP1, pretty recent patch set.

@todb-r7
Copy link

todb-r7 commented Feb 13, 2015

Note, the library Ruby will also likely need to be reviewed, too, and there are other not-very-Ruby idioms in this PR. I don't mind fixing them up once we hit a reasonable working state, though.

@todb-r7 todb-r7 added the blocked Blocked by one or more additional tasks label Feb 13, 2015
@Meatballs1
Copy link
Contributor

@todb-r7 the library code has already been tidied up and landed in #3144. This PR needs rebasing (and fixing up) against the latest master

@todb-r7
Copy link

todb-r7 commented Feb 17, 2015

@Meatballs1 it's not like there were merge conflicts -- I tried this PR both in isolation and in a test merge, and the module appears non-functional on Windows 7, where regular psexec_command is fine.

IOW. rebasing doesn't appear to be able to solve the functionality or the non-Ruby styles. Do you disagree?

@Meatballs1
Copy link
Contributor

I agree, the last time I tested this it didn't work for me either. :)

@todb-r7
Copy link

todb-r7 commented Feb 18, 2015

Since neither @Meatballs1 nor I were able to get this module to function, let's close out the PR. @jtesta when you have something functional feel free to open a new one, or bug me to reopen this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked by one or more additional tasks feature library module
Projects
None yet
Development

Successfully merging this pull request may close these issues.