1、执行单条cmd命令

public static string ExecuteCmd(string command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
p.Start();//启动程序
//消除三行启动信息
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close();
return result;
}

2、一次执行多条cmd命令

public static string ExecuteCmd(string[] commands)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//p.StandardInput.AutoFlush = true;//每次写入命令后刷新,报未StandardInput未重定向
//p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
p.Start();//启动程序
//消除三行启动信息
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine(); //向cmd窗口发送输入信息
foreach (string cmd in commands)
{
p.StandardInput.WriteLine(cmd);
p.StandardInput.Flush();
}
p.StandardInput.WriteLine("exit");
string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close(); return result;
}

3、利用cmd重定向注册DLL、创建服务、加载证书

public static string RegsvrFile(string filePath, bool reg = true, bool fileBit64 = false)
{
string result;
string[] commands = { "", "" };
if (!fileBit64 && Environment.Is64BitOperatingSystem)
commands[0] = "cd /d %WinDir%\\SysWOW64";
if (reg)
commands[1] = "regsvr32 /s \"" + filePath + "\"";
else
commands[1] = "regsvr32 /u /s \"" + filePath + "\"";
if (string.IsNullOrWhiteSpace(commands[0]))
result = CmdOper.ExecuteCmd(commands[1]);
else
result = CmdOper.ExecuteCmd(commands);
return result;
} public static string ScCreate(string filePath, string serviceName, string displayName, string description, string start="auto", string depend=null)
{
string result;
string[] commands = { "", "" };
if (string.IsNullOrWhiteSpace(depend))
{
commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\"",
serviceName, filePath, start, displayName);
}
else
{
commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\" depend= {4}",
serviceName, filePath, start, displayName, depend);
}
if (!string.IsNullOrWhiteSpace(description))
{
commands[1] = string.Format("sc description {0} \"{1}\"", serviceName, description);
}
if (string.IsNullOrWhiteSpace(commands[1]))
result = CmdOper.ExecuteCmd(commands[0]);
else
result = CmdOper.ExecuteCmd(commands); return result;
} public static string ScStart(string serviceName)
{
string command = "net start " + serviceName;
return CmdOper.ExecuteCmd(command);
} public static string ScStop(string serviceName)
{
string command = "net stop " + serviceName;
return CmdOper.ExecuteCmd(command);
} public static string ScDelete(string serviceName)
{
string[] commands = { "net stop " + serviceName, "sc delete " + serviceName };
return CmdOper.ExecuteCmd(commands);
} public static string CertFile(string filePath, bool install=true)
{
string result;
string[] commands = { "", "" };
if (Environment.Is64BitOperatingSystem)
commands[0] = "cd /d %WinDir%\\SysWOW64";
if (install)
commands[1] = "certutil -addstore root \"" + filePath + "\"";
else
commands[1] = "certutil -delstore root \"" + filePath + "\"";
if (string.IsNullOrWhiteSpace(commands[0]))
result = CmdOper.ExecuteCmd(commands[1]);
else
result = CmdOper.ExecuteCmd(commands);
return result;
}

Cmd重定向的更多相关文章

  1. cmd 重定向

    关于cmd 命令的重定向输出 2>&1 mycommand >mylog.txt 2>&1 应该是最经典的用法了. 命令的结果可以通过" %> &qu ...

  2. <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序

    最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...

  3. Windows 常用Cmd命令行 (持续更新...)

    查看IP ipconfig 查看WIFI密码 netsh wlan show profiles wifi_name key = clear 系统探针 systeminfo CMD重定向 输出符号> ...

  4. Kali Linux渗透基础知识整理(四):维持访问

    Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...

  5. NetCat使用手册

    简介:   在网络工具中有“瑞士军刀”美誉的NetCat(以下简称nc),在我们用了N年了至今仍是爱不释手.因为它短小精悍(这个用在它身上很适合,现在有人已经将其修改成大约10K左右,而且功能不减少) ...

  6. IPC$命令详解

    一 摘要二 什么是ipc$三 什么是空会话四 空会话可以做什么五 ipc$所使用的端口六 ipc管道在hack攻击中的意义七 ipc$连接失败的常见原因八 复制文件失败的原因九 关于at命令和xp对i ...

  7. 445port入侵具体解释

    445port入侵具体解释   关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...

  8. 445port入侵详细解释

    445port入侵具体解释   关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...

  9. 空连接ipc$入侵

    使用命令 net use url=file://\\IP\ipc$\\IP\ipc$ "" /user:"" 就可以简单地和目标建立一个空连接(需要目标开放ip ...

随机推荐

  1. Install Centos7 on VirtualBox in mac

    Step 1:准备虚拟机及镜像 下载合适的Virtual Box版本 官方下载链接:https://www.virtualbox.org/wiki/Downloads 这里选择的版本是:https:/ ...

  2. mysql常用基础指令大全

    mysql指令 启动 net start mysql 退出mysql quit 登录 mysql -uroot -p 逻辑非 not ! 逻辑与 and && 或者 or || 逻辑异 ...

  3. 【JMeter_14】JMeter逻辑控制器__交替控制器<Interleave Controller>

    交替控制器<Interleave Controller> 业务逻辑: 根据被控制器触发执行次数,去依次执行控制器下的子节点<逻辑控制器.采样器>. 被触发执行可以由线程组的线程 ...

  4. 挖洞入门_显错型SQL注入

    简介:在漏洞盒子挖洞已经有一段时间了,虽说还不是大佬,但技术也有所进步,安全行业就是这样,只有自己动手去做,才能将理论的知识变为个人的经验.本篇文章打算分享一下我在挖显错型SQL注入漏洞过程中的一些个 ...

  5. 使用java类的方式配置spring 需要什么注解?

    1.@Configuration 修饰类,声明当前类是一个配置类,相当于applicationContext.xml文件 2.@ComponentScan 用于指定spring在初始化容器时要扫描的包 ...

  6. .net core 静态类获取appsettings

    注入获取 通过IConfiguration直接获取的方法官方文档里就有,可以直接看这里 如:appsettings.json { "Position": { "Title ...

  7. 计算机网络之tcp四次挥手

    TCP的四次挥手(Four-Way Wavehand)1.前言对于"三次握手"我们耳熟能详,因为其相对的简单.但是,我们却不常听见“四次挥手”,就算听过也未必能详细地说明白它的具体 ...

  8. SpringBoot--使用Spring Cache整合redis

    一.简介 Spring Cache是Spring对缓存的封装,适用于 EHCache.Redis.Guava等缓存技术. 二.作用 主要是可以使用注解的方式来处理缓存,例如,我们使用redis缓存时, ...

  9. 洛谷 P6145 【[USACO20FEB]Timeline G】

    这道题难就难在建图吧,建图懂了之后,跑一遍最长路就好了(也就是关键路径,但是不会用拓补排序求qnq,wtcl). 怎么建图呢?先不管输入的S,看下面的输入,直接建有向边即可,权值为x.如果现在跑最长路 ...

  10. 使用Xmanager连接linux,操作“xhost +”时出现类似“xhost: unable to open display "192.168.1.1811:1.0" ”问题的解决

    远程连接linux服务器时,有的时候需要把服务器上的图形界面投影到本地来进一步操作,比如linux下安装oracle时就需要在oracle用户下允许视图状态投影到本地,这需要使用命令: xhost + ...