Week 3: BitNinja Alerts on Slack or Discord with Custom NodeJS Webhook
This third week, we will be digging into the advanced area or deepwater, where we will be setting up our own NodeJS webhook. The best thing about it, you can really customize it and the data received nearly instantly. You may clone the GitHub repository and use it for your own, but note that this has limited support by me and I also rather recommend to extend it to your needs.
Server Requirements
Please note that the above requirements for a very few alerts, in case there is more incidents and alerts incoming, this will be more resource-heavy.
Software/Package Stack
Setting up the Workspace
sudo apt update && sudo apt install nodejs npm git
2. Git clone "ninjahook"
git clone https://github.com/sqpp/ninjahook
3. Install the required packages
npm install
4. Copy the .env.example to .env and edit using
cp .env.example .env && nano .env
Since we don't want people to bug or make requests to our API randomly, we will create an environment variable with our own API key, plus of course the Discord and Slack API keys that we will be using to send the messages in.
APP_NAME=Ninjahook
WEBHOOK_KEY=
DISCORD_CH=
SLACK_CH=alerts
PORT=8012
DISCORD_TOKEN=
SLACK_TOKEN=
Additionally, you can generate any key such as a 12 format random hash, etc, but make sure it's secure and only you and BitNinja Alerts Page has it. Also make sure the port you set here is open in your firewall on the server, as BitNinja will need to send POST requests to it.
You can use Strong Random Password Generator for this, but make sure to uncheck the symbols.
IMPORTANT
In case you don't want to use both of these communication channels, leave the environment variable empty. Additionally the support for the "WHM" will only work if the ServerName from BitNinja matches with the HostName of the server.
Discord.js Setup
Let's head over to Discord Developers page and create a New Application.
Once you receive the Token, make sure to copy it and add it to the .env file after DISCORD_TOKEN=
Slack Setup
BitNinja setup
https://<ip-address/domain>:8012/malware-alert?key=<your-api-key
https://<ip-address/domain>:8012/waf-alert?key=<your-api-key>
https://<ip-address/domain>:8012/dos-alert?key=<your-api-key>>
(Optional) You can of course use domains as well for prettier webhook URLs.
Start the Webhook Service
npm run start
Once the service is up and events being triggered, you will see the notifications with the matching AlertIDs in the logs.
Alerts
Discord
Slack
[2023-05-05T08:53:05.578Z] Discord Notification | 641fb9b4893a80002a48100
[2023-05-05T08:53:06.489Z] Slack Notification | 641fb9b4893a80002a4810011
Senior Backend Engineer (Go, PHP, AWS, Azure) | Software Architect | Observability Specialist
1 年Wow :O. Nice writing ??. I always felt the need to, how can one show how awesome BitNinja is? More integrations, tutorials, and of course public GitHub repos. All my sweat spots.