Many 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 many cases, for example to debug ERROR_ACCESS_DENIED type errors that are coming from a system service.

Let’s take a simple example to illustrate where running CMD as system would 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 would you debug COM calls, or even a failed CoCreateInstance? The “source code” for the COM infrastructure is not available. And even if you would 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 would 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, someone told me a quick-and-dirty solution that uses the AT.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 AT.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 and Settings\Adi>time

The current time is: 16:29:00.96

Enter the new time:

E:\Documents and Settings\Adi>at 16:30 /interactive cmd.exe

Added a new job with job ID = 1

Well, this method looked pretty ugly 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 and Settings\Adi>sc create testsvc binpath= "cmd" type= own type= interact

[SC] CreateService SUCCESS

E:\Documents 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 and Settings\Adi>sc delete testsvc

[SC] DeleteService SUCCESS

E:\Documents and Settings\Adi>sc create testsvc binpath= "cmd /K start" type= own type= interact

[SC] CreateService SUCCESS

E:\Documents 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 would like to mention one more thing. You can use this new service to start as many CMD windows as you want, and you will get a new CMD window as soon as you do a “sc start testsvc” again.

Running CMD.EXE as Local System(转)的更多相关文章

  1. Local System、Local Service與Network Service

    CreateService参数介绍SC_HANDLE CreateService( SC_HANDLE hSCManager, //服务控制管理程序维护的登记数据库的句柄,由系统函数OpenSCMan ...

  2. (转)内置系统账户:Local system/Network service/Local Service 区别

    最近会转载一些 MSSQL 基础相关的文章. 参考文献: http://www.cnblogs.com/xianspace/archive/2009/04/05/1429835.html 前言 今天在 ...

  3. Local System/Network Service/Local Service

    // The name of the account under which the service should run// 1 NT AUTHORITY\\SYSTEM 2 NT AUTHORIT ...

  4. C#程序调用cmd.exe执行命令

    代码部分 using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Wi ...

  5. C# 调用cmd.exe的方法

    网上有很多用C#调用cmd的方法,大致如下: [c-sharp] view plaincopy private void ExecuteCmd(string command)   {   Proces ...

  6. [转帖]内置系统账户:Local system/Network service/Local Service 区别

    内置系统账户:Local system/Network service/Local Service 区别 学习使用 xp_cmdshell 的时候 发现必须 sqlserver 的服务运行在local ...

  7. 内置系统账户:Local system/Network service/Local Service 区别

    参考文献: http://www.cnblogs.com/xianspace/archive/2009/04/05/1429835.html 前言 今天在安装sqlserver2008 r2的时候,在 ...

  8. Java 调用cmd.exe命令

    原理:java的Runtime.getRuntime().exec(commandText)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是 ...

  9. C#窗体内嵌外部程序(cmd.exe)的显示【转载】

    [DllImport("User32.dll ", EntryPoint = "SetParent")] private static extern IntPt ...

随机推荐

  1. mybatis 控制台打印sql脚本

    在mybatis-config.xml文件中加一句 <setting name="logImpl" value="STDOUT_LOGGING" /> ...

  2. java 多线程总结篇1之——基本概念

    1.什么是线程 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程.(进程是资源分配的最小单位) 线程:同一类线程共享代码和数据空间,每个线程 ...

  3. (五)HttpClient 连接超时及读取超时

    第一节: HttpClient 连接超时及读取超时 HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接 ...

  4. python3项目之数据可视化

    数据可视化指的是通过可视化表示来探索数据,它与数据挖掘紧密相关,而数据挖掘指的是使用代码来探索数据集的规律和关联. 数据科学家使用Python编写了一系列令人印象深刻的可视化和分析工具,其中很多也可供 ...

  5. VS Code折腾记 - (1)扯淡

    题外话 距离上篇介绍VSCode的文章已经过去四十多天,已经在正式项目作为主力开发工具了. 社区的发展非常快速,更新迭代够快,功能基本已经满足我所需了: 这个系列教程基于最新的vs code 1.8. ...

  6. Linux学习笔记:touch新建文件、修改访问、改动时间

    touch用于创建新的空文件或者修改已有文件的时间戳. 语法:touch file.txt 如果file存在,使用touch指令可更改这个文件或目录的日期时间,包括存取时间和更改时间. 如果file不 ...

  7. JAVA 转义字符串中的特殊字符

    package test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { pu ...

  8. c 语言文本文件判断是否到达结尾的问题

    在c语言中,判断文件结尾有两种方法,第一种是使用feof()函数,feof(fp)用于测试fp所指向的文件的当前状态是否为“文件结束”.如果是,函数则返回的是非0值(真),否则为0(假),要注意的是, ...

  9. 2016-2017-2 20155309南皓芯《java程序设计》第十周学习总结

    教材内容总结 网络编程 定义:网络编程就是在两个或两个以上的设备之间传输数据. 计算机网络概述: 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 网络中的每个设备都会有一个唯一的数 ...

  10. springboot 零xml集成mybatis-plus

    工程结构 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...