Skip to content

Instantly share code, notes, and snippets.

@deekayen
Last active March 3, 2020 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deekayen/fd60aafe81e801372f9e15965f98afad to your computer and use it in GitHub Desktop.
Save deekayen/fd60aafe81e801372f9e15965f98afad to your computer and use it in GitHub Desktop.
Curl the Redlock Prism Cloud API to get alerts from last week then POST them into a Splunk HTTP event collector for alternate archival and analysis. Note the date calculation appends 3 digits of milliseconds from microtime for a total 13 digital epoch value.
#!/bin/bash
# For Mac, date is BSD based. We like GNU date parsing,
# which comes with sideloading date as a renamed gdate util.
# Use Homebrew to install some utilities: https://brew.sh
# brew install coreutils jq
######################
# SET VALUES FOR THESE
# Prisma Cloud config
API="api2.prismacloud.io"
# update the api key in the JWT call
# Splunk config
HEC="http://splunk.example.com:8088/services/collector"
TOKEN="ce40f9de-asdf-asdf-adsf-d80ce1add1ef"
# Note this is in millisecond format with three extra digits of microtime.
START=$(gdate -d '2 weeks ago Sunday 00:00:00' +%s000)
END=$(gdate -d '1 week ago Saturday 23:59:59' +%s999)
# END EDITABLE VARIABLES
########################
echo "Logging in..."
JWT="$(curl -s --request POST \
--url https://${API}/login \
--header 'accept: application/json; charset=UTF-8' \
--header 'content-type: application/json; charset=UTF-8' \
--data '{"username":"76a0d9c6-asdf-asdf-asdf-6a928a492a93","password":"v2gST9ktKbgPiDfrlsJilIXvFxg="}' \
| jq '.["token"]' \
| tr -d '"')"
echo "GETing alerts for UNIX millisecond epoch ${START} to ${END}..."
curl -s --request GET \
--url "https://${API}/alert?detailed=true&timeType=absolute&startTime=${START}&endTime=${END}" \
--header 'accept: */*' \
--header "x-redlock-auth: ${JWT}" \
> "prisma_alerts_${START}_${END}.json"
LINES=$(cat "prisma_alerts_${START}_${END}.json" | jq -r '.[] | @base64' | wc -l | xargs)
echo "Found ${LINES} alerts..."
# Filesize
du -sh "prisma_alerts_${START}_${END}.json"
# jq outputs prettified JSON over multiple lines.
# Doing a base64 encode brings it back to a single line string
# so that we can treat split elements the same as loop rows.
for row in $(cat "prisma_alerts_${START}_${END}.json" | jq -r '.[] | @base64'); do
_decode() {
echo ${row} | base64 --decode | jq -r ${1}
}
printf "\nPOSTing $(_decode .'id') to Splunk HTTP Event Collector..."
echo $TIME
curl -k "${HEC}" \
-H "Authorization: Splunk ${TOKEN}" \
-d "{\"time\": $(_decode .'alertTime'), \"sourcetype\": \"_json\", \"event\": $(_decode)}"
done
@deekayen
Copy link
Author

deekayen commented Mar 3, 2020

This version is designed to run on a Macbook, which uses slightly different utiltities and versions than what you might find on EL7.

To seed data from the start of the account, the bottom half might look something more like this:

echo "GETing all alerts up to last week..."
curl -s --request GET \
  --url "https://api2.prismacloud.io/alert?detailed=true&timeType=absolute&startTime=1&endTime=${END}" \
  --header 'accept: */*' \
  --header "x-redlock-auth: ${JWT}" \
  -o "prisma_alerts_1_${END}.json"

LINES=$(cat "prisma_alerts_1_${END}.json" | jq -r '.[] | @base64' | wc -l | xargs)
echo "Found ${LINES} alerts..."

# Filesize
du -sh "prisma_alerts_1_${END}.json"

# jq outputs prettified JSON over multiple lines.
# Doing a base64 encode brings it back to a single line string
# so that we can treat split elements the same as loop rows.
for row in $(cat "prisma_alerts_1_${END}.json" | jq -r '.[] | @base64'); do
    _decode() {
     echo ${row} | base64 --decode | jq -r ${1}
    }

   printf "\nPOSTing $(_decode .'id') to Splunk HTTP Event Collector..."
   curl -k "${HEC}" \
    -H "Authorization: Splunk ${TOKEN}" \
    -d "{\"time\": $(_decode .'alertTime'), \"sourcetype\": \"_json\", \"event\": $(_decode)}"
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment