c# 服務器上傳木馬監(jiān)控代碼(包含可疑文件)
更新時間:2010年05月28日 20:15:23 作者:
c# 監(jiān)控服務器上傳木馬(包含可疑文件)
復制代碼 代碼如下:
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)
{//文件改動監(jiān)控(包含了新增)
fname = e.Name;
thr = new Thread(new ThreadStart(chkfile));
thr.IsBackground = true;
thr.Start();
}
private void fsw_Renamed(object sender, RenamedEventArgs e)
{//重命名監(jiān)控
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 + "] 發(fā)現(xiàn)可疑文件: [" + 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=發(fā)現(xiàn)可疑文件(" + ip + ")&EmailKey=" + emailkey + "&SendHtml=[" + ip + "][" + DateTime.Now + "] 發(fā)現(xiàn)可疑文件: [" + filepath + "]");//發(fā)送Email
sw = File.AppendText(filepath);
sw.WriteLine("此文件檢測到有可疑問題!請聯(lián)系管理員!");
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 + "http://monitorpath.ini");//獲取監(jiān)控路徑 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");
//獲取發(fā)送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 退出系統(tǒng)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#實現(xiàn)數(shù)據(jù)同步的方法(使用文件監(jiān)控對象filesystemwatcher)
- C#的FileSystemWatcher用法實例詳解
- c#使用filesystemwatcher實時監(jiān)控文件目錄的添加和刪除
- c#使用filesystemwatcher監(jiān)視文件系統(tǒng)的變化
- C#采用FileSystemWatcher實現(xiàn)監(jiān)視磁盤文件變更的方法
- C#監(jiān)控文件夾變化的方法
- C#判斷本地文件是否處于打開狀態(tài)的方法
- c# 共享狀態(tài)的文件讀寫實現(xiàn)代碼
- C#使用FileSystemWatcher控件實現(xiàn)的文件監(jiān)控功能示例
相關文章
C#調用halcon實現(xiàn)使用鼠標滾輪對圖片進行縮放顯示
這篇文章主要為大家詳細介紹了C#如何調用halcon實現(xiàn)使用鼠標滾輪對圖片進行縮放顯示,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-03-03
C#中的小數(shù)和百分數(shù)計算與byte數(shù)組操作
這篇文章介紹了C#中的小數(shù)和百分數(shù)計算與byte數(shù)組操作,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

