Sitemap
Everything AppSec

I like AppSec, I work in AppSec and this is the place I write about AppSec

Log Driven Development

7 min readFeb 1, 2017

--

Talk to any security engineer and you’ll find that security engineers rarely if ever say no to more logs, John Lambert had this insight in his talk Changing the Physics of Defense:

If you went to a network defender team and you said “I’m gonna make compute and storage free for you”, you(network defenders) would defend your network in a fundamentally different way.

It turns out this doesn’t only apply to network defenders, in this blog post, I’ll discuss how logs help application security as well. Specifically, how you could use logs to gain more insights into the application and as a result, drive your application security development with logs.

Why do I need logs?

Alice recently joined a startup as their first application security engineer, after poking around, the login function caught her eyes:

At line 18, the function is checking a form field called VIP_KEY against a md5 hash and if it checks out, the application will recognize the user who’s trying to log in as a VIP user. Alice soon realized that this code could use some love because essentially anyone how happens to know about this magic value will become a VIP user. However, according to the comments above, this code path has been around for quite a while, even Alice’s colleagues are not sure if this code path is still used by users or not. Alice is now stuck with a dilemma:

On the one hand, Alice certainly doesn’t want to “just forget about it and move on because it could be risky”. After all, if you’re not paying the technical debt by fixing it now, you might find yourselves in the future paying the debt in a more dramatic way.

On the other hand “ignoring the fact that the feature might be active, just go ahead and delete this feature” is not a good idea either. If the feature was indeed used by users, Alice would find herself damaging the running business because of the refactoring. While in some circumstances we must make business impacting decisions in face of severe security risks, this is not one of them.

What’s the real problem here?

The problem really boils down to the lack of information, no one really knows if the VIP feature is still active or not and if it is, how many users are depending on it. Undoubtedly, logs will give visibility into the running state of the application across all live traffic, which makes it much easier for Alice to answer the above questions.

Come to think about it, logs really have some nice properties:

  1. Logs are easy to roll out. Usually just add a few lines of code and voila, you’re all set. Note that if it took more than a few lines of code or it took a while to add some new logs for your organization, it’s a good sign you should invest some time on improving the infrastructure and tooling around your log systems.
  2. Logs are of low risk. While it is true that if there were syntax errors in the code that introduced the new logs, the whole application would break, it could be easily avoided if you have a syntax checker anywhere in the deployment lifecycle before you push the code into production. Better yet, you could also run some linter and code formatter to make sure the code conforms to certain coding style.
  3. Logs doesn’t lie. Machines almost never make mistakes, many times I asked myself “how is this even possible, there must be something wrong with the computer”, and it always turned out that I was the one who got it wrong. If the logs say so, it must be the truth.
  4. Logs reflect what’s going on in the live traffic. Logs are useful for you only when they truly reflect the state of the live traffic, otherwise you won’t be able to make business decisions based on them and as a result, won’t be able to do anything meaningful for your business.

How do I drive development with logs?

After going through the above thought process, Alice decided to try out this log driven approach, now the question becomes, how could she drive the development with logs?

It turned out to be pretty straightforward. Instead of just removing the VIP feature and hoping for the best, Alice would just first augment the code with some logging in order to better understand users’ accessing pattern and once she gained more insights, it would be pretty easy to decide what to do next.

The first idea Alice has, is to add a log line if the VIP check succeeds. Not before long, Alice is able to come up with a code snippet like this:

In the above example, Alice first added a wrapper function that will augment the log line with some meta data like ip, timestamp, request_id etc. Then she added some code so that if any user indeed logged in using the hidden VIP_KEY, the application would generate a log line. So Alice would immediately found out that the VIP feature is still active if she saw any log line in ‘vip_log’.

Get Luqi Pan’s stories in your inbox

Join Medium for free to get updates from this writer.

Alice could do a little bit better though, if Alice indeed found some log lines in ‘vip_log’, signaling the VIP feature is still active, a natural followup she would have is: who are these VIP users and how many VIP users are there? With these questions in mind, she modified the login function slightly:

With a simple addition of ‘user_id’ field, Alice would be able to get an idea of who are the VIP users and how many are there. On the other hand, ‘user_id’ field on its own is not personal identifiable information, so Alice would not hurt users’ privacy with this addition.

“Getting an idea of how many users are VIP users is nice” Alice started thinking, “But wouldn’t it be even cooler if I could fathom what percentage of our user base are VIP users?”. Realizing that, Alice added another log line for non-VIP users as well:

This way, by comparing the number of log lines with ‘is_vip’ flag set to True and False respectively, Alice could figure out very easily what percentage of users are actually dependent on this VIP_KEY and come up with a solution going forward pretty easily. The percentage number being 0.5% might warrant a different engineering solution than, say, if the percentage number is 50%. If nothing else, the numbers will come in handy when communicating with product managers.

Alice was pretty happy with her work, so she opened up a pull request that proposed the above changes, her colleagues were very nice to get it reviewed and shipped very soon. Now it’s the perfect time to look at the logs.

Ref: http://valientmarketresearch.com/uncategorized/market-research-data-analysis-plan/

Let’s first figure out if the VIP feature is still in use, this is pretty easy, every time the VIP feature is indeed in use, Alice will see a log line with “is_vip” flag set to true. Even better, Alice could also deduct what percentage of users are actually VIP users as I explained above and then plan accordingly.

The opposite is a little bit tricky though. For Alice to determine that the VIP feature is not in use, she would need to find no log lines with “is_vip” flag set to True for a certain period of time, depending on the business requirement it might be 7 days or it might be 12 months . The good news is, once Alice is confident that it is not in use, she could just delete the VIP handling code all together without the fear of potentially damaging the business.

Are logs one-size-fits-all?

No, while logs are definitely a useful tool in your arsenal, you should be aware of following limitations:

  1. Privacy: be careful to avoid logging sensitive information such as personal identifiable information
  2. Performance: be aware of potentially high CPU/memory/disk usage

Conclusions

There are a lot of blog posts already on how important logs are for infrastructure security, incident response, malware analysis, etc. I hope after reading this blog post I’ve made it clear that logs are useful for application security development as well.

I used to dislike logs back when I started working in application security, they always take more time and I just wanted to ship my code change as soon as possible.

Ref: http://combiboilersleeds.com/keywords/soon-1.html

It was several breakages later that I gradually adopted the habit of shipping logs first. Logs make it a lot safer to push changes, and believe me, the last thing an application security engineer want to do is causing breakage. If a security team broke too many things, no one would like the team any more. Security team should add value to the business too, not damage it.

One way or other, logs will gain you more insights into the running application, which in turn would always produce actionable items for you to take on. With that you’ll be able to keep the ball rolling instead of contemplating and second-guessing “what the heck does this code do?”

Start driving your security development with logs, you’ll love it!

--

--

Everything AppSec
Everything AppSec

Published in Everything AppSec

I like AppSec, I work in AppSec and this is the place I write about AppSec