Win32-OpenSSH icon indicating copy to clipboard operation
Win32-OpenSSH copied to clipboard

OpenSSH requires be run as service to work - fork of unprivileged child failed

Open illera88 opened this issue 7 years ago • 28 comments

Please answer the following

"OpenSSH for Windows" version OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.4

Server OperatingSystem Windows 10 Enterprise

Client OperatingSystem Windows 10 Enterprise

What is failing I cannot get a shell after I successfully log in.

I'm debugging within VS libsshd to understand how it works. I got it listening to a port (not 22 since that requires admin privileges) and the problem is that when I introduce my user and password I automatically get disconnected. In the log I can see:

Server listening on :: port 2233.
Server listening on 0.0.0.0 port 2233.
Could not load host key: __PROGRAMDATA__\\ssh/ssh_host_ecdsa_key
Could not load host key: __PROGRAMDATA__\\ssh/ssh_host_ed25519_key
Failed password for user from ::1 port 3123 ssh2
Accepted password for user from ::1 port 3123 ssh2
CreateProcessAsUserW failed error:1314
fork of unprivileged child failed

I've tried with my built sshd and with the release one and I get the same thing.

Btw: how can I get full debug messages to get print out to stdout?

illera88 avatar Dec 10 '18 23:12 illera88

Debugging When Running As A Service

