In this article we show how to configure an alert that fires whenever someone starts an ssh session to a remote machine from a Bastion Host using ssh. We then show how to build a context query, to list all the commands run on the remote machine, by that user, during the ssh session.
1. Add a Javascript event rule to capture the ssh session:
Query
|
const events = [];
const desc = 'User sudo ssh from bastion host';
records.forEach(record => { |
Note the "value" field in the above rule = "uname + time" so that each alert value is unique and thus sent to our SIEM.
2. Add a corresponding alert rule (make sure Event code trigger matches the code used in the creation of the event rule). The boilerplate Javascript for this alert rule is:
Query
|
const alerts = []; |
3. Add the following context query to the alert rule:
target_hostname | TEXT |
alertTime | DATETIME |
pid | NUMBER |
assetId | TEXT |
Query
|
with ssh_session as ( |
Now we will test by logging into our Bastion Host (draco) and starting an ssh session to a remote machine (uptycs-centos1). Note osquery needs to be installed on both machines for this use case to succeed.
Once logged into the remote machine, we run a few commands...
Check Uptycs for the alert:
Click on the "Alert Detail" column for that alert row...
...then click on the "COMMANDS FOR SSH SESSION" context query...
...to get the list of commands run on the remote host (during the ssh session):