Running cmd. EXE as local system

Source: Internet
Author: User

Times in the past I had to run an interactive command-line Shell under the local system account. that is, a cmd window on your desktop running under the system account. this technique is extremely useful in your cases, for example to debug error_access_denied type errors that are coming from a system service.

 

Let's take a simple example to initialize strate where running cmd as system wocould be useful. let's say that you write an ASP or ASP. NET application, configured to run under local system, without client impersonation. this application gets some weird e_accessdenied errors when invoking some COM objects (say that you are developing an ASP. net layer on top of visual source safe, for example ).

 

One option to "debug" this is to attach a debugger to the ASP. net working process, and attempt to figure out what goes wrong. but how woshould you debug com CILS, or even a failed cocreateinstance? The "Source code" for the com infrastructure is not available. and even if you wocould be able to get access to this complex source code, there is a very high chance that you won't understand what happens under the cover.

 

So, instead, here is a simpler solution: Just start a cmd. EXE instance, running as local system (using one of the methods described below ). then, in this instance, try to execute an equivalent VB script (or C ++) code that does almost the same thing. you wowould probably get the same e_accessdenied but this time you are now able to fine-tune the com security parameters, like specifying special settings through dcomcnfg. EXE.

 

Anyway, let's go back to the original issue-how to get "cmd. EXE "to run as the system account. interactively. my first shot was to the RunAs. EXE command. we already know that RunAs allows you to run a process under a given user account. unfortunately, RunAs won't work in our case, since a process running under an interactive logon session won't be able to spawn a local system process. for security reasons, only a system process (that is already running as local system) Like the SCM (services.exe) or RPCSS is allowed to spawn another system process.

 

After asking around und, someone told me a quick-and-dirty solution that uses the. EXE command. at can be used to spawn jobs at a certain point in time. what people don't immediately realize is that these jobs run under the local system account. in fact, what happens under the cover is that at creates a special task in the task scheduler database. to complete the trick, at allows tasks to run in "interactive" mode, I. e. the ability to interact with the desktop Console (if active ). in the end, to run a cmd through the. EXE command you must do two things:

1) Get the local time (through the time shell command, for example)

2) add one minute to this time

3) run the AT command with this new time.

4) Wait one minute for the command window to appear.

 

E: \ Documents ents and Settings \ Adi> time

The current time is:16: 2900.96

Enter the new time:

 

E: \ Documents ents and Settings \ Adi>16: 30/Interactive cmd.exe

Added a new job with job id = 1

 

Well, this method looked pretty uugly to me. first of all is not Scriptable. second, it requires me to wait for one minute for the command window to appear. at last, the job remains there and needs to be cleaned up.

 

After this first shot, I started looking for simpler methods to show this command window. A nice start point was the SC. EXE command. if you are not already familiar with it, note just the fact that this is the standard utility for Windows service related tasks: creating, deleting, enumerating services and their configuration details, etc.

 

My first attempt was to create an interactive service that just runs cmd. EXE. this didn't work. I found out that when SCM starts a service, it waits a little for this new process to setup a communication channel (through the registerservicectrlhandler API ). this channel is needed by SCM to send commands to the service.

 

E: \ Documents ents and Settings \ Adi> SC create testsvc binpath = "cmd" type = own type = interact

[SC] createservice success

 

E: \ Documents ents and Settings \ Adi> SC start testsvc

[SC] startservice failed 1053:

 

The Service did not respond to the start or control request in a timely fashion.

 

But anyway, I succeeded to run cmd. EXE for a very brief period of time as local system. after a couple of tweaks, I discovered the solution. you need to "convince" the original cmd. EXE process to spawn a separate cmd. EXE command window. then, even if the original process dies, the other cmd. EXE instance will stay alive. spawning a new cmd. EXE is very easy through the start shell command. in fact, just running start (with no parameters) from a cmd shell will start another window.

 

Let's Delete the original service and try again.

 

E: \ Documents ents and Settings \ Adi> SC Delete testsvc

[SC] deleteservice success

 

E: \ Documents ents and Settings \ Adi> SC create testsvc binpath = "CMD/K start" type = own type = interact

[SC] createservice success

 

E: \ Documents ents and Settings \ Adi> SC start testsvc

[SC] startservice failed 1053:

 

The Service did not respond to the start or control request in a timely fashion.

 

Note that this time, the SC start immediately creates a new cmd window, even if the original cmd window failed to start with error 1053 (this is expected since cmd. EXE doesn' t have any service related code in it ).

 

In the end, I wowould like to mention one more thing. you can use this new service to start as your cmd windows as you want, and you will get a new cmd window as soon as you do a "SC start testsvc" again.

 

Original article: http://blogs.msdn.com/adioltean/articles/271063.aspx

Also see:

Http://geek.hubkey.com/2008/02/impersonating-built-in-service-account.html

 

 

Impersonate windows built-in account.

Http://www.codeproject.com/KB/cs/cpimpersonation1.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.