So here's what I do for debugging as a service. Running as a service can result in different behavior compared to running sshd as an interactive user.

  1. Install SSHD using the normal instructions.
  2. Set the following in C:\ProgramData\ssh\sshd_config to enable verbose logging:
    SyslogFacility LOCAL0
    LogLevel DEBUG3
    
  3. Install Microsoft Child Process Debugging Power Tool for Visual Studio. This allows the debugger to pickup any child processes.
  4. Launch Visual Studio as Administrator.
  5. Activate the aforementioned plugin by going to Debug :arrow_right: Other Debug Targets :arrow_right: Child Process Debugging Settings :arrow_right: Check 'Enable child process debugging' :arrow_right: Click 'Save'.
  6. Replace the executable(s) in 'C:\Program Files\OpenSSH' with your development executables (or make symbolic links for the relevant executables that point to your Debug directory:
    C:\Program Files\OpenSSH>del sshd.exe
    
    C:\Program Files\OpenSSH>MKLINK sshd.exe C:\openssh-portable\bin\x64\Debug\sshd.exe
    symbolic link created for sshd.exe <<===>> C:\openssh-portable\bin\x64\Debug\sshd.exe
    
  7. Start the 'OpenSSH SSH Server' service.
  8. Startup PowerShell as Administrator and tail the sshd log file. This will display messages as they are added to the log file:
    PS C:\> Get-Content -Path C:\ProgramData\ssh\Logs\sshd.log -Wait -Tail 0
    
  9. Attach to the sshd.exe process using Debug :arrow_right: Attach to Process....
  10. Place breakpoints where desired.
  11. Login using ssh (or another SSH client).

NoMoreFood avatar Dec 11 '18 00:12 NoMoreFood

Those are great instructions!

Trying right now. I'll come back to the results to see if we can find the root cause

illera88 avatar Dec 11 '18 00:12 illera88

I did what you said and it seems that there is no log files created at C:\ProgramData\ssh\Logs\sshd.log any reason why that could be?

thanks

illera88 avatar Dec 11 '18 00:12 illera88

Hmmm... that should be it. Did you start sshd as service? I’ll check my sshd_config against the default when I get back to my computer. I suppose step 2 should technically be after the service starts to get the file in place.

NoMoreFood avatar Dec 11 '18 00:12 NoMoreFood

No, I did not start it as a service since I want to start it from within the debugger (instead of attaching to it) and I want to create a ssh server that does not require privileges (running on port > 1000)

illera88 avatar Dec 11 '18 00:12 illera88

I think I got close from where the bug happens: It is at spawn_child_internal(char* cmd, char *const argv[], HANDLE in, HANDLE out, HANDLE err, unsigned long flags, HANDLE* as_user, BOOLEAN prepend_module_path) function and the cmd argument instead of being cmd.exe or powershell.exe it is sshd.exe and argv is -z.

Don't understand really why that is happening.

For clarification this happens after successful authentication (that's why I think the process that should be executed there is cmd.exe or powershell.exe .

Ideas?

illera88 avatar Dec 11 '18 01:12 illera88

Ok, my bad. My write up is for debugging as a service. I’ll update them for running as as a regular user in a bit - much of the same should apply. I usually run as a service.

NoMoreFood avatar Dec 11 '18 01:12 NoMoreFood

I've kept digging and I see that in load_user_profile checks if I'm SYSTEM (have run the process as a service):

int 
load_user_profile(HANDLE user_token, char* user)
{
	wchar_t * user_utf16 = NULL;

	if (!am_system()) {
	    debug("Not running as SYSTEM: skipping loading user profile");
	    return 0;
	}

As I said I don't and I need it to run in user land. Do you think that could be causing the problem?

illera88 avatar Dec 11 '18 01:12 illera88

Indeed I tried to run sshd as a service and:

  1. If I use the release binaries provided in this repo it does works perfect.
  2. If I use the debug binaries I did create with VS it does not (I guess this needs further debugging)

In any case it seems pretty obvious that sshd cannot be used if it is not run as a service and it may be related with load_user_profile not passing the if (!am_system()) { condition.

Is the requirement of running sshd as a system a known issue?

Thank you

illera88 avatar Dec 11 '18 02:12 illera88

I know your intent is to not run as a system user, but is it also your intent to run an unprivileged user?

NoMoreFood avatar Dec 11 '18 08:12 NoMoreFood

Yes, it should not be necesary to run it as admin as long as the it listens in a port > 1000

On Tue, Dec 11, 2018 at 12:29 AM Bryan Berns [email protected] wrote:

I know your intent is to not run as a system user, but is it also your intent to run an unprivileged user?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/Win32-OpenSSH/issues/1304#issuecomment-446114371, or mute the thread https://github.com/notifications/unsubscribe-auth/AE89qHfYeioSHfSrTimXuQVYDX42fxDWks5u32zlgaJpZM4ZMZON .

-- Alberto García Illera

GPG Public Key https://goo.gl/yshdwh

illera88 avatar Dec 11 '18 08:12 illera88

Alright, I'm not familiar with this use case and have not tested it myself. I'll give it a test today. I do know that public key authentication (into a different user account) is not going to work because it relies on privileges that a normal user does not have. Password-based authentication can technically work at the OS level assuming Secondary Logon is enabled (it should be by default).

NoMoreFood avatar Dec 11 '18 08:12 NoMoreFood

So I did a quick debug from and the problem is indeed to the fact that CreateProcessAsUserW() failed, as indicated by your session output. This is likely for two reasons: 1) The token returned from LogonUserExExW() probably should be a primary token and will never be since LOGON32_LOGON_NETWORK_CLEARTEXT is being used as the logon type, and 2) when using CreateProcessAsUserW(), additional privileges are needed to assign the token to the new process (this is what the 1314 error indicates). In order to run in a truly unprivileged mode, I suspect that CreateProcessWithLogonW() would have to be used instead of the LogonUserExExW()/LogonUserExExW() combo.

I haven't familiarized enough with the functions that call these to confidently infer why it was designed this way; somebody else would have to speak to that.

NoMoreFood avatar Dec 11 '18 11:12 NoMoreFood

Thank you for your time on debugging this issue. Do you know someone that may know about the internals of the mentioned APIs so we can mention him to take a look at this?

thanks again

illera88 avatar Dec 11 '18 20:12 illera88

IIRC the trouble with CreateProcessWithLogonW() is that it creates a process with an interactive logon type whereas normally an ssh spawned logon would have a network cleartext logon type for password based auth. The other option is CreateProcessWithToken() but that need the SeImpersonatePrivilege which isn't assigned to standard accounts.

I'm curious as to why you want to run this as a non privileged account considering ssh key auth won't ever work.

jborean93 avatar Dec 12 '18 04:12 jborean93

I do not need ssh key to work. Just to have a proper portable ssh server they I can drop to listen in any >1024port

On Tue, Dec 11, 2018 at 8:03 PM Jordan Borean [email protected] wrote:

IIRC the trouble with CreateProcessWithLogonW() is that it creates a process with an interactive logon type whereas normally an ssh spawned logon would have a network cleartext logon type for password based auth. The other option is CreateProcessWithToken() but that need the SeImpersonatePrivilege which isn't assigned to standard accounts.

I'm curious as to why you want to run this as a non privileged account considering ssh key auth won't ever work.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/Win32-OpenSSH/issues/1304#issuecomment-446454432, or mute the thread https://github.com/notifications/unsubscribe-auth/AE89qJPMzMyWjnvaS5gPsVG2t22pWwVBks5u4IAkgaJpZM4ZMZON .

-- Alberto García Illera

GPG Public Key https://goo.gl/yshdwh

illera88 avatar Dec 12 '18 08:12 illera88

It's not my place to say whether CreateProcessWithLogonW() is an alternative as this isn't my project. Just merely stating the facts as to how moving to that will result in a behaviour change where arguably an interactive logon from a network source isn't really correct. I'm sure there will be other issues that would pop up if this ever changed to support this scenario as logon and process creation is just the tip of the iceberg.

jborean93 avatar Dec 12 '18 08:12 jborean93

Do you know anyone we can ask about it?

Thank you Jordan.

On Wed, Dec 12, 2018 at 12:39 AM Jordan Borean [email protected] wrote:

It's not my place to say whether CreateProcessWithLogonW() is an alternative as this isn't my project. Just merely stating the facts as to how moving to that will result in a behaviour change where arguably an interactive logon from a network source isn't really correct. I'm sure there will be other issues that would pop up if this ever starts working as the logon and process creation is just the tip of the iceberg.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/Win32-OpenSSH/issues/1304#issuecomment-446506136, or mute the thread https://github.com/notifications/unsubscribe-auth/AE89qFRgARcpTJ_101S6RZQOtqrMWR7eks5u4MC8gaJpZM4ZMZON .

-- Alberto García Illera

GPG Public Key https://goo.gl/yshdwh

illera88 avatar Dec 12 '18 08:12 illera88

@illera88 sshd needs to run as system to be able to spawn off child process in different security contexts - these include sshd nonprivileged workers and user session proesses. sshd in interactive mode is only supported for debugging purposes. Also make a note of this restriction in https://github.com/PowerShell/Win32-OpenSSH/wiki/Troubleshooting-Steps

sshd.exe -d This will run sshd in interactive mode under currently logged on user (typically as admin). Note that, in this mode, you can only login as the "currently logged on user" and only using "key based auth". To overcome these restrictions, you should instead run psexec -s sshd.exe -d to run sshd as SYSTEM.

Can you please summarize again what your requirement is?

manojampalam avatar Dec 12 '18 21:12 manojampalam

The use case is pretty simple. If I do no have admin access but I still want to have an ssh server I can connect to and interact with.

The ssh protocol does not requiere to be used in ports <1024 and therefore it should be doable.

I know it may be a pain to make it work as a non service but I think it will be useful tu have a portable ssh server that you can drop and run without privileges.

BTW, awesome job porting it to Windows!!

On Wed, Dec 12, 2018 at 1:26 PM Manoj Ampalam [email protected] wrote:

@illera88 https://github.com/illera88 sshd needs to run as system to be able to spawn off child process in different security contexts - these include sshd nonprivileged workers and user session proesses. sshd in interactive mode is only supported for debugging purposes. Also make a note of this restriction in https://github.com/PowerShell/Win32-OpenSSH/wiki/Troubleshooting-Steps

sshd.exe -d This will run sshd in interactive mode under currently logged on user (typically as admin). Note that, in this mode, you can only login as the "currently logged on user" and only using "key based auth". To overcome these restrictions, you should instead run psexec -s sshd.exe -d to run sshd as SYSTEM.

Can you please summarize again what your requirement is?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/PowerShell/Win32-OpenSSH/issues/1304#issuecomment-446750165, or mute the thread https://github.com/notifications/unsubscribe-auth/AE89qMWJvYSdvLdWkHnMzWfrlYjBhT09ks5u4XShgaJpZM4ZMZON .

-- Alberto García Illera

GPG Public Key https://goo.gl/yshdwh

illera88 avatar Dec 12 '18 23:12 illera88