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

Run Snort and Wazuh Tests Using PowerShell and Testinfra #6

Merged
merged 60 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
7634544
refactor: Update snort.ps1 to launch Snort with the default network i…
bengo237 Sep 5, 2024
476883a
refactor: Changed from Out-File -Append to Set-Content for better con…
bengo237 Sep 5, 2024
bac7503
refactor: Update snort.ps1 to use Invoke-WebRequest for downloading f…
bengo237 Sep 5, 2024
b491741
refactor: Update snort.ps1 to download files using Invoke-WebRequest …
bengo237 Sep 5, 2024
6ff9630
refactor: Update snort.ps1 to use curl.exe for downloading files
bengo237 Sep 5, 2024
cfa7554
refactor: Update snort.ps1 to use Invoke-WebRequest for downloading f…
bengo237 Sep 5, 2024
74ada00
refactor: Update snort.ps1 to use Out-File instead of Set-Content for…
bengo237 Sep 5, 2024
2804cd0
refactor: Update snort.ps1 to use Out-File instead of Set-Content for…
bengo237 Sep 5, 2024
07a4548
feat : add snort.conf for windows OS
bengo237 Sep 5, 2024
af13ce6
refactor: Update snort.ps1 to install Snort, Npcap, and WinPcap
bengo237 Sep 5, 2024
eed8335
refactor: Update snort.ps1 to remove WinPcap installation
bengo237 Sep 5, 2024
69f3e88
refactor: Remove WinPcap installation from Install-Snort function
bengo237 Sep 5, 2024
6c7e305
refactor: Update snort.ps1 to use UTF-8 encoding when writing rules t…
bengo237 Sep 5, 2024
a2e2dfd
refactor: Update snort.ps1 to use UTF-8 encoding when writing rules t…
bengo237 Sep 5, 2024
01c4fdf
refactor: Remove WinPcap installation from Install-Snort function
bengo237 Sep 5, 2024
0b2f2fc
refactor: Update snort.ps1 to replace snort.conf file
bengo237 Sep 5, 2024
22a7f0d
refactor: Update snort.conf output settings
bengo237 Sep 5, 2024
8933c0d
feat: add snort local.rules
bengo237 Sep 5, 2024
a68f555
refactor: Update snort.ps1 to replace snort.conf file
bengo237 Sep 5, 2024
aa14353
refactor: Update snort.ps1 to update URLs for local.rules and snort.conf
bengo237 Sep 5, 2024
9dcf64e
refactor: Remove temporary directory after installation
bengo237 Sep 5, 2024
91dff1e
refactor: Remove temporary directory after installation
bengo237 Sep 6, 2024
1dfaa7b
refactor: Update snort.ps1 to use correct log location
bengo237 Sep 9, 2024
b9d985f
refactor: Update readme
bengo237 Sep 9, 2024
348cae2
refactor: Update snort rules and priorities
bengo237 Sep 10, 2024
c087806
refactor: Update snort.conf and arpspoof configuration
bengo237 Sep 10, 2024
608ca71
refactor: Update dynamic detection directory in snort.conf
bengo237 Sep 10, 2024
f26ae18
refactor: Register Snort as a scheduled task to run at startup
bengo237 Sep 11, 2024
3e112b9
refactor: Update snort.ps1 to use hidden task settings when registeri…
bengo237 Sep 11, 2024
df550e1
refactor: Update pytest command in pytests.yml to run test_linux.py i…
bengo237 Sep 11, 2024
73d34bc
refactor: Add Windows tests for Snort installation and configuration
bengo237 Sep 11, 2024
afb9669
refactor: Add Windows tests for Snort installation and configuration
bengo237 Sep 11, 2024
13b228f
refactor: Update test_windows.py with string decoding
bengo237 Sep 11, 2024
9d7804a
refactor: Update test_windows.py to include string decoding for varia…
bengo237 Sep 11, 2024
1cd6b41
refactor: Update test_windows.py to include string decoding for varia…
bengo237 Sep 11, 2024
270bf27
refactor: Update test_windows.py to include string decoding for varia…
bengo237 Sep 11, 2024
64dc8d3
refactor: Update test_windows.py to include string decoding for varia…
bengo237 Sep 11, 2024
4426c74
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
358f588
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
1befa70
refactor: Update pytests.yml to fix path issue for running test_windo…
bengo237 Sep 11, 2024
f6ade2f
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
1bdd384
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
c79c468
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
5f02569
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
9ade4eb
Refactor pytests.yml and test_windows.py for Snort installation on Wi…
bengo237 Sep 11, 2024
a370c2f
refactor: Update pytests.yml and test_windows.py for Snort installati…
bengo237 Sep 11, 2024
0994c44
Refactor pytests.yml and test_windows.py for Snort installation on Wi…
bengo237 Sep 11, 2024
08774f4
Refactor pytests.yml and test_windows.py for Snort installation on Wi…
bengo237 Sep 11, 2024
89db3c9
Refactor pytests.yml and test_windows.py for Snort installation on Wi…
bengo237 Sep 11, 2024
1417222
Refactor Snort installation on Windows scripts
bengo237 Sep 11, 2024
3736df5
refactor: Update Snort test script for Windows
bengo237 Sep 11, 2024
ad4b83a
refactor: Update Snort test script for Windows
bengo237 Sep 11, 2024
59db57b
refactor: Update Snort test script for Windows
bengo237 Sep 11, 2024
4a44c75
refactor: Update Snort test script for Windows
bengo237 Sep 11, 2024
28095bf
Refactor Snort test script for Windows
bengo237 Sep 11, 2024
ff10dd7
Refactor Snort test script for Windows
bengo237 Sep 11, 2024
6b7b38a
refactor: Update Snort test script for Windows
bengo237 Sep 11, 2024
546a30a
chore: update readme for tests instructions
bengo237 Sep 11, 2024
7b3292c
Merge branch 'main' into snortwin
bengo237 Sep 11, 2024
5ca7735
chore: Update Snort test script for Windows
bengo237 Sep 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions .github/workflows/pytests.yml

