-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cyberphor
committed
Oct 13, 2024
1 parent
74d076a
commit 1368705
Showing
16 changed files
with
734 additions
and
423 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
cov.xml | ||
dist/ | ||
docs/_build | ||
backup/ | ||
backup/ | ||
dataset/ | ||
rules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,23 @@ | ||
![Tests](https://github.com/cyberphor/pySigma-backend-powershell/actions/workflows/test.yml/badge.svg) | ||
![Coverage Badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/cyberphor/d3f7db7182e7819f3748e64a2ab2d126/raw/cyberphor-pySigma-backend-powershell.json) | ||
![Status](https://img.shields.io/badge/Status-pre--release-orange) | ||
# pySigma Powershell Backend | ||
![Status](https://img.shields.io/badge/Status-pre--release-orange) | ||
|
||
# pySigma PowerShell Backend | ||
The pySigma PowerShell backend uses [pySigma](https://github.com/SigmaHQ/pySigma) to convert [Sigma rules](https://github.com/SigmaHQ/sigma) into PowerShell queries. It was designed to be used in conjunction with the [Soap](https://github.com/cyberphor/Soap) PowerShell module (i.e., the `Read-WinEvent` function). | ||
|
||
## Overview | ||
The pySigma PowerShell backend includes two Python packages: | ||
* `sigma.pipelines.powershell`: normalizes Sigma rules for PowerShell. | ||
* `sigma.backends.powershell`: declares the `PowerShellBackend` class and multiple output methods. | ||
|
||
It currently supports the following output formats: | ||
- [x] default: plain PowerShell queries | ||
- [ ] script: a PowerShell script | ||
- [ ] xml: XML documents | ||
- [ ] xpath: XML strings | ||
- [ ] subscription: Windows event subscriptions | ||
The pySigma PowerShell Backend converts Sigma rules into PowerShell-based queries. It was designed to be used in conjunction with the the [`Read-WinEvent`](/scripts/Read-WinEvent.ps1) filter. | ||
|
||
## Usage | ||
**Step 1.** After downloading this repository, install this Python-based project using `poetry`. | ||
```bash | ||
poetry run python sigma2powershell.py -p rules/ | ||
poetry install | ||
``` | ||
|
||
## Testing | ||
```python | ||
python -m pip install --user pytest | ||
python -m pytest # test all functions | ||
python -m pytest tests/test_backend_powershell.py::test_powershell_and_expression # test a specific function | ||
**Step 2.** Next, use the provided PowerShell script to import the `Read-WinEvent` filter. You will need to do this everytime you start a new PowerShell session (pro-tip: add this filter to your PowerShell profile). | ||
```bash | ||
./scripts/Read-WinEvent.ps1 | ||
``` | ||
|
||
## Updating to the Latest Version of pySigma | ||
```python | ||
python -m poetry add pysigma@latest | ||
**Step 3** Convert whatever Sigma rules you have to PowerShell queries. | ||
```bash | ||
sigma2powershell -r rules/demo.yml | ||
``` | ||
|
||
## References | ||
* [Understanding XML and XPath by the Microsoft Scripting Guy, Ed Wilson](https://devblogs.microsoft.com/scripting/understanding-xml-and-xpath/) | ||
## Copyright | ||
This project is licensed under the terms of the [MIT license](/LICENSE). |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[virtualenvs] | ||
in-project = true | ||
path = ".venv" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
filter Read-WinEvent { | ||
<# | ||
.EXAMPLE | ||
Get-WinEvent -FilterHashTable @{LogName="Security";Id=4625} | Read-WinEvent | Select-Object -Property TimeCreated,Hostname,TargetUserName,LogonType | Format-Table -AutoSize | ||
TimeCreated TargetUserName LogonType | ||
----------- -------------- --------- | ||
9/12/2021 8:23:27 AM Victor 2 | ||
9/12/2021 8:23:27 AM Victor 2 | ||
9/12/2021 7:49:37 AM Victor 2 | ||
9/12/2021 7:49:37 AM Victor 2 | ||
#> | ||
$WinEvent = [ordered]@{} | ||
$XmlData = [xml]$_.ToXml() | ||
$SystemData = $XmlData.Event.System | ||
$SystemData | | ||
Get-Member -MemberType Properties | | ||
Select-Object -ExpandProperty Name | | ||
ForEach-Object { | ||
$Field = $_ | ||
if ($Field -eq 'TimeCreated') { | ||
$WinEvent.$Field = Get-Date -Format 'yyyy-MM-dd HH:mm:ss K' $SystemData[$Field].SystemTime | ||
} elseif ($SystemData[$Field].'#text') { | ||
$WinEvent.$Field = $SystemData[$Field].'#text' | ||
} else { | ||
$SystemData[$Field] | | ||
Get-Member -MemberType Properties | | ||
Select-Object -ExpandProperty Name | | ||
ForEach-Object { | ||
$WinEvent.$Field = @{} | ||
$WinEvent.$Field.$_ = $SystemData[$Field].$_ | ||
} | ||
} | ||
} | ||
$XmlData.Event.EventData.Data | | ||
ForEach-Object { | ||
$WinEvent.$($_.Name) = $_.'#text' | ||
} | ||
return New-Object -TypeName PSObject -Property $WinEvent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Downloads a dataset.""" | ||
|
||
from os import remove, rename | ||
from requests import session | ||
from zipfile import ZipFile | ||
|
||
EXIT_SUCCESS = 0 | ||
DATASET = "dataset.zip" | ||
URL = "https://github.com/sbousseaden/EVTX-ATTACK-SAMPLES/archive/refs/heads/master.zip" | ||
|
||
|
||
def main() -> int: | ||
"""Downloads a dataset.""" | ||
with session() as client: | ||
repo = client.get(URL) | ||
with open(DATASET, "wb") as download: | ||
download.write(repo.content) | ||
with ZipFile(DATASET, "r") as dataset: | ||
dataset.extractall(".") | ||
rename("EVTX-ATTACK-SAMPLES-master", "dataset") | ||
remove(DATASET) | ||
return EXIT_SUCCESS | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from shutil import move | ||
from urllib.request import urlretrieve | ||
from glob import glob | ||
from os import remove | ||
from zipfile import ZipFile | ||
import shutil | ||
|
||
|
||
def main(): | ||
urlretrieve( | ||
"https://github.com/SigmaHQ/sigma/archive/refs/heads/master.zip", "tmp.zip" | ||
) | ||
with ZipFile("tmp.zip", "r") as zf: | ||
zf.extractall("tmp") | ||
for rule_dir in glob("tmp/sigma-master/rule*"): | ||
move(rule_dir, "rules") | ||
remove("tmp.zip") | ||
shutil.rmtree("tmp") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from .powershell import PowerShellBackend | ||
|
||
# TODO: add all backend classes that should be exposed to the user of your backend in the import statement above. | ||
|
||
backends = { # Mapping between backend identifiers and classes. This is used by the pySigma plugin system to recognize backends and expose them with the identifier. | ||
backends = { # Mapping between backend identifiers and classes. This is used by the pySigma plugin system to recognize backends and expose them with the identifier. | ||
"powershell": PowerShellBackend, | ||
} | ||
} |
Oops, something went wrong.