之前的文档中,描写了如何对WFP防火墙进行操作以及如何在防火墙日志中读取被防火墙拦截网络通讯的日志。这边文档,着重描述如何读取操作系统中所有被放行的网络通信行为。
读取系统中放行的网络通信行为日志,在win10之后的操作系统上,也可以通过前一篇提到的读取阻断日志的方式进行读取(以FWPM_NET_EVENT0.type字段区分),但是在较老的系统中却不支持直接读取。为了保持系统兼容性,可以通过读取操作系统安全日志(EventId:5156)的方式进行网络通信日志的采集。
采用读取安全日志的方式进行网络事件获取,首先需要在系统中开启审计功能。在代码里面也有多种方式可以开启,之后会单开一篇文档进行描述,在这里先手动开启。
网络通信日志默认情况下是开启状态,为了以防万一,每次获取之前需要使用代码开启一次。使用代码的开启方式下次单开文档分享。
优点:兼容性高,可支持XP/2003操作系统。读取性能高。
缺点:无法做过滤,在大量日志中提取少量日志时效率较低
使用ReadEventLog读取Windows的安全日志只需要三步即可,1、打开EventLog句柄;2、使用ReadEventLog循环读取日志;3、关闭EventLog句柄。具体API描述如下。
打开EventLog句柄
HANDLE OpenEventLog(
LPCSTR lpUNCServerName,
LPCSTR lpSourceName
);
读取日志
BOOL ReadEventLog(
HANDLE hEventLog,
DWORD dwReadFlags,
DWORD dwRecordOffset,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
DWORD *pnBytesRead,
DWORD *pnMinNumberOfBytesNeeded
);
输入参数
输出参数
关闭EventLog句柄
BOOL CloseEventLog(
HANDLE hEventLog
);
关联结构体
typedef struct _EVENTLOGRECORD {
DWORD Length;
DWORD Reserved;
DWORD RecordNumber;
DWORD TimeGenerated;
DWORD TimeWritten;
DWORD EventID;
WORD EventType;
WORD NumStrings;
WORD EventCategory;
WORD ReservedFlags;
DWORD ClosingRecordNumber;
DWORD StringOffset;
DWORD UserSidLength;
DWORD UserSidOffset;
DWORD DataLength;
DWORD DataOffset;
} EVENTLOGRECORD, *PEVENTLOGRECORD;
Length:当前结构体的长度,由于ReadEventLog是以内存块的方式返回,单次返回的内存块中可能包含多个,特别是为了节省资源,可能会在代码中刻意一次读取大量的EventLogRecord结构体。这些结构体在内存块中,就以Length参数作为分界线来进行分割
Reserved:保留,没有可以研究过用于啥
RecordNumber:日志序号,可配合ReadEventLog函数中的dwReadFlags参数和dwReadOffset参数设置读取的偏移地址
TimeGenerated:事件时间,转time_t就可以
TimeWrittern:日志写入时间,time_t格式
EventID:事件ID,最高两位代表严重性,第三位代表是否为系统事件,低16位代表在安全日志中可见的ID。
EventType:事件类型,
NumStrings:包含字符串数目
EventCategory:事件类别
ReservedFlags:保留
ClosingRecordNumber:保留
StringOffset:事件包含字符串起始地址的偏移。字符串按顺序依次在内存中存储,0长度的字符串代表结束。字符串的顺序,和在事件查看器中看到的顺序一致,如图
UserSidLength/UserSidOffset: 用户SID的长度及偏移,与字符串类似
DataLength/DataOffset:数据部分的长度及偏移
参考代码
#include
#include
#include
#include
#define MAX_EVENTLOG_READONCE 2048
void ReadLogsByReadEventLogAPI()
{
LPBYTE pEventLogBuffer = new(std::nothrow) BYTE[MAX_EVENTLOG_READONCE];
if (pEventLogBuffer == NULL)
{
return;
}
ZeroMemory(pEventLogBuffer, MAX_EVENTLOG_READONCE);
HANDLE hEventLog = OpenEventLog(NULL, L"Security");
if (hEventLog == NULL)
{
delete[] pEventLogBuffer;
pEventLogBuffer = NULL;
return;
}
DWORD dwMemoryLen = MAX_EVENTLOG_READONCE;
DWORD dwReaded = 0, dwMiniMemoryNeeded = 0;
while (true)// -_-|||
{
BOOL isSucc = ReadEventLog(hEventLog, EVENTLOG_BACKWARDS_READ | EVENTLOG_SEQUENTIAL_READ, 0, pEventLogBuffer, dwMemoryLen, &dwReaded, &dwMiniMemoryNeeded);
if (isSucc == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER && dwMiniMemoryNeeded)
{
delete[] pEventLogBuffer;
pEventLogBuffer = NULL;
pEventLogBuffer = new (std::nothrow) BYTE[dwMiniMemoryNeeded];
if (pEventLogBuffer == NULL)
{
break;
}
ZeroMemory(pEventLogBuffer, dwMiniMemoryNeeded);
dwMemoryLen = dwMiniMemoryNeeded;
isSucc = ReadEventLog(hEventLog, EVENTLOG_BACKWARDS_READ | EVENTLOG_SEQUENTIAL_READ, 0, pEventLogBuffer, dwMemoryLen, &dwReaded, &dwMiniMemoryNeeded);
}
if (isSucc == FALSE)
{
break;
}
int pos = 0;
do
{
EVENTLOGRECORD* pTempRecord = (EVENTLOGRECORD*)(pEventLogBuffer + pos);
__time32_t occurTime = pTempRecord->TimeWritten;
int nEventId = pTempRecord->EventID & 0xffff;
if (nEventId != 5156)
{
pos = pos + pTempRecord->Length;
continue;
}
std::vector vecEventParam;
LPBYTE pTempBuffer = (LPBYTE)pTempRecord + pTempRecord->StringOffset;
int strCount = 0;
while ((pTempBuffer < ((LPBYTE)pTempRecord + pTempRecord->Length)) && (strCount < pTempRecord->NumStrings))
{
int len = wcslen((wchar_t*)pTempBuffer);
if (len == 0)
{
break;
}
vecEventParam.push_back((wchar_t*)pTempBuffer);
pTempBuffer = pTempBuffer + (len + 1) * sizeof(wchar_t);
strCount++;
}
if (vecEventParam.size() > 10)
{
std::wcout << "\n\n=========================================" << std::endl;
std::wcout << L"Source:\t" << vecEventParam[3] << L"[" << vecEventParam[4] << L"]" << std::endl;
std::wcout << L"Destination:\t" << vecEventParam[5] << L"[" << vecEventParam[6] << L"]" << std::endl;
std::wcout << L"Protocol Code:\t" << vecEventParam[7] << std::endl;
std::wcout << L"Process Id:\t" << vecEventParam[0] << std::endl;
std::wcout << L"Process Name:\t" << vecEventParam[1] << std::endl;
std::wcout << "=========================================\n\n" << std::endl;
}
pos = pos + pTempRecord->Length;
} while (pos < dwReaded);
}
CloseEventLog(hEventLog);
delete[] pEventLogBuffer;
pEventLogBuffer = NULL;
}
优点:性能高,可自由配置过滤条件,方便在海量日志中检索,可读取的内容相当丰富
缺点:不支持xp/2003操作系统
Evt系列API涉及到的功能较多,如果只需要读取系统日志的话,只需要枚举、遍历、读取、关闭四步即可完成。
枚举当前日志
EVT_HANDLE EvtQuery(
EVT_HANDLE Session,
LPCWSTR Path,
LPCWSTR Query,
DWORD Flags
);
遍历日志,获取下一条
BOOL EvtNext(
EVT_HANDLE ResultSet,
DWORD EventsSize,
PEVT_HANDLE Events,
DWORD Timeout,
DWORD Flags,
PDWORD Returned
);
读取日志内容
BOOL EvtRender(
EVT_HANDLE Context,
EVT_HANDLE Fragment,
DWORD Flags,
DWORD BufferSize,
PVOID Buffer,
PDWORD BufferUsed,
PDWORD PropertyCount
);
关闭枚举句柄
BOOL EvtClose(
EVT_HANDLE Object
);
实例代码
void parseEventXML(std::wstring wstrXML)
{
std::wstring_convert> cv;
std::string utfXML = cv.to_bytes(wstrXML);
tinyxml2::XMLDocument rootXML;
if (rootXML.Parse(utfXML.c_str()) != tinyxml2::XML_SUCCESS)
{
return;
}
tinyxml2::XMLNode* rootNode = rootXML.FirstChild();
if (rootNode == NULL)
{
return;
}
std::string processId;
std::string processName;
std::string sourceAddress;
std::string sourcePort;
std::string destAddress;
std::string destPort;
std::string protocol;
tinyxml2::XMLNode* eventNode = rootXML.FirstChildElement("Event");
if (eventNode)
{
tinyxml2::XMLElement* eventDataNode = eventNode->FirstChildElement("EventData");
if (eventDataNode == NULL)
{
return;
}
tinyxml2::XMLElement* dataNode = eventDataNode->FirstChildElement("Data");
do
{
std::string strName = dataNode->Attribute("Name");
if (_stricmp(strName.c_str(), "ProcessID") == 0)
{
processId = dataNode->GetText();
}
else if (_stricmp(strName.c_str(), "Application") == 0)
{
processName = dataNode->GetText();
}
else if (_stricmp(strName.c_str(), "SourceAddress") == 0)
{
sourceAddress = dataNode->GetText();
}
else if (_stricmp(strName.c_str(), "SourcePort") == 0)
{
sourcePort = dataNode->GetText();
}
else if (_stricmp(strName.c_str(), "DestAddress") == 0)
{
destAddress = dataNode->GetText();
}
else if (_stricmp(strName.c_str(), "DestPort") == 0)
{
destPort = dataNode->GetText();
}
else if (_stricmp(strName.c_str(), "Protocol") == 0)
{
protocol = dataNode->GetText();
}
dataNode = dataNode->NextSiblingElement("Data");
} while (dataNode);
std::cout << "\n\n=========================================" << std::endl;
std::cout << "Source:\t" << sourceAddress << "[" << sourcePort << "]" << std::endl;
std::cout << "Destination:\t" << destAddress << "[" << destPort << "]" << std::endl;
std::cout << "Protocol Code:\t" << protocol << std::endl;
std::cout << "Process Id:\t" << processId << std::endl;
std::cout << "Process Name:\t" << processName << std::endl;
std::cout << "=========================================\n\n" << std::endl;
}
}
void ReadLogsByEvtAPI()
{
DWORD dwEventLogBufferLen = MAX_EVENTLOG_READONCE;
LPBYTE pEventLogBuffer = new(std::nothrow) BYTE[MAX_EVENTLOG_READONCE];
if (pEventLogBuffer == NULL)
{
return;
}
ZeroMemory(pEventLogBuffer, MAX_EVENTLOG_READONCE);
std::wstring wstrQuery = std::wstring(
L""
L" "
L" "
L" "
L" ");
EVT_HANDLE hResult = EvtQuery(NULL, L"Security", wstrQuery.c_str(), EvtQueryChannelPath | EvtQueryReverseDirection);
if (hResult == NULL)
{
delete[] pEventLogBuffer;
pEventLogBuffer = NULL;
return;
}
while (true) // -_-
{
EVT_HANDLE hEventArrs[MAX_PATH] = { 0 };
DWORD dwReturnEvents = 0;
BOOL isSucc = EvtNext(hResult, MAX_PATH, hEventArrs, INFINITE, 0, &dwReturnEvents);
if (isSucc && dwReturnEvents)
{
for (int eventPos = 0; eventPos < dwReturnEvents; eventPos++)
{
DWORD dwBufferUsed = 0;
DWORD dwPropertyCount = 0;
int nBufferSize = dwEventLogBufferLen;
ZeroMemory(pEventLogBuffer, nBufferSize);
isSucc = EvtRender(NULL, hEventArrs[eventPos], EvtRenderEventXml, nBufferSize, pEventLogBuffer, &dwBufferUsed, &dwPropertyCount);
if (isSucc == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
delete[] pEventLogBuffer;
pEventLogBuffer = NULL;
dwEventLogBufferLen = dwBufferUsed + 2;
pEventLogBuffer = new(std::nothrow) BYTE[dwEventLogBufferLen];
if (pEventLogBuffer == NULL)
{
break;
}
nBufferSize = dwEventLogBufferLen;
ZeroMemory(pEventLogBuffer, dwEventLogBufferLen);
isSucc = EvtRender(NULL, hEventArrs[eventPos], EvtRenderEventXml, nBufferSize, pEventLogBuffer, &dwBufferUsed, &dwPropertyCount);
}
if (isSucc)
{
std::wstring strEventXML = std::wstring((wchar_t*)pEventLogBuffer);
parseEventXML(strEventXML);
}
}
}
}
if (pEventLogBuffer)
{
delete[] pEventLogBuffer;
pEventLogBuffer = NULL;
}
}
备注
实例代码中解析xml使用的是tinyxml2库,对应github地址为:https://github.com/leethomason/tinyxml2/tree/master
当前最新版(9.0.0)版本下载地址:https://download.csdn.net/download/QQ1113130712/88235095