Facebook SDK

Monday, December 30, 2013

Programming Security-Part 2 (CERT Secure Programming Practices)


Because some people like lists, here is a list of the top 10 recommended, language-neutral, secure coding practices (adopted from https://www.securecoding.cert.org/confluence/display/seccode/Top+10+Secure+Coding+Practices).

1.       Validate input. As mentioned in my first security blog post, validating input can eliminate the vast majority of software vulnerabilities. External data sources you are using, e.g. pulling from databases, using APIs, etc., have the capability of having malformed objects or “inappropriate” syntax designed to break your system.

2.       Use compiler warnings. When using compiled code (as opposed to interpreted code like Python), ensure the compiler is set to generate the highest warning levels and don’t ignore the warnings when compiling. By eliminating the warnings, you are ensuring that security vulnerabilities don’t exist in your code, to the best of your ability.

3.       Design based on security policies. Develop your software architecture and develop the code base around established security policies. For example, design your system to have independent subsystems that can communicate with each other; each subsystem has a different privilege level so the highest privilege isn’t being used all the time. A similar example of this is that Windows XP and older versions defaulted to giving each user administrator privileges, making it easy for malware to execute.

4.       KISS (Keep It Simple, Stupid). The more complex you design something, the more likely errors and vulnerabilities will creep in. If your programming language allows for subroutines, subprocesses, modules, etc., then use them to break up the program into smaller parts. It is easier to troubleshoot and debug these smaller sections than try to work with a monolithic program.

5.       Deny by default. When setting permissions, assume access is denied until proven otherwise. Also, ensure that conditions are identified for when access is permitted.

6.       Use the concept of least privilege. Much like #5 above, every process should be executed with the minimum privileges required to complete the job. If a process has to have a higher level of access, that access should be removed as soon as possible. This removes many of the avenues malicious attacks can use for privilege escalation.

7.       Sanitize data transfers. When passing data to other subsystems, such as databases, other programs, command shells, etc., sanitize the data first. Unused functionality within these other systems can be attacked through a number of vectors.  Sanitizing your data can remove some of these vectors, as your program knows the context of the data transfer; the called system doesn’t know anything about the transfer and will accept whatever it is given.

8.       Practice defense in depth. Use multiple defensive measures so attacks have to circumvent a variety of countermeasures in order to run. For example, use a sandbox environment (like a virtual machine) when testing unknown code to minimize the risk of damaging your system.

9.       Make use of quality assurance testing. Good QA can identify and remove vulnerabilities. When possible, have someone else look at your code; the programmer may become so used to looking at the code that he or she may miss something obvious. Automated tools can quickly find common errors while audits can track frequent problems and provide better education.

10.   Create a secure coding standard. Develop and implement a secure standard for programmers, taking into consideration the programming language(s) used and the target platforms.

11.   Define security requirements. Identify security requirements early in the development cycle and, whenever changes are made to the development plan, ensure the changes are vetted against the requirements.

12.   Use threat modeling. Anticipate possible threats to the software and develop mitigation strategies to address these threats. Identify key assets of the software and system, decompose the application, categorize threats, and then rate the threats.

Saturday, December 28, 2013

Programming Security-Part 1 (General Thoughts)

As part of thinking about updates to my book, if I ever get around to making another revision, I've thought that a chapter on programming security would be good. It is also helpful that part of the requirements to maintain my Security+ certification can be met by writing security-oriented blog posts. Thus, I will start writing a number of posts about programming security in general and how Python deals with some of these issues, if it does. If I ever make a new version of my book, I will include these posts into a new chapter. Hopefully this information will be useful.

The big thing to remember when it comes to programming security is that it just takes one mistake to create a security vulnerability. While Python alleviates a lot of the problems other languages have, e.g. memory allocation, it doesn't mean the Python is invulnerable. In fact, one of the points against Python is the fact that it isn't compiled; because Python programs are frequently provided in their raw .py files, anyone can open them up and view or modify them. Thus, if a person looks at the file for nefarious purposes, it's very easy for him or her to identify vulnerabilities that the programmer left in the code.

Another problem with Python is the incompatibility between Python 2.x and Python 3.x. If a programmer is converting a file from 2.x to 3.x, and not using the 2to3 converter (or doesn't look at the resulting code), there is a chance that vulnerability snuck through.

Secure programming, regardless of the language, involves some assumptions on the part of the programmer. Essentially, the programmer never assumes the system is in a particular state or that functions, methods, libraries, etc. will work as advertised. The programmer attempts to handle all possible errors and conditions within the code.

For example, buffer overflows occur when too much data is put into a buffer (a portion of memory). If the buffer fills up, any additional data spills over into adjacent memory areas, potentially causing system malfunctions, memory corruption, or exploiting the system. C and C++ are famous for buffer overflows, as they have no inherent protection against accessing or overwriting data in any memory location and do not automatically check the input data against the boundaries of a buffer.

As a programmer, you should develop the habit of validating any input data, whether it be ensuring the type of data (character strings vs. numbers), the length of the data (ensuring too much data isn't put into a buffer), allowed characters, etc. Many SQL attacks are performed due to poor or non-existent data checking.

One of the reasons there are so many languages available for programming nowadays is because of the inherent lack of security in C and C++. For decades, those languages dominated programming; they are still the go-to choice for many programmers, especially for low-level work on hardware devices, such as video cards. Newer languages, like Java, Python, Ruby, or the various .NET flavors have some safety features built-in.


One example is garbage collection. Traditional C/C++ programmers have to be aware of memory allocation and ensure that, once a data object is no longer in use, all references to it are removed from memory. If not, the memory remains allocated for that non-existent object and can't be used for another purpose. If this happens too often, eventually there will be no memory available for legitimate data objects and the program fails, the system crashes, or other problems arise. If a malware writer wanted to create a denial of service, simply providing a program that eats up memory will do it; once the system or program crashes, no one can use it until it is reset.