In this lab, we will explore how we can integrate Splunk and Python. Whilst Splunk provides sophisticated means of analysis through the Splunk Query Language, the ability to integrate Python allows for even greater control of how we analyse and process our data. We can then make use of the extensive data science libraries in Python to perform further analytics.
Useful links:
General Guidance to Setup with DetectionLab instance
You can use the following examples to link up your UWEcyber VM with your DetectionLab logger machine that has an instance of Splunk running on it. This may be useful if you wish to obtain data from your DetectionLab to explore further in Python and Pandas.
python3 -m pip install splunk-sdk
vagrant ssh logger
to log into the logger machine via ssh.sudo nano server.conf
allowRemoteLogin = always
under the [general] section of the configuration file. Save the file with Ctrl+S and exit with Ctrl+X.sudo ./splunk restart
to restart Splunk. If asked for a password for user vagrant, type vagrant
.exit
to exit the ssh session.Basics of cURL usage for Splunk
https://docs.splunk.com/Documentation/SplunkCloud/8.2.2112/RESTTUT/RESTsearches
curl -u admin:changeme -k https://192.168.56.105:8089/services/search/jobs -d search="search *"
. If the above is configured correctly, then your default credentials will allow you to get a XML response that contains a search ID. You can then follow this up for more information.import splunklib.client as client
HOST = "192.168.56.105"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"
# Create a Service instance and log in
service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD)
# Print installed apps to the console to verify login
for app in service.apps:
print (app.name)
import pandas as pd
kwargs_oneshot = {"earliest_time": "2021-10-1T12:00:00.00", "latest_time": "2022-10-1T12:00:00.00"}
searchquery_oneshot = "| tstats count WHERE index=* by index"
oneshotsearch_results = service.jobs.oneshot(searchquery_oneshot, **kwargs_oneshot)
def splunk_to_pandas(reader):
d = []
for item in reader:
entry = {}
for i in item:
entry[i] = item[i]
d.append(entry)
d = pd.DataFrame(d)
return d
reader = results.ResultsReader(oneshotsearch_results)
d = splunk_to_pandas(reader)
d
import splunklib.results as results
kwargs_oneshot = {"earliest_time": "2021-10-1T12:00:00.00", "latest_time": "2022-10-1T12:00:00.00"}
searchquery_oneshot = "search index=suricata | head 10"
oneshotsearch_results = service.jobs.oneshot(searchquery_oneshot, **kwargs_oneshot)
reader = results.ResultsReader(oneshotsearch_results)
d = splunk_to_pandas(reader)
d
import splunklib.results as results
kwargs_oneshot = {"earliest_time": "2021-10-1T12:00:00.00", "latest_time": "2022-10-1T12:00:00.00"}
searchquery_oneshot = "search index=zeek | head 10"
oneshotsearch_results = service.jobs.oneshot(searchquery_oneshot, **kwargs_oneshot)
reader = results.ResultsReader(oneshotsearch_results)
d = splunk_to_pandas(reader)
d