This file was deleted.

70 changes: 70 additions & 0 deletions .github/workflows/snort-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Run Snort tests

on: [push, pull_request]

jobs:
run-tests:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest] # Operating systems

steps:
- name: Checkout code
uses: actions/checkout@v2

# Linux-specific steps
- name: Set up Python (Linux)
if: matrix.os == 'ubuntu-latest'
uses: actions/setup-python@v2
with:
python-version: '3.9'

- name: Install dependencies (Linux)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y curl gnupg2 iproute2
pip install pytest pytest-testinfra

- name: Install wazuh-agent (Linux)
if: matrix.os == 'ubuntu-latest'
run: |
curl -s https://packages.wazuh.com/key/GPG-KEY-WAZUH | sudo apt-key add -
echo "deb https://packages.wazuh.com/4.x/apt/ stable main" | sudo tee /etc/apt/sources.list.d/wazuh.list
sudo apt-get update
sudo apt-get install -y wazuh-agent

- name: Run Snort install script (Linux)
if: matrix.os == 'ubuntu-latest'
run: |
sudo bash scripts/install.sh

# Windows-specific steps
- name: Install Wazuh agent (Windows)
if: matrix.os == 'windows-latest'
run: |
Invoke-WebRequest -Uri https://packages.wazuh.com/4.x/windows/wazuh-agent-4.9.0-1.msi -OutFile $env:TEMP\wazuh-agent.msi
msiexec.exe /i $env:TEMP\wazuh-agent.msi /qn WAZUH_MANAGER='10.0.0.1' WAZUH_AGENT_NAME='test'

- name: Install Snort (Windows)
if: matrix.os == 'windows-latest'
run: |
Start-Process -FilePath "powershell.exe" -ArgumentList "-File $env:GITHUB_WORKSPACE/scripts/snort.ps1" -Verb RunAs -Wait

# Running tests
- name: Run tests (Linux)
if: matrix.os == 'ubuntu-latest'
run: |
sudo $(which pytest) -vv scripts/tests/test_linux.py

- name: List files (Windows)
if: matrix.os == 'windows-latest'
run: |
dir $env:GITHUB_WORKSPACE\scripts

