先貼上C#的原始碼

它的作用是到網域內其他的電腦上收Eventlog

 

///////////////////////////////////////////////////

protected void FetchEvt(object sender, EventArgs e)
{
string queryString = "*[System[EventID=4647 or EventID=4768] and System/TimeCreated[@SystemTime >= '2015-12-01T09:46:30.000Z']]";
SecureString pw = new SecureString();
foreach (char c in "p@ssw0rd")
{
pw.AppendChar(c);
}

EventLogSession session = new EventLogSession(
"VLEJXE0VHI8OPOF", // Remote Computer
"SYSTEXWARES", // Domain
"administrator", // Username
pw,
SessionAuthentication.Default);

pw.Dispose();

EventLogQuery query = new EventLogQuery("Security", PathType.LogName, queryString);
query.Session = session;

try
{
EventLogReader logReader = new EventLogReader(query);
EventRecord log = logReader.ReadEvent();
var result = new List<EventRecord>();
while (log != null)
{
result.Add(log);
if (result.Count >= 100)
{
break;
}
log = logReader.ReadEvent();
}
}
catch (EventLogException ex)
{
Console.WriteLine("Could not query the remote computer! " + ex.Message);
return;
}
}

///////////////////////////////////////////////////

 

我先在事件檢視器的畫面幾個對應的地方標上數字

這樣比較好說明

2015-12-11_163526_结果  

 

來解釋程式重要的地方吧

string queryString = "*[System[EventID=4647 or EventID=4768] and System/TimeCreated[@SystemTime >= '2015-12-01T09:46:30.000Z']]";

這裡的字串內容, 指的就是事件檢視器裡的篩選條件, 也就是相當於按下圖片第4點的地方

在事件檢視器圖片第3點的地方, 就是需要篩選的來源資料

假設我們需要撈出EventID是4647的檔案, 此時條件可以寫成 System/EventID=4647, 外面再包上 *[]即可

這裡的* 其實是root的意思, 包上去後, 條件就長得像這樣 *[System/EventID=4647]

假設我們要搜尋兩個條件其中一個即可, 就會變成 *[System/EventID=4647 or System/EventID=4768] 

由於這兩個條件一樣都是在System底下, 因此可以改寫成 *[System[EventID=4647 or EventID=4768] 

也就是利用方括弧來表示System下的標籤操作

另外 System/TimeCreated[@SystemTime >= '2015-12-01T09:46:30.000Z'] 這一行中的@SystemTime

對應到的就是TimeCreated標籤中的SystemTime 屬性, 上述的語法指的只找大於上述時間的日期

 

在來我們需要連線到目標電腦的資訊

EventLogSession session = new EventLogSession(

需在上面的物件指名 電腦名稱, 連線帳號, 帳號的domain, 密碼

這裡的密碼須加密, 而加密的程式區段在 

SecureString pw = new SecureString();
foreach (char c in "p@ssw0rd")
{
pw.AppendChar(c);
}

接下來建立query物件

EventLogQuery query = new EventLogQuery("Security", PathType.LogName, queryString);
query.Session = session;

物件中提到的 "Security" 就是 事件檢視器圖片第1點的地方

其實指的就是實際事件檢視檔的檔案路徑

C:\Windows\System32\winevt\Logs\Security.evtx

如果要看其他的檢視紀錄, 就改檔案名稱就可以了

EventLogReader logReader = new EventLogReader(query);
EventRecord log = logReader.ReadEvent();

連線至遠端電腦

var result = new List<EventRecord>();
while (log != null)
{
result.Add(log);
if (result.Count >= 100)
{
break;
}
log = logReader.ReadEvent();
}

利用while把事件檢視器圖片第2點的紀錄, 存到 result 中

這裡的 100 指的是先抓 100 筆就好

抓完後看result [0].properties中的內容, 就是事件檢視器圖片第3點

在來擷取我們要的資訊就可以了

 

arrow
arrow
    文章標籤
    eventlog remote
    全站熱搜

    海勒姆 發表在 痞客邦 留言(0) 人氣()