How to Write Insecure Code | OWASP Foundation
How to Write Insecure Code Contributor(s): Jeff Williams, KristenS, Jarrod Stenberg, Jesse Ruderman, Shady, Myilmaz, kingthorin Introduction In the interest of ensuring that there will be a future for hackers, criminals, and others who want to destroy the digital future, this paper captures tips from the masters on how to create insecure code. With a little creative use of these tips, you can also ensure your own financial future.
Be careful, you don’t want to make your code look hopelessly insecure, or your insecurity may be uncovered and fixed. The idea for this article comes from Roedy Green’s How to write unmaintainable code . You may find the one page version more readable . Actually, making your code unmaintainable is a great first step towards making it insecure and there are some great ideas in this article, particularly the section on camouflage.
Also many thanks to Steven Christey from MITRE who contributed a bunch of particularly insecure items. Special note for the slow to pick up on irony set. This essay is a joke ! Developers and architects are often bored with lectures about how to write secure code. Perhaps this is another way to get the point across. General Principles Avoid the tools To ensure an application is forever insecure, you have to think about how security vulnerabilities are identified and remediated.
Many software teams believe that automated tools can solve their security problems. So if you want to ensure vulnerabilities, simply make them difficult for automated tools to find. This is a lot easier than it sounds. All you have to do is make sure your vulnerabilities don’t match anything in the tool’s database of signatures. Your code can be as complex as you want, so it’s pretty easy to avoid getting found.
In fact, most inadvertent vulnerabilities can’t be found by tools anyway. Always use default deny Apply the principle of “Default Deny” when building your application. Deny that your code can ever be broken, deny vulnerabilities until there’s a proven exploit, deny to your customers that there was ever anything wrong, and above all - deny responsibility for flaws. Blame the dirty cache buffers. Be a shark Always be on the move.
Leave security problems to operations staff. Complexity Distribute security mechanisms Security checks should be designed so that they are as distributed as possible throughout the codebase. Try not to follow a consistent pattern and don’t make it easy to find all the places where the mechanism is used. This will virtually ensure that security is implemented inconsistently. Spread the wealth Another great way to avoid being found is to make sure your security holes aren’t located in one place in your code.
It’s very difficult for analysts to keep all of your code in their head, so by spreading out the holes you prevent anyone from finding or understanding them. Use dynamic code The single best way to make it difficult for a security analyst (or security tool for that matter) to follow through an application and uncover a flaw is to use dynamic code. It can be almost impossible to trace the flow through code that is loaded at runtime.
Features like reflection and classloading are beautiful features for hiding vulnerabilities. Enable as many “plugin” points as possible. Secure Languages Type safety Means anything you enter at the keyboard is secure. Secure languages Pick only programming languages that are completely safe and don’t require any security knowledge or special programming to secure. Mix languages Different languages have different security rules, so the more languages you include the more difficult it will be to learn them all.
It’s hard enough for development teams to even understand the security ramifications of one language, much less three or four. You can use the transitions between languages to hide vulnerabilities too. Trust Relationships Rely on security checks done elsewhere It’s redundant to do security checks twice, so if someone else says that they’ve done a check, there’s no point in doing it again. When possible, it’s probably best to just assume that others are doing security right, and not waste time doing it yourself.
Web services and other service interfaces are generally pretty secure, so don’t bother checking what you send or what they return. Trust insiders Malicious input only comes from the Internet, and you can trust that all the data in your databases is perfectly validated, encoded, and sanitized for your purposes. Code wants to be free! Drop your source code into repositories that are accessible by all within the company.
This also prevents having to email those hard-coded shared secrets around. Logging Use your logs for debugging Nobody will be able to trace attacks on your code if you fill up the logs with debugging nonsense. Extra points for making your log messages undecipherable by anyone except you. Even more points if the log messages look as though they might be security relevant. Don’t use a logging framework The best approach to logging is just to write to stdout, or maybe to a file.
Logging frameworks make the logs too easy to search and manage to be sure that nobody will ever review them. Encryption Build your own encryption scheme The standard encryption mechanisms are way too complicated to use. It’s much easier to make up an encryption algorithm that scrambles up secret stuff. You don’t need to bother with a key or anything, just mix it all up and nobody will figure it out. Hard-code your keys Hard-coding is the best way to retain control.
This way those pesky operations folks won’t be able to monkey with (they say “rotate”) the keys, and they’ll be locked into the source code where only smart people can understand them. This will also make it easier to move from environment to environment as the hard-coded key will be the same everywhere. This in turn means your code is secure everywhere. Smart coders don’t read manuals Just keep adding bytes until the algorithm stops throwing exceptions.
Or just save yourself time and cut-and-paste from a popular coding site. This looks like a good start: byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; Pack up your sensitive payload If you need to pass things that are sensitive, like passwords or PII, just pack it up in an encrypted blob. Then just email the key to the developer who is coding the other side of the transaction, and punch out early.
This will also get you past Data Loss Prevention (DLP) tools which is a huge bonus. Encryption is hard A great way to improve the security posture of your site is to use client-side JavaScript to encrypt passwords and other sensitive data that are submitted. This can be used with or without HTTPS (TLS/SSL). If you use it without HTTPS, you can save some money by skipping the annual purchase of a signed third party certificate.
HTTPS makes your application bullet proof If you use HTTPS encryption between your web application endpoint and the client, there is absolutely no way for anyone to steal any of the data that is transmitted. Authentication Build your own authentication scheme Authentication is simple, so just use your common sense and implement. Don’t worry about esoteric stuff you hear about like session prediction, hashing credentials, brute force attacks, and so on.
Only NSA eggheads could possibly ever figure that stuff out. And if users want to choose weak passwords, that’s their own fault. Just use a hash Salt is for pigs. Its way too much work to salt and hash passwords. Besides, only the application can get to the database and hashes are one way trap doors, so why would you need it? Sessions are inherently secure Sessions timeout after 20 minutes, so don’t worry about them.
You can put the SESSIONID in the URL, log files, or wherever else you feel like. What
The rest of this page is on the live site.