- name: Run tests (Windows)
if: matrix.os == 'windows-latest'
run: |
pwsh -ExecutionPolicy Bypass -File $env:GITHUB_WORKSPACE/scripts/tests/test.ps1
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Snort3 Docker Multi Arch](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/snort-build.yml/badge.svg)](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/snort-build.yml)[![Build and Package Snort 3](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/package-snort.yml/badge.svg)](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/package-snort.yml)[![Run Pytest](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/pytests.yml/badge.svg)](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/pytests.yml)
[![Build Snort3 Docker Multi Arch](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/snort-build.yml/badge.svg)](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/snort-build.yml)[![Build and Package Snort 3](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/package-snort.yml/badge.svg)](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/package-snort.yml)[![Run Snort-tests](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/pytests.yml/badge.svg)](https://github.com/ADORSYS-GIS/wazuh-snort/actions/workflows/pytests.yml)

# Wazuh Snort
This repository contains several resources for installing and configuring Snort, as well as its integration with Wazuh. Here is a detailed description of each item:
Expand All @@ -25,7 +25,7 @@ This repository contains several resources for installing and configuring Snort,
- `README.md`: This file provides general information about the project.
- `rules`: This folder contains the rules for configuring Snort.
- `scripts`: This folder contains a script for installing and configuring Snort on Linux and MacOS. It also includes a README with instructions for building and packaging Snort 3 using GitHub Actions.
- `scripts/tests`: Additionally, for details on testing with Pytest, see [scripts/tests/README.md](scripts/tests/README.md)
- `scripts/tests`: Additionally, for details on testing with Pytest and Powershell, see **[scripts/tests/README.md](scripts/tests/README.md)**

## Getting Started
### Prerequisites
Expand All @@ -34,10 +34,7 @@ This repository contains several resources for installing and configuring Snort,
### Installation
Install using this command:
```bash
curl -SL --progress-bar https://raw.githubusercontent.com/ADORSYS-GIS/wazuh-snort/main/scripts/install.sh -o install.sh
chmod +x install.sh
sudo ./install.sh

sudo curl -SL https://raw.githubusercontent.com/ADORSYS-GIS/wazuh-snort/main/scripts/install.sh | bash
```

## Description
Expand Down
59 changes: 42 additions & 17 deletions scripts/tests/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Run Pytest

This repository features a GitHub Actions workflow that automatically executes Pytest tests for your project. The workflow performs several tasks: it checks out your code, sets up the environment, installs required dependencies, and runs tests to verify that Snort and Wazuh are properly configured.
# Run Snort Tests Using PowerShell and Testinfra

This repository features a GitHub Actions workflow that automatically executes tests for your project. The workflow performs several tasks: it checks out your code, sets up the environment, installs required dependencies, and runs tests to verify that Snort and Wazuh are properly configured on both Linux and Windows.

## Testing

The tests use `pytest` and `testinfra` to verify system configuration. Here’s a summary of the tests:
The tests use `pytest`, `testinfra`, and PowerShell to verify system configuration. Here’s a summary of the tests:

### Dependencies Installation

- **`install_dependencies` fixture**: Installs necessary dependencies based on the operating system (Ubuntu/Debian or Alpine). If the OS is not supported, the test fails.
- **`install_dependencies` fixture**: Installs necessary dependencies based on the operating system (Ubuntu/Debian or Windows). If the OS is not supported, the test fails.

### Snort Installation

Expand All @@ -26,33 +25,59 @@ The tests use `pytest` and `testinfra` to verify system configuration. Here’s
To ensure the GitHub Actions workflow runs properly, you need to have:

- Python 3.9 or the version specified in the `setup-python` action.
- A script located at `scripts/install.sh` for installing Snort.
- The `testinfra` Python package for running system checks.
- For Linux:
- A script located at `scripts/install.sh` for installing Snort.
- For Windows:
- A PowerShell script located at `scripts/snort.ps1` for installing and configuring Snort and Wazuh agent.
- A PowerShell test script located at `scripts/tests/test.ps1` for running tests.

### Running Tests Locally

To run the tests locally, follow these steps:

1. Install the required Python packages:
1. **Install the required Python packages:**
```bash
pip install pytest pytest-testinfra
```

2. Ensure that all system dependencies are installed. the wazuh agent should be already installed:
2. **Ensure that all system dependencies are installed and wazuh-agent have to be installed already:**
```bash
sudo apt-get update
sudo apt-get install -y curl gnupg2 iproute2
```

3. Run the tests:
```bash
pytest -vv scripts/tests/test_install.py
```
3. **For Windows:**
- Ensure you have PowerShell installed and configured.
- Run the following commands:
```powershell
# Install required packages
pip install pytest pytest-testinfra

# Run the Snort and Wazuh installation and configuration script
.\scripts\snort.ps1

# Run the tests
.\scripts\tests\test.ps1
```

4. **For Linux:**
- Run the tests with:
```bash
pytest -vv scripts/tests/test_install.py
```

### PowerShell Test Script

The `scripts/tests/test.ps1` PowerShell script includes various tests to ensure that Snort and Wazuh are correctly installed and configured. Here’s an overview of what the script does:

- **Initialize a list to store test results**.
- **Define a function `Run-Test`** to run each test and log the results.
- **Define test scripts** to check the existence of Snort and Wazuh files, verify configurations, and check environment variables.
- **Run all defined tests** and output the results.




## Contributing

If you’d like to contribute to this project, please follow the standard GitHub flow: fork the repository, create a feature branch, commit your changes, and submit a pull request.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
94 changes: 94 additions & 0 deletions scripts/tests/test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Initialize a list to store test results
$testResults = @()

# Function to run a test and log the result
function Run-Test {
param (
[string]$TestName,
[scriptblock]$TestScript
)

try {
& $TestScript
$testResults += [pscustomobject]@{ TestName = $TestName; Status = "Passed" }
} catch {
$testResults += [pscustomobject]@{ TestName = $TestName; Status = "Failed"; Error = $_.Exception.Message }
}
}

# Define the test scripts
$tests = @{
"SnortInstalled" = {
if (Test-Path "C:\Snort\bin\snort.exe") {
Write-Host "Snort is installed."
} else {
throw "Snort is not installed."
}
}
"NpcapInstalled" = {
if (Test-Path "C:\Program Files\Npcap") {
Write-Host "Npcap is installed."
} else {
throw "Npcap is not installed."
}
}
"RulesDirectoryExists" = {
if (Test-Path "C:\Snort\rules") {
Write-Host "Rules directory exists."
} else {
throw "Rules directory does not exist."
}
}
"LocalRulesFileExists" = {
if (Test-Path "C:\Snort\rules\local.rules") {
Write-Host "Local rules file exists."
} else {
throw "Local rules file does not exist."
}
}
"SnortConfFileExists" = {
if (Test-Path "C:\Snort\etc\snort.conf") {
Write-Host "Snort configuration file exists."
} else {
throw "Snort configuration file does not exist."
}
}
"OssecConfFileExists" = {
if (Test-Path "C:\Program Files\Wazuh\ossec.conf") {
Write-Host "OSSEC configuration file exists."
} else {
throw "OSSEC configuration file does not exist."
}
}
"SnortConfigInOssecConf" = {
if (Select-String -Path "C:\Program Files\Wazuh\ossec.conf" -Pattern "snort") {
Write-Host "Snort is configured in OSSEC configuration."
} else {
throw "Snort is not configured in OSSEC configuration."
}
}
"EnvironmentVariables" = {
if ([System.Environment]::GetEnvironmentVariable("SNORT_PATH") -ne $null) {
Write-Host "SNORT_PATH environment variable is set."
} else {
throw "SNORT_PATH environment variable is not set."
}
}
"ScheduledTaskRegistered" = {
$task = Get-ScheduledTask | Where-Object { $_.TaskName -eq "SnortTask" }
if ($task) {
Write-Host "Scheduled task is registered."
} else {
throw "Scheduled task is not registered."
}
}
}

# Run all tests
foreach ($test in $tests.GetEnumerator()) {
Run-Test -TestName $test.Key -TestScript $test.Value
}

# Output the test results
Write-Host "Test Results:"
$testResults | Format-Table -AutoSize
File renamed without changes.
Binary file added scripts/windows/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions scripts/windows/local.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2001-2013 Sourcefire, Inc. All Rights Reserved.
#
# This file contains (i) proprietary rules that were created, tested and certified by
# Sourcefire, Inc. (the "VRT Certified Rules") that are distributed under the VRT
# Certified Rules License Agreement (v 2.0), and (ii) rules that were created by
# Sourcefire and other third parties (the "GPL Rules") that are distributed under the
# GNU General Public License (GPL), v2.
#
# The VRT Certified Rules are owned by Sourcefire, Inc. The GPL Rules were created
# by Sourcefire and other third parties. The GPL Rules created by Sourcefire are
# owned by Sourcefire, Inc., and the GPL Rules not created by Sourcefire are owned by
# their respective creators. Please see http://www.snort.org/snort/snort-team/ for a
# list of third party owners and their respective copyrights.
#
# In order to determine what rules are VRT Certified Rules or GPL Rules, please refer
# to the VRT Certified Rules License Agreement (v2.0).
#
#-------------
# LOCAL RULES
#-------------
alert icmp any any -> any any (msg:"ICMP connection attempt:"; sid:1000010; rev:1; priority:3;)
alert icmp any any -> 8.8.8.8 any (msg:"Pinging..."; sid:1000004; priority:3;)
alert tcp any any -> any 6667 (msg:"IRC protocol traffic"; sid:1000005; priority:3;)
alert tcp any 6667 -> any any (msg:"C&C Server sent netinfo command"; content:"!netinfo"; sid:1000006; priority:4;)
alert tcp any 6667 -> any any (msg:"C&C Server sent sysinfo command"; content:"!sysinfo"; sid:1000007; priority:5;)
alert tcp any 6667 -> any any (msg:"C&C Server sent port scan command"; content:"!scan"; sid:1000008; priority:3;)
alert tcp any 6667 -> any any (msg:"C&C Server sent download command"; content:"!download"; sid:1000009; priority:7;)
Loading
Loading