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

Add new output module for Splunk #1091

Merged
merged 6 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
59 changes: 59 additions & 0 deletions bbot/modules/output/splunk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from bbot.core.errors import RequestError

from bbot.modules.output.base import BaseOutputModule


class Splunk(BaseOutputModule):
watched_events = ["*"]
meta = {"description": "Send every event to a splunk instance through HTTP Event Collector"}
options = {
"url": "",
"hectoken": "",
"index": "",
"source": "",
"timeout": 10,
}
options_desc = {
"url": "Web URL",
"hectoken": "HEC Token",
"index": "Index to send data to",
"source": "Source path to be added to the metadata",
"timeout": "HTTP timeout",
}

async def setup(self):
self.url = self.config.get("url", "")
self.source = self.config.get("source", "bbot")
self.index = self.config.get("index", "main")
self.timeout = self.config.get("timeout", 10)
self.headers = {}

hectoken = self.config.get("hectoken", "")
if hectoken:
self.headers["Authorization"] = f"Splunk {hectoken}"
if not self.url:
return False, "Must set URL"
if not self.source:
self.warning("Please provide a source")
return True

async def handle_event(self, event):
while 1:
try:
data = {
"index": self.index,
"source": self.source,
"sourcetype": "_json",
"event": event.json(),
}
await self.helpers.request(
url=self.url,
method="POST",
headers=self.headers,
json=data,
raise_error=True,
)
break
except RequestError as e:
self.warning(f"Error sending {event}: {e}, retrying...")
await self.helpers.sleep(1)
58 changes: 58 additions & 0 deletions bbot/test/test_step_2/module_tests/test_module_splunk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import json
import httpx

from .base import ModuleTestBase


class TestSplunk(ModuleTestBase):
downstream_url = "https://splunk.blacklanternsecurity.fakedomain:1234/services/collector"
config_overrides = {
"output_modules": {
"splunk": {
"url": downstream_url,
"hectoken": "HECTOKEN",
"index": "bbot_index",
"source": "bbot_source",
}
}
}

def verify_data(self, j):
if not j["source"] == "bbot_source":
return False
if not j["index"] == "bbot_index":
return False
data = j["event"]
if not data["data"] == "blacklanternsecurity.com" and data["type"] == "DNS_NAME":
return False
return True

async def setup_after_prep(self, module_test):
self.url_correct = False
self.method_correct = False
self.got_event = False
self.headers_correct = False

async def custom_callback(request):
j = json.loads(request.content)
if request.url == self.downstream_url:
self.url_correct = True
if request.method == "POST":
self.method_correct = True
if "Authorization" in request.headers:
self.headers_correct = True
if self.verify_data(j):
self.got_event = True
return httpx.Response(
status_code=200,
)

module_test.httpx_mock.add_callback(custom_callback)
module_test.httpx_mock.add_callback(custom_callback)
module_test.httpx_mock.add_response()

def check(self, module_test, events):
assert self.got_event == True
assert self.headers_correct == True
assert self.method_correct == True
assert self.url_correct == True
19 changes: 19 additions & 0 deletions docs/scanning/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ output_modules:
password: P@ssw0rd
```

### Splunk

The `splunk` output module sends [events](events.md) in JSON format to a desired splunk instance via [HEC](https://docs.splunk.com/Documentation/Splunk/9.2.0/Data/UsetheHTTPEventCollector).

You can customize this output with the following config options:

```yaml title="~/.bbot/config/bbot.yml"
output_modules:
splunk:
# The full URL with the URI `/services/collector/event`
url: https://localhost:8088/services/collector/event
# Generated from splunk webui
hectoken: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
# Defaults to `main` if not set
index: my-specific-index
# Defaults to `bbot` if not set
source: /my/source.json
```

### Asset Inventory

The `asset_inventory` module produces a CSV like this:
Expand Down
Loading