Home > Articles > Programming > Windows Programming

Tipping the Scales Toward Secure Code

  • Print
  • + Share This
Everybody can use more secure code—and sometimes the best way to hone your skills is by listening to other programmers. Here are 18 tips offered by your fellow developers.
Like this article? We recommend

Tipping The Scales Toward Secure Code

By Rebecca Rohan
June 15, 2004

Everybody can use more secure code—and sometimes the best way to hone your skills is to listen to other programmers. Here are 18 concise tips offered by your fellow developers, each a specific (and opinionated!) piece of advice that you can put to work immediately. You may not agree with all these suggestions, but each is worth contemplating.

Be Careful What You Call

"If your application calls a DLL or another program, make sure you call it specifically," says Joshua Levy, senior software engineer for Reasoning Inc. in Mountain View, CA. "Don't rely on relative paths or search paths. The danger here is that you will end up calling a different program or DLL than you expected. This is the ultimate security flaw, because suddenly you're running a program or DLL written by the attacker! This is often a problem with 'inside jobs,' where an employee is trying to get more permissions than he should, or [to] read data or files that he isn't allowed to read. We review other people's code, and I see a huge amount of these sorts of errors." Levy writes programs to find and report such weaknesses. "It's better to check that the executable or DLL is what you expected, using signing or CRC," he suggests.

Security Focus

"Get someone to review your code specifically for security," reccomends Levy. "We've got huge QA departments that look for defects, but we don't have similar organizations that look for security vulnerabilities. Companies don't spend money unless they can see the value. When your security audit is done by a third party, you get the benefit of their expertise without the danger of your internal politics."

Church and State

"Code is secured by testing and review procedures — and the test-and-review team should be separate from the development team," says Paul Smith, CTO of PRRN Enterprise Solutions Group of Los Angeles, CA. "The objective is to ensure that only the code required to implement the function is included as part of the source, and to look for things like back doors, and things that don't relate to what the code is supposed to do."

How Authentic Are You?

"For any network application, authenticate early; authenticate often," says Dan Kaminsky, Senior Security Consultant for Avaya Inc.'s Enterprise Security Practice. "Every transaction needs to be tied to an authenticated context. As an attacker, the first thing I'll do is gauge the amount of functionality accessible without me possessing any credentials. I may have compromised another machine with the same software. I may know the underlying content of the site—and use this knowledge to expand my awareness of unauthenticated functionality exposed by the site. From there, my goal is to use the unauthenticated functionality as a backdoor to functionality that should require authentication."

Tough Love

"Don't trust the user," says Kaminsky. "Once they log in, watch for things they shouldn't do and lock them out." No letting them come at you again and again, trying sharper knives.

"[A hacker can] go to a Web site and click on all the buttons to see what they let [him] do," says Kaminsky. "Ninety-five percent of the buttons you need to log in require credentials. He finds something open, downloads the page, and sees something administrative—database setup scripts someone forgot to delete—a script to add a new user. He adds himself. He uses it to get around permissions."

Don't Let Your Cookies Crumble

"Encrypt your cookies, so that users can only reflect them back to you unchanged," says Kaminsky."Otherwise, malicious users can simply subtract from their given session ID and browse the resources available within other sessions."

Escape Evil Characters

"The key to sanitizing user input," says Anton Rager, Sr. Security Consultant and penetration tester for Avaya BCSI's Enterprise Security Practice, "is to make sure that you're escaping out evil characters that may be interpreted by the process of whatever you're sending them to. Make sure that characters that relate to the Web server or command processor or SQL server or URLs or Meta data tags, depending on what you're passing the data to, are trapped out. This requires some contextual thought."

Step Gingerly Around Your Own Feet

To make sure you're not overwriting any patches that were already installed, "Run Microsoft's Baseline Security Analyzer, a component on Microsoft's Web site for analysts and developers, while developing install scripts," recommends Robert Brown, owner of UltraTech Web Solutions of Fontana, CA.

Look Where You Are — and Where You Aren't

