8

Coding4Fun - Pktmon + PowerShell 自製簡易 Windows 遠端存取警報器

 2 years ago
source link: https://blog.darkthread.net/blog/pktmon-ps-alarm/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Pktmon + PowerShell 自製簡易 Windows 遠端存取警報器-黑暗執行緒

先聲明,本次程式的趣味性大於實用性,請抱著好玩心情欣賞勿認真計較。

這陣子我學會了幾項新技能:

突發奇想,我如果用 PowerShell 串接 Pktmon 的即時輸出,在有人試圖存取我的網路分享資料夾或想遠端管理我的電腦時發出警報呢?差不多像下面這種用鐵罐簡易 DIY 的入侵偵測警報:

thumbnail
圖片來源

運作原理是 Windows 網路分享、遠端管理介面會走 Microsoft-DS TCP 445 Port (延伸閱讀:開啟 CIFS / SMB / UNC 連線應開放哪些 Ports 與注意事項 by 保哥),是攻擊者的熱門目標。Pktmon 除了抓封包寫檔案還有個即時監控模式,pktmon -c -m real-time,啟用後會源源不絕輸出捕追到的封包資訊,直到按下 Ctrl-C 中斷為止。

Pktmon 輸出的封包資訊如下,其中有段 192.168.50.9.51061 > 192.168.50.7.445 可用來識別來源及目的IP:

12:20:21.293786700 PktGroupId 29,PktNumber 1,外觀 1,方向 Rx ,類型 乙太網路 ,元件 18,邊緣 1,篩選器 1,OriginalSize 66,LoggedSize 66
        00-15-5D-54-95-27 > BC-A8-28-82-52-52, ethertype IPv4 (0x0800), length 66: 192.168.50.9.51061 > 192.168.50.7.445: Flags [S], seq 1563724667, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
12:20:21.293924600 PktGroupId 30,PktNumber 1,外觀 1,方向 Tx ,類型 乙太網路 ,元件 18,邊緣 1,篩選器 1,OriginalSize 66,LoggedSize 66
        BC-A8-28-82-52-52 > 00-15-5D-54-95-27, ethertype IPv4 (0x0800), length 66: 192.168.50.7.445 > 192.168.50.9.51061: Flags [S.], seq 4017831713, ack 1563724668, win 65535, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
12:20:21.294368500 PktGroupId 31,PktNumber 1,外觀 1,方向 Rx ,類型 乙太網路 ,元件 18,邊緣 1,篩選器 1,OriginalSize 54,LoggedSize 54
        00-15-5D-54-95-27 > BC-A8-28-82-52-52, ethertype IPv4 (0x0800), length 54: 192.168.50.9.51061 > 192.168.50.7.445: Flags [.], ack 4017831714, win 513, length 0
12:20:21.295264500 PktGroupId 32,PktNumber 1,外觀 1,方向 Rx ,類型 乙太網路 ,元件 18,邊緣 1,篩選器 1,OriginalSize 213,LoggedSize 128
        00-15-5D-54-95-27 > BC-A8-28-82-52-52, ethertype IPv4 (0x0800), length 213: 192.168.50.9.51061 > 192.168.50.7.445: Flags [P.], seq 1563724668:1563724827, ack 4017831714, win 513, length 159
12:20:21.295845100 PktGroupId 33,PktNumber 1,外觀 1,方向 Tx ,類型 乙太網路 ,元件 18,邊緣 1,篩選器 1,OriginalSize 506,LoggedSize 128
        BC-A8-28-82-52-52 > 00-15-5D-54-95-27, ethertype IPv4 (0x0800), length 506: 192.168.50.7.445 > 192.168.50.9.51061: Flags [P.], seq 4017831714:4017832166, ack 1563724827, win 4106, length 452

由以上原理,我們可以啟動 Pktmon 即時監聽 445 Port,寫一段 PowerShell 用 Pipeline 接收 Pktmon 輸出結果,發現有人試圖存取時顯示對方 IP。不過每次存取的封包不只一個,一個封包喊一次會把人逼瘋,故我設計成每三分鐘每個 IP 只警告一次。程式邏輯蠻單純的,前面已奠定知識基礎,邏輯想完程式也差不多寫完了:

pktmon filter remove
pktmon filter add -t tcp -p 445 
Add-Type -AssemblyName PresentationFramework
# 每次存取包含多個封包,每個 IP 每 3 分鐘只告警一次,用 Hashtable 管理
$nextAlarmTimes = @{}
$ready = $false
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
. cmd /c "pktmon start -c -m real-time --comp 180" | ForEach-Object {
    if (!$ready) {
        $_
        if ($_.Contains('...')) {
            $ready = $true
        }
    }
    else {
        $m = [regex]::Match($_, '(?<s>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{5} > \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.445')
        if ($m.Success) {
            $srcIp = $m.Groups['s'].Value
            $nextAlarmTime = $nextAlarmTimes[$srcIp]
            if (!$nextAlarmTime -or $nextAlarmTime.CompareTo([DateTime]::Now) -le 0) {
                $nextAlarmTimes[$srcIp] = [datetime]::Now.AddMinutes(3)
                # 發出警告
                [System.Windows.MessageBox]::Show("$([datetime]::Now.ToString('HH:mm:ss')) 偵測到來自 $srcIp 的活動") | Out-Null
            }
        }
    }
}

實際測試一下,大成功!!

and has 1 comment

Comments

# 2021-09-15 10:15 AM by Jason Lo

試過後回報幾個問題: 1、pktmon start -c 後面不指定參數會有問題,我直接指定網卡ID就正常了,不過查文件卻又說可以不指定,預設是所有網卡(ALL)。 2、-m的部分也需要修改 3、因為有調整執行powershell的權限,所以執行時我要改成管理員權限才可以正常執行,所以要注意powershell執行權限。 4、版大的設計在整體資安考量上,還可以再擴充,算是蠻有趣的嘗試,雖然還是習慣用wireshark查網路問題,不過就像版大說的,總會遇到不方便使用wireshark的時候,不過如果公司有預算,會比較建議全面佈建MDR,看起來會比較清楚,因為有些攻擊要看行為模式才能認定。

Post a comment

Comment
Name Captcha 70 - 50 =

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK