c# 服务器上传木马监控代码(包含可疑文件)

 更新时间:2010年05月28日 20:15:23   作者:  
c# 监控服务器上传木马(包含可疑文件)
复制代码 代码如下:

using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Net;
namespace TrojanMonitor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void SetTextCallback(string text);
private string fname,code,emailkey,ip;
private Thread thr;
private void fsw_Changed(object sender, FileSystemEventArgs e)
{//文件改动监控(包含了新增)
fname = e.Name;
thr = new Thread(new ThreadStart(chkfile));
thr.IsBackground = true;
thr.Start();
}
private void fsw_Renamed(object sender, RenamedEventArgs e)
{//重命名监控
fname = e.Name;
thr = new Thread(new ThreadStart(chkfile));
thr.IsBackground = true;
thr.Start();
}
private void chkfile(){
string filename = fname;
string content="",filepath=fsw.Path+@"\"+filename,fileName="",hzhui="";
fileName = Path.GetFileName(filename);
hzhui = Path.GetExtension(filename).ToLower();
if (hzhui == ".asp" || hzhui == ".aspx" || hzhui == ".php" || hzhui == ".jpg" || hzhui == ".gif")
{
try{
if (IsFileInUse(filename)) { System.Threading.Thread.Sleep(2000); chkfile(); }
StreamReader sr = new StreamReader(filepath);
content = sr.ReadToEnd();
sr.Close();
if (chkcontent(content)){
try{
string bakpath = Application.StartupPath + @"\TrojanMonitorbak",
logfile = bakpath + @"\log" + DateTime.Today.ToShortDateString() + ".dat",
newfile = bakpath + @"\" + DateTime.Today.ToShortDateString() + @"\",
newfilepath = newfile + DateTime.Now.Hour.ToString() + "点" + DateTime.Now.Minute.ToString() + "分" + DateTime.Now.Second.ToString() + "秒" + DateTime.Now.Millisecond.ToString() + "毫秒-" + fileName;
if (!Directory.Exists(bakpath)) { Directory.CreateDirectory(bakpath); }
if (!Directory.Exists(newfile)) { Directory.CreateDirectory(newfile);}
if (File.Exists(newfilepath)){File.Delete(newfilepath);}
File.Move(filepath,newfilepath);
string str = "[" + DateTime.Now + "] 发现可疑文件: [" + filepath + "] To [" + newfilepath + "]";
addtiem(str);
StreamWriter sw = File.AppendText(logfile);
sw.WriteLine(str + " \r\n");//写入日志
sw.Flush();
sw.Close();
sw.Dispose();
downurl("http://www.cqeh.com/mail/?EmailSubject=发现可疑文件(" + ip + ")&EmailKey=" + emailkey + "&SendHtml=[" + ip + "][" + DateTime.Now + "] 发现可疑文件: [" + filepath + "]");//发送Email
sw = File.AppendText(filepath);
sw.WriteLine("此文件检测到有可疑问题!请联系管理员!");
sw.Flush();
sw.Close();
sw.Dispose();
}
catch (Exception ex) { addtiem(ex.ToString()); }
}
}
catch (Exception ex) { addtiem(ex.ToString()); }
}
}
private string downurl(string url){
WebClient client = new WebClient();
string result=client.DownloadString(url);
return result;
}
private void addtiem(string text){
if (this.lb.InvokeRequired){
SetTextCallback d = new SetTextCallback(addtiem);
this.Invoke(d, new object[] { text });
} else {
this.lb.Items.Add(text);
}
}
private bool chkcontent(string content)
{
bool returnval = false;
string[] sArray = code.ToLower().Split('|');
content = content.ToLower();
foreach (string i in sArray)
{
if (content.IndexOf(i)>-1){returnval=true;break;}
}
return returnval;
}
private void Form1_Load(object sender, EventArgs e){
ip = Dns.GetHostEntry(Environment.MachineName).AddressList[0].ToString();
string config = File.ReadAllText(Application.StartupPath + "//monitorpath.ini");//获取监控路径 d:\wwwroot
try{
code = downurl("http://www.cqeh.com/txt/trojan.txt");
          //获取木马特征库
filepath.Text = config;
fsw.Path = config;
emailkey = downurl("http://www.cqeh.com/txt/trojanemailkey.txt");
          //获取发送email许可key;
this.ShowInTaskbar=false;
this.Visible = false;
}
catch (Exception ex){
MessageBox.Show("错误:" + ex.Message, "无法启动程序!", MessageBoxButtons.OK); Application.Exit();
}
finally { }
}
bool IsFileInUse(string fileName){//判断文件是否使用中
bool inUse = true;
if (File.Exists(fileName)){
FileStream fs = null;
try{fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,FileShare.None);inUse = false;}
catch{}finally{if (fs != null)fs.Close();}
return inUse;
}else{return false;}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized){
this.ShowInTaskbar = false;
this.Visible = false;
}
}
private void 退出系统ToolStripMenuItem_Click_1(object sender, EventArgs e){
Application.Exit();
}
private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e){
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e){
this.ShowInTaskbar = false;
this.Visible = false;
e.Cancel = true;
}
}
}

源码包下载

相关文章

  • C#利用GDI+画图的基础实例教程

    C#利用GDI+画图的基础实例教程

    编写图形程序时需要使用GDI(Graphics Device Interface,图形设备接口),所以通过网上的相关资料整理了这篇文章,下面这篇文章主要给大家介绍了关于C#利用GDI+画图基础的相关资料,需要的朋友可以参考下。
    2018-04-04
  • C#常用日期时间方法汇总

    C#常用日期时间方法汇总

    这篇文章介绍了C#常用的日期时间方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C# WORD操作实现代码

    C# WORD操作实现代码

    在当前项目开发过程中,客户有根据数据库数据生成WORD文档的需求,在和同事沟通的过程中,找到了两个解决方案
    2009-04-04
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下
    2015-11-11
  • C#聊天程序服务端与客户端完整实例代码

    C#聊天程序服务端与客户端完整实例代码

    这篇文章主要介绍了C#聊天程序服务端与客户端完整实例代码,很经典的应用,需要的朋友可以参考下
    2014-07-07
  • c#在excel中添加超链接示例分享

    c#在excel中添加超链接示例分享

    c#在excel中添加超链接示例分享,大家参考使用吧
    2013-12-12
  • C#调用halcon实现使用鼠标滚轮对图片进行缩放显示

    C#调用halcon实现使用鼠标滚轮对图片进行缩放显示

    这篇文章主要为大家详细介绍了C#如何调用halcon实现使用鼠标滚轮对图片进行缩放显示,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • C#使用OpenCvSharp实现图像校正

    C#使用OpenCvSharp实现图像校正

    这篇文章主要为大家详细介绍了C#如何使用OpenCvSharp实现图像校正功能,文中的示例代码简洁易懂,具有一定的学习价值,需要的小伙伴可以参考下
    2023-11-11
  • C#中的小数和百分数计算与byte数组操作

    C#中的小数和百分数计算与byte数组操作

    这篇文章介绍了C#中的小数和百分数计算与byte数组操作,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#生成指定范围内的不重复随机数

    C#生成指定范围内的不重复随机数

    对于随机数,大家都知道,计算机不 可能产生完全随机的数字,所谓的随机数发生器都是通过一定的算法对事先选定的随机种子做复杂的运算,用产生的结果来近似的模拟完全随机数,这种随机数被称 作伪随机数。伪随机数是以相同的概率从一组有限的数字中选取的。
    2015-05-05

最新评论