0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(弁護士向け).evtxファイルからOfficeソフトの動作を抽出する

Posted at

約1年ほど前に,.evtxファイルをPythonで解析するという記事を書きましたが,ざっくり言って,その応用編といいますか。

弁護士が裁判で労働時間を立証する際に使う…かどうかはともかくとして,Windowsのシステムログを記録したEVTX (Windows XML Event Log)ファイルから,なんか労働してそうなデータを抽出したい場合があろうかと思います。(どうやらEVTXファイルってのはWin7での形式だそうで,OSのバージョンによって違う形式なんでしょうけども,まぁ,考え方はそう大きく変わらないかと思います。)
事業所から持ち出さないパソコンであれば起動から終了まででよさそうですが,PCの持ち出しを許可されていたりすると,休日のPC起動が仕事なのか私用なのか必ずしも判然としません。
でも,Officeソフトを使っていればお仕事っぽいかもしれない。
ということで,前回の記事から引き続き,Python-Evtxを使ってOfficeソフトを使ってそうなイベントログを抽出してみましょう。

考え方はだいたい前回の記事と一緒なので,そちらをご参照。
で,Data[@Name=ProcessName]またはData[@Name=NewProcessName]にどんなプログラムが出てくるかをスクリプト書いてリストアップしてみたところ,どうやらOfficeという文字列が含まれていたらOfficeソフトが動いてそうな雰囲気がしました。
ということで,ざっくりこんな感じのコードを作って,$ python ExtractOffice.py EventLog.evtxみたいな感じにすると,Events_office.tsvってファイルを吐き出してくれます。

ExtractOffice.py
import Evtx.Evtx as evtx
from lxml import etree

schema = "http://schemas.microsoft.com/win/2004/08/events/event"

def main():
    f = open("events_office.tsv", "w") #File名直打ち(汗
    import argparse
    
    parser = argparse.ArgumentParser(
        description="Dump a binary EVTX file into XML.")
    parser.add_argument("evtx", type=str,
                        help="Path to the Windows EVTX event log file")
    args = parser.parse_args()
    
    #EVTXファイルの操作
    with evtx.Evtx(args.evtx) as log:
        counter = 0 #進捗報告用
        for record in log.records():
            elm = record.lxml()
            #進捗報告
            counter += 1
            if counter % 1000 == 0:
                print("Now on record:"+str(counter))
            
            pn = elm.xpath("//event:Data[@Name='ProcessName']", namespaces={"event":schema})
            npn = elm.xpath("//event:Data[@Name='NewProcessName']", namespaces={"event":schema})
            pnt="" #ProcessNameのデフォルト値を""にしとく
            npnt="" #NewProcessNameの(略)
            try: #なんか失敗キャストが発生する場合があるのでtry
                if ("Office" in pn[0].text): #ここで文字列検索
                    pnt = pn[0].text
            except:
                pnt = ""
            try:
                if "Office" in npn[0].text:
                    npnt = npn[0].text
            except:
                npnt = ""
            
            if ( len(pnt) or len(npnt) ):
                print(
                    elm.xpath("//event:EventID", namespaces={"event":schema})[0].text
                    +"\t"+
                    elm.xpath("//event:TimeCreated", namespaces={"event":schema})[0].get("SystemTime")
                    +"\t"+pnt
                    +"\t"+npnt
                , file=f)
        print(counter) #終わったときに,イベント数を書き出す。
    f.close()


if __name__ == "__main__":
    main()

そんでもって,吐き出されたEvents_office.tsvをExcelで読み込んで適当に整形して(→たとえばこんな感じ(別記事)),適当にまとめると,それっぽくなるんじゃないでしょうか。

若干の補足説明

EVTXファイルはイベント(Record)ごとにXMLになっていて,//Event/EventData/下のDataエレメントたちは,どうもEventの種類によって一定しないみたいです。
が,ProcessNameNewProcessNameのどちらかがあれば,何となく,どのプログラムからログが吐き出されたのかがわかりそうな感じ。
なので,このいずれかを補足するようにしてみたっちゅうわけです。
同内容の処理をProcessNameとNewProcessNameに行っているので,関数として外出しするリファクタリングをしといたほうが良いのは分かっとるのです…。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?