C# Windows IPSEC监控(仅此一家,别无分店)
Windows IPSEC监控,使用C#编写,输出为一行字符串,可以按照既有IPSEC规则生成模板
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace WindowsIPSecMonitor
{
class WindowsIPSecMonitor
{
//*****颜色提示*****
//红色警告
private static void RedError(string text)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
//黄色提示
private static void YellowWarn(string text)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
//绿色提示
private static void GreenPrint(string text)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
//系统命令执行函数
private static string Execute(string command, int seconds)
{
string output = ""; //输出字符串
if (command != null && !command.Equals(""))
{
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";//设定需要执行的命令
startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
startInfo.RedirectStandardInput = false;//不重定向输入
startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
try
{
if (process.Start())//开始进程
{
if (seconds == 0)
{
process.WaitForExit();//这里无限等待进程结束
}
else
{
process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
}
output = process.StandardOutput.ReadToEnd();//读取进程的输出
}
}
catch
{
}
finally
{
if (process != null)
process.Close();
}
}
return output;
}
//追加写入文件函数
private static void FileRec(string input, string filename)
{
FileStream fs = new FileStream(filename, FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
//开始写入
sw.Write(input);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
//追加一个文件到另一个文件末尾
private static void AppendFile(string Filenamesrc, string Filenamedst)
{
string command = "type " + Filenamesrc + ">>" + Filenamedst;
Execute(command, 1);
}
//生成机器当前操作模板用作比对
private static void GenCurrentLocalTemplates()
{
//命令全局变量
string cmd;
//获取所有的策略名
//cmd = "netsh ipsec static show policy all | findstr \"策略名称\" 2>&1"; //适用中文简体语言环境
cmd = "netsh ipsec static show policy all | findstr \"Policy name\" 2>&1";//适用于英文环境
string[] policy = Execute(cmd, 1).Replace("策略名称", "").Replace("Policy Name", "").Replace("\r\n", "").Replace(":", "").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string policyname in policy)
{
//执行命令生成文件
cmd = "netsh ipsec static show rule all policy = " + policyname + " level = Verbose Format = table>CurrentFullIPSec.log 2>&1";
Execute(cmd, 1);
//处理文件
try
{
//int Counter = 0;
string line;
//处理文件中“是”/“YES”开头不连续的行
StreamReader file1 = new StreamReader("CurrentFullIPSec.log", Encoding.GetEncoding("GB2312"));
while ((line = file1.ReadLine()) != null)
{
//if (line.StartsWith("是"))//适用于中文简体语言环境
if (line.StartsWith("YES"))//适用于英文环境
{
string ProcessedData = Environment.NewLine + line.TrimEnd();
FileRec(ProcessedData, "Temp1.log");
}
else
{
FileRec(line + Environment.NewLine, "Temp1.log");
}
//Counter++;//计数
}
file1.Close();//关闭文件读取流
//截取所有YES开头的行保存到文件
//Counter = 0;
StreamReader file2 = new StreamReader("Temp1.log", Encoding.GetEncoding("GB2312"));
while ((line = file2.ReadLine()) != null)
{
//if (line.StartsWith("是") || line.StartsWith("YES") || line.StartsWith("筛选器列表名称") || line.StartsWith("Rule Name") || line.StartsWith("筛选器操作名称") || line.StartsWith("FilterAction Name") || line.StartsWith("操作") || line.StartsWith("Action") || line.StartsWith("筛选器数目") || line.StartsWith("No. of Filters"))
if (line.StartsWith("是") || line.StartsWith("YES") || line.StartsWith("操作") || line.StartsWith("Action"))
{
string ProcessedData = line.Trim() + Environment.NewLine;
ProcessedData = ProcessedData.Replace(" ", "").Replace("\t", "");
FileRec(ProcessedData, "LocalTemplate.log");
}
//Counter++;//计数
}
file2.Close();//关闭文件读取流
//删除临时文件
File.Delete("Temp1.log");
File.Delete("CurrentFullIPSec.log");
//Counter = 0;
StreamReader file3 = new StreamReader("LocalTemplate.log", Encoding.GetEncoding("GB2312"));
while ((line = file3.ReadLine()) != null)
{
if (line.StartsWith("操作") || line.StartsWith("Action"))
{
//用操作名作为文件名
string NewFilename = line.Trim() + ".txt";
//将文件内容读取进操作名文件
AppendFile("Temp2.log", NewFilename);
File.Delete("Temp2.log");
}
else
{
FileRec(line + Environment.NewLine, "Temp2.log");
}
//Counter++;//计数
}
file3.Close();//关闭文件读取流
File.Delete("LocalTemplate.log");
}
catch (IOException)
{
Console.WriteLine("IO Error! Please consult the programmer!" + Environment.NewLine);
}
}
}
//根据监控模板生成比对文件
private static void GenMonitorTemplates(string TemplateFile)
{
string line;
try
{
StreamReader file = new StreamReader(TemplateFile, Encoding.GetEncoding("GB2312"));
line = file.ReadToEnd();
string[] policylines = line.Split(new string[] { "BLOCK", "PERMIT" }, StringSplitOptions.RemoveEmptyEntries);
FileRec(policylines[0], "MonitorPermitTemplate.txt");
FileRec(policylines[1], "MonitorBlockTemplate.txt");
file.Close();//关闭文件读取流
}
catch (Exception)
{
Console.WriteLine("File IO Error!");
}
}
//比对文件
private static string CompareFile(string FileSRC, string FileDST)
{
//int Counter = 0;
string lineA;
string lineB;
string lineC = "";
if (!File.Exists(FileSRC) || !File.Exists(FileDST))
{
return "Files don't exist! Comparation failed!";
}
else
{
try
{
StreamReader fileA = new StreamReader(FileSRC, Encoding.GetEncoding("GB2312"));
StreamReader fileB = new StreamReader(FileDST, Encoding.GetEncoding("GB2312"));
lineB = fileB.ReadToEnd();
while ((lineA = fileA.ReadLine()) != null)
{
if (!lineB.Contains(lineA.Trim()))
{
lineC += lineA + Environment.NewLine;
}
//Counter++;//计数
}
fileA.Close();//关闭文件读取流
fileB.Close();//关闭文件读取流
}
catch (Exception)
{
Console.WriteLine("File IO error!");
}
return lineC;
}
}
//删除临时文件
private static void DeleteFile()
{
//File.Delete("操作允许.txt");//中文简体环境
//File.Delete("操作阻止.txt");//中文简体环境
File.Delete("ActionPERMIT.txt");//英文环境
File.Delete("ActionBLOCK.txt");//英文环境
File.Delete("MonitorPermitTemplate.txt");//删除根据监控模板生成的PERMIT对比文件
File.Delete("MonitorBlockTemplate.txt");//删除根据监控模板生成的BLOCK对比文件
}
//数据类
public class Data
{
public string time;
public string result;
public string status;
public string JobID;
public void Print_data()
{
Console.WriteLine("{\"result\":\"" + result + "\",\"status\":\"" + status + "\",\"time\":\"" + time + "\",\"id\":\"" + JobID + "\",\"info\":\"\"}");
}
}
//主函数
static void Main(string[] args)
{
string Current_Path = AppDomain.CurrentDomain.BaseDirectory;
//一次赋值使用的全局变量,用于接收命令行字符串
string cmd;
try
{
//*****帮助*****
if (args[0] == "-h" || args[0] == "--help")
{
GreenPrint("Usage:");
GreenPrint("Use WindowsIPSecMonitor.exe --Backup to backup current localmachine's IPSec");
GreenPrint("Use WindowsIPSecMonitor.exe --GCLMT to generate current localmachine's IPSec monitor tempalte");
GreenPrint("Use WindowsIPSecMonitor.exe --Investigate --idle [MonitorTemplateFileName] to see the IPSec comparation result");
GreenPrint("Use WindowsIPSecMonitor.exe [JobID] [Server] [MonitorTemplateFileName] to see the monitor result");
Environment.Exit(0);
}
//备份IPSec
if (args[0] == "--Backup")
{
string time = DateTime.Now.ToString("yyyy-MM-dd#HH.mm.ss");
cmd = "netsh ipsec static exportpolicy file=" + time;
Execute(cmd, 1);
YellowWarn("IPSec has been backed up as " + time + ".ipsec!");
Environment.Exit(0);
}
//生成当前机器用作监控的模板
if (args[0] == "--GCLMT")
{
//生成用作对比的当前机器模板
GenCurrentLocalTemplates();
//cmd = "(echo PERMIT&type 操作允许.txt&echo BLOCK&type 操作阻止.txt)>NewlyGeneratedTempalteForMonitor.txt";//简体中文环境
cmd = "(echo PERMIT&type ActionPERMIT.txt&echo BLOCK&type ActionBLOCK.txt)>NewlyGeneratedTempalteForMonitor.txt";//英文环境
Execute(cmd, 1);
YellowWarn("Template generated! Filename is NewlyGeneratedTempalteForMonitor.txt.");
//File.Delete("操作允许.txt");//简体中文环境
//File.Delete("操作阻止.txt");//简体中文环境
File.Delete("ActionPERMIT.txt");//英文环境
File.Delete("ActionBLOCK.txt");//英文环境
Environment.Exit(0);
}
}
catch (Exception)
{
RedError("Parameter error! Use -h or --help for help");
Environment.Exit(0);
}
//实例化类
Data D = new Data();
D.status = "2";
D.result = "IPSec is fine!";
D.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
try
{
D.JobID = args[0];
}
catch (Exception)
{
RedError("Parameter error! Use -h or --help for help");
Environment.Exit(0);
}
//检查IPSec是否开启
cmd = "netsh ipsec static show policy all";
bool NotAssigned1 = Execute(cmd, 1).Contains("Assigned : NO");
bool NotAssigned2 = Execute(cmd, 1).Contains("已分配 : 否");
if (NotAssigned1 || NotAssigned2)
{
D.status = "4";
D.result = "Policy is not assigned!";
D.Print_data();
Environment.Exit(0);
}
//生成用作对比的当前机器模板
GenCurrentLocalTemplates();
try
{
//处理生成监控模板对比文件,使用命令行参数做文件名
GenMonitorTemplates(Current_Path + args[2]);
//比对文件
//string result1 = CompareFile("MonitorPermitTemplate.txt", "操作允许.txt");//中文简体环境
//string result2 = CompareFile("操作允许.txt", "MonitorPermitTemplate.txt");//中文简体环境
//string result3 = CompareFile("MonitorBlockTemplate.txt", "操作阻止.txt");//中文简体环境
//string result4 = CompareFile("操作阻止.txt", "MonitorBlockTemplate.txt");//中文简体环境
string result1 = CompareFile("MonitorPermitTemplate.txt", "ActionPERMIT.txt");//英文环境
string result2 = CompareFile("ActionPERMIT.txt", "MonitorPermitTemplate.txt");//英文环境
string result3 = CompareFile("MonitorBlockTemplate.txt", "ActionBLOCK.txt");//英文环境
string result4 = CompareFile("ActionBLOCK.txt", "MonitorBlockTemplate.txt");//英文环境
//显示调试信息
if (args[0] == "--Investigate")
{
YellowWarn("LocalPermit lacks the following line(s):");
Console.WriteLine(result1);
YellowWarn("MonitorPermit lacks the fowllowing line(s):");
Console.WriteLine(result2);
YellowWarn("LocalBlock lakcs the following line(s):");
Console.WriteLine(result3);
YellowWarn("MonitorBlock lacks the following line(s):");
Console.WriteLine(result4);
DeleteFile();
}
else
{
if (result1 != "" || result2 != "" || result3 != "" || result4 != "")
{
D.status = "4";
D.result = "IPSec error!";
D.Print_data();
DeleteFile();
Environment.Exit(0);
}
//如果没有错误则输出正确结果
D.Print_data();
DeleteFile();
Environment.Exit(0);
}
}
catch (Exception)
{
RedError("Error! No filename parameter provided!");
DeleteFile();
}
}
}
}
C# Windows IPSEC监控(仅此一家,别无分店)的更多相关文章
- paip.windows io监控总结
paip.windows io监控总结 io的主要参数是个.disk queue length 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专 ...
- Windows 回调监控 <二>
在之前的文章Windows 回调监控 <一> 总结了关于CreateProcessNotify,CreateProcessNotifyEx和LoadImageNotify一些用法,之后产生 ...
- windows 进程监控 Procmon.exe
windows 进程监控 Procmon.exe window下一个程序打开太慢,可以用此程序监控.在哪一步慢了,读取文件还是注册表. ProcessMonitor3.2 Process Monito ...
- Windows性能计数器监控实践
Windows性能计数器(Performance Counter)是Windows提供的一种系统功能,它能实时采集.分析系统内的应用程序.服务.驱动程序等的性能数据,以此来分析系统的瓶颈.监控组件的表 ...
- python对 windows系统监控插件
在python编程的windows系统监控中,需要监控监控硬件信息需要两个模块:WMI 和 pypiwin32 .
- Windows - 性能监控之磁盘剩余空间大小警报
开始 -> 运行 -> 键入命令 perfmon.msc 数据收集器(Data Collector Sets) -> 用户自定义(User Defined)
- Windows zabbix监控远程进程实现机制
最近负责zabbix监控部署方面的工作,需要完成本地服务端监控远程虚拟机的运行状态(CPU.打开的进程等),与大家分享下我的实现方法. (1) 首先,需要实现记录zabbix客户端的进程的批处理:za ...
- Windows 回调监控 <一>
在x86的体系结构中,我们常用hook关键的系统调用来达到对系统的监控,但是对于x64的结构,因为有PatchGuard的存在,对于一些系统关键点进行hook是很不稳定的,在很大几率上会导致蓝屏的发生 ...
- 探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据
上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...
随机推荐
- uva-11324-SCC+dp
https://vjudge.net/problem/UVA-11324 给出一幅有向图,问最大能找到多少个节点,使得这些节点中任意两个节点之间都至少有一条可达路径. 找出SCC后缩点求权重最大路即可 ...
- 提高Bash使用效率的方法
环境:centos6.5 1.移动 Ctrl + a :移到命令行首Ctrl + e :移到命令行尾 Ctrl + xx:在命令行首和光标之间移动 左右键移动字符 Ctrl+左右键移动单词(不记快捷键 ...
- python中的IO模块
1.简介 读写文件是常见的IO操作,python内置了读写文本的函数. 读写文件的模式描述如下: 模式 描述 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb 以二进制格式打 ...
- Linux修改用户密码有效期
linux默认用户的密码是永不过期的,但是出于安全考虑在企业环境中一般会要求设置过期日期:但有时要求90天就过期,在这种严柯条件下我们有可能想给某个或某些用户开设后门,延长其密码有效期. 一.用户密码 ...
- PE文件结构解析
说明:本文件中各种文件头格式截图基本都来自看雪的<加密与解密>:本文相当<加密与解密>的阅读笔记. 1.PE文件总体结构 PE文件框架结构,就是exe文件的排版结构.也就是说我 ...
- CAS5.3-下载安装
cas版本:cas5.3 环境准备:1.JDK8:2.Apache Maven:3.Apache Tomcat:4.git 1.通过git工具(本文使用的是TortoiseGit,也可用IDE或者gi ...
- Mysql索引引起的死锁
提到索引,首先想到的是效率提高,查询速度提升,不知不觉都会有一种心理趋向,管它三七二十一,先上个索引提高一下效率..但是索引其实也是暗藏杀机的... 今天压测带优化项目,开着Jmeter高并发访问项目 ...
- js里面判断一个字符串是否包含某个子串的方法
1. ES6的includes, 返回 Boolean var string = "foo", substring = "oo"; string.include ...
- 【配置】pom.xml的配置
pom.xml的配置: 地址:https://mvnrepository.com/ 示例:配置log4j 1.在搜索框中搜索log4j 2.在搜索结果页点击log4j 3.选择一个最新的版本,点击 4 ...
- 基础数据类型的坑和集合及深浅copy
一.基础数据类型的坑: 元组: 如果一个元组中,只有一个元素,且没有逗号,则该"元组"与里面的数据的类型相同. # 只有一个数据,且没有逗号的情况: print(tu1,type( ...