Detecting Sensitive Info with OSSEC
OSSEC is one of those tools that continues to surprise me with its ability to perform low-level and important security tasks. In fine Unix tradition, individual parts of OSSEC can often be persuaded to do your bidding in ways not previously conceived. Put another way, it’s nice to hack. :)
Today, I’m going to show you how to use OSSEC to detect sensitive info within flat files. This example will demonstrate American social security numbers, but the same logic could be used to detect clear-text credit card numbers (a violation of the PCI DSS standard), or other personally identifiable information which should not be in clear-text.
We’re going to rely on the rootcheck functionality of OSSEC. Rootcheck does all kinds of nifty things, one of which is to scan within files for patterns. Normally, you would expect to look for things like insecure system configuration settings. We’re going to use it to find possible social security numbers.
First, create a test file containing a properly formatted SSN in a location that OSSEC is monitoring:
echo 123-45-6789 > /var/www/html/example.com/www/customer_data
Next, add the following to /var/ossec/etc/shared/system_audit_rcl.txt:
# Detect possible SSNs
[Possible Unencrypted Social Security Number Detected] [any] []
d:$web_dirs -> r:^\. -> r:\d\d\d-\d\d-\d\d\d\d;
Now, let’s create an alert so we get a heads up on what OSSEC finds (change the rule ID as needed):
<rule id=”100024″ level=”12″>
<if_sid>516</if_sid>
<match>Unencrypted Social Security Number</match>
<description>Possible Unencrypted Social Security Number Detected</description>
</rule>
Restart OSSEC:
/var/ossec/bin/ossec-control restart
Finally, we’ll initiate a scan on the local system:
/var/ossec/bin/agent_control -r -u 000
And here is what the resulting alert looks like:
OSSEC HIDS Notification.
2010 Jan 12 18:25:43
Received From: hostname->rootcheck
Rule: 100024 fired (level 12) -> “Possible Unencrypted Social Security Number Detected”
Portion of the log(s):System Audit: Possible Unencrypted Social Security Number Detected. File: /var/www/html/example.com/www/customer_data.
–END OF NOTIFICATION
One of the nice things about the alert above is that it doesn’t actually include the contents of the file, so you don’t have to worry about clear-text data traversing your mail server.
There are also a few caveats to keep in mind.
First, it is slightly prone to false-positives. Since the OSSEC regex library is designed for speed, rather than full regex support, it’s not possible to use something like \b to define a word boundary. This means that while the regex will match SSNs like 123-45-6789, it would also match something like 00123-45-678911. It also wouldn’t match a delimiter other than the dash. You might be able to mitigate some of these risks if you’re sure how the data would be formatted (but then again, if you’re sure you have clear-text data on the server why do you need this :) ?)
Second, it won’t find data in databases, which is probably where it is likely to be stored. It’s really only useful against flat-files.
Finally, there’s currently no facility to save this change when upgrading, such as you can already do with local_rules.xml. Make sure you have a backup of system_audit_rcl.txt handy that you can restore.
Despite these shortcomings, if you are already using OSSEC then this can be an excellent way to quickly add some rudimentary data checking to the OSSEC scans. It shouldn’t be relied on exclusively and the absense of an alert doesn’t necessarily mean you’re safe, but for a 5-minute change and in the absence of a budget for a more robust tool, this might lead to quite a surprise!
Thanks to Daniel Cid for help in getting this working, and of course for the excellent tool that OSSEC is.