"Don't underestimate the environment where a program can be used," counsels Seth Fogie, VP of Product Management, Business Development and Marketing for Airscanner, Dallas, TX. "Even PDAs are susceptible to buffer overflow attack. There are programs that run on the PDA that allow a remote user to connect and execute their own code without any authorization or authentication problems. Trojans similiar to NetBus and Back Orifice are available on PDAs—and there are no indications that they're there," says Fogie. "You have to consider the environment you're programming for, consider the risks associated with the environment, consider how it could be used in an alternative environment, and how it could be abused in that."

Keep the Nuts Out

"Make sure the correct permissions are set on Internet directories so people can't download your code," says Marc Hansche, president of ACES, in San Marcos, California. Make files readable or writable with care. "Some people just set everything to 777 to make something work, and move on," says Hansche. It's the biggest hole in Web sites that you'll find." And disable directory browsing!

Don't Let Them Talk To Themselves

"If a malicious user is allowed to enter raw HTML tags and JavaScript, and this input is displayed unfiltered to other users, the malicious user will be able to steal other users' session information and obtain confidential data. This is a 'Cross Site Scripting' attack," advises Paul Keister, Principal of San Francisco-based PJPM.

Clear Is Not Cool

"Put the password in the database in encrypted form instead of plain text," says Yitzchak Ehrlich, principal of Dynamic Ventures Inc. of Cupertino, CA. "Encrypt information about how to access the database."

Do You Validate?

"Add a signature to configuration files to make sure they have not changed," says Ehrlich. "Validate data so the user only enters an expected range, instead of, say, coming to a field for their name and typing a command to the database to delete the table and set the password for the main user to be ABC." You need to protect against SQL injection.

War Room

"Sit down and approach it systemically," says David Randolph, president of Prairie Trail Software, Inc. in Plano, TX. "It's more important to say what kind of attacks are going to come in. For example, on a point to point basis, people are dialing in and using normal protocol: Don't use normal protocol! The first level protocol [that] people use is SLIP or PPP. I'm familiar with several other protocols that don't behave the same way as SLIP or PPP. What's the cost? What's the benefit? Provide sufficient challenges so that most of the people give up before they get to the stuff that's good."

Know Your Enemy — Don't Let Him Know You

"Remember, security is people," says Randolph. It's people designing code. It's people trying to break it. Hackers try things one place and try again." Once they break in one place, they learn things that make it easy for them to get around the next place with similar furnishings, so write in some speed bumps.

Watch Your Language!

"Don't use C++," says Bradford Bingel, Managing Director of ITM3, in Walnut Creek, CA. "If you were to save a character string into a variable, then later [you] came back and saved a larger character string into the same variable, you might inadvertently overwrite an unprotected area of memory," says Bingel. "That's a security issue. Today's programming languages don't have a lot of inherent security built into them. Java's an exception, as is .NET."

"A primary issue in writing a secure application is whether it uses a managed programming language or [if it's] non-managed," says Kevin Coyle, security consultant and principal of KJC.com, Dallas, TX. Managed examples are Java and C# instead of C and C++, in which you do your own memory management. This can lead to buffer overflows and stack overflows (you can use stack canaries), says Coyle, though you can have integer overflows in managed languages like Java.

"No" To the Flow

Coyle points out that tools, such as stack canaries in Visual Studio.NET and Linux StackGuard, can help with stack overflows, which have probably been a problem for twenty years. But a much more recent phenomenon, heap overflows, "are currently the most dangerous type of overflows, because the software allocates the memory dynamically from the heap, and hackers work on causing them." Coyle says libraries help; his personal recommendation is ElectricFence.

Rough Fuzzies

"People look for somebody to audit code, but they're hard to find, and it costs a lot so they end up doing it themselves," says Coyle. "There are tools to do fuzzing — program stress testing. Hackers don't have access to source [code], so they hammer away, trying to cause an error and find out the security implications." Fortunately, the fuzzing tool Spike is free.

Feel your security senses tingling? Your code will get tighter if you just keep thinking about this stuff and respecting the paranoiac within you!

  • + Share This
  • 🔖 Save To Your Account