defensive coding techniques pt 2
play

Defensive Coding Techniques (Pt. 2) Engineering Secure Software - PowerPoint PPT Presentation

Defensive Coding Techniques (Pt. 2) Engineering Secure Software Last Revised: September 25, 2020 SWEN-331: Engineering Secure Software Benjamin S Meyers 1 Last Time... Always code defensively Validating input Principles


  1. Defensive Coding Techniques (Pt. 2) Engineering Secure Software Last Revised: September 25, 2020 SWEN-331: Engineering Secure Software Benjamin S Meyers 1

  2. Last Time... Always code defensively Validating input ● ● Principles Sanitizing input ● ● Writing insecure code is easy ○ Exception handling ● Maintainability still counts ○ Know thy API’s ○ Subclassing ● ○ Complexity is the enemy of security Immutability ● Don’t be paranoid ○ Tree of knowledge ○ Concurrency ● ● public static final SWEN-331: Engineering Secure Software Benjamin S Meyers 2

  3. Cloning is Insecure (and Medically Unethical!) Every Java object has a clone() method ● Shallow Copy: Obj Obj2 = Obj1.clone() ○ ObjA Obj1 Val SWEN-331: Engineering Secure Software Benjamin S Meyers 3

  4. Cloning is Insecure (and Medically Unethical!) Every Java object has a clone() method ● Shallow Copy: Obj Obj2 = Obj1.clone() ○ ObjA Obj1 Obj2 Val SWEN-331: Engineering Secure Software Benjamin S Meyers 4

  5. Cloning is Insecure (and Medically Unethical!) Every Java object has a clone() method ● Shallow Copy: Obj Obj2 = Obj1.clone() ○ What happens when: Obj1.ObjA.value = “blah” ○ ObjA Obj1 Obj2 Val SWEN-331: Engineering Secure Software Benjamin S Meyers 5

  6. Cloning is Insecure (and Medically Unethical!) Every Java object has a clone() method ● Deep Copy ○ Object Serialization ■ Defensive Copying ■ Copy Constructors ■ Factory Methods ■ ObjA ObjB Obj1 Obj2 ValA ValB SWEN-331: Engineering Secure Software Benjamin S Meyers 6

  7. Cloning is Insecure (and Medically Unethical!) Every Java object has a clone() method ● Deep Copy ○ Object Serialization ■ public final class Galaxy { Defensive Copying ■ // Regular Constructor Copy Constructors ■ public Galaxy(Double mass, String name) { this.mass = mass; Factory Methods ■ this.name = name; } // Copy Constructor public Galaxy(Galaxy galaxy) { this(galaxy.getMass(), galaxy.getName()); } public Double getMass() { return mass; } public String getName() { return name; } } SWEN-331: Engineering Secure Software Benjamin S Meyers 7

  8. Cloning is Insecure (and Medically Unethical!) Every Java object has a clone() method ● Often error-prone ○ Doesn’t do what you think it does ○ Most people don’t abide by the contract ○ Even the Java architects don’t like it ● The Java Language Secure Coding Guidelines from Oracle ○ recommend not using java.lang.Cloneable entirely Use your own copy mechanism if needed ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 8

  9. Don’t Be a Serial Killer! Serialization is often unnecessary; difficult to get right ● Deserializing is essentially constructing an object without ● executing the constructor If you use it, don’t assume the constructor will be executed ○ Can reverse-engineer to violate constructor post-conditions ○ Complex input! ○ Note: serialized != encrypted ● Confidentiality disclosure ○ Use transient for variables that don’t need serialization ○ e.g. environment info, timestamps, keys ■ SWEN-331: Engineering Secure Software Benjamin S Meyers 9

  10. Memory Organization Assumptions Don’t rely upon the memory organization of the compiler/OS ● e.g. C-code: ● char a=5; char b=3; *(&a+1)=0; /* b is now 0 */ /* this may work, but not advisable */ Lots of problems with this ● Compilers change ○ OS’s change ○ Development environment vs. customer environment ○ Really difficult to debug ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 10 10

  11. Dead Store Removal Don’t leave sensitive data sitting in memory longer than needed ● Hibernation features dump RAM to HDD ○ Segfault → core dump → passwords! ○ The following is usually a good idea… ● void GetData(char *MFAddr) { char pwd[64]; if (GetPasswordFromUser(pwd, sizeof(pwd))) { if (ConnectToMainframe(MFAddr, pwd)) { /* Interact with mainframe */ } memset(pwd, 0, sizeof(pwd)); /* Clear password */ } } … BUT C++ .NET and GCC 3.X will optimize away that last call, since ● pwd is never used again Some libraries have specific functions to avoid such ○ optimizations, e.g. explicit_bzero on OpenBSD SWEN-331: Engineering Secure Software Benjamin S Meyers 11 11

  12. Environment & File Confusion In C/C++, the putenv() and getenv() methods vary between OS ● Change depending on the compiler and platform ○ Sometimes case-sensitive, sometimes not ○ An attacker can add an environment variable (e.g. to his own ○ JVM) that overrides yours putenv(“TEST_ENV=foo”); putenv(“Test_ENV=bar”); const char *temp = getenv(“TEST_ENV”); if (temp == NULL) { /* Handle error */ } printf(“%s\n”, temp); /* “foo” on Linux, “bar” on Windows */ Same with file names in Windows (insensitive) and Linux ● Do not rely on case sensitivity when interacting with platform ● SWEN-331: Engineering Secure Software Benjamin S Meyers 12 12

  13. Native Wrappers If you use another language, you inherit all of the risks in that ● language e.g. Java Native Interface (JNI) can execute a C program with a ○ buffer overflow Also: treat native calls as external entities ● Perform input validation and sanitization ○ Loaded at runtime → spoofing opportunity ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 13 13

  14. Watch Character Conversions Most apps require Internationalization (I18N) in some form ● Need to convert one character set to another for translation ○ When apps get big, I18N is usually an afterthought ○ Not all character sets are the same size! ● Assume 4-bytes for a character? Buffer overrun on Chinese chars ○ Not every byte maps to a character ○ Sometimes multiple bytes map to a single character ○ Recommendations ● Use unicode (UTF-8 or UTF-16) ○ Don’t write your own converters ○ Check: web servers, database systems, command inputs ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 14 14

  15. Let Your GET Mean GET HTTP protocols have different actions ● GET -- for retrieving data (typically) ○ POST, DELETE, etc. -- modify stuff ○ HTTP protocol specifies that GET actions should never have a ● persistent effect (e.g. database) Even though you can encode parameters into URL’s ○ Greatly helps mitigate Cross-Site Request Forgery (CSRF) ○ Rarely respected ○ Good: <a href=“index.jsp?navigation=home”>Home</a> ● Bad: <a href=“index.jsp?changeName=Bobby”>Change Name</a> ● SWEN-331: Engineering Secure Software Benjamin S Meyers 15 15

  16. DoS in Many Forms Denial of service occurs in many, many ways ● Overflow the hard drive ○ Overflow memory → page faults ○ Poor hashcodes → constant hash collisions ○ Slow database queries ○ Deadlocks, race conditions, other concurrency issues ○ Network bandwidth issues ○ Recommendations ● Black-box stress testing ○ White-box, unit-level stress testing ○ Focus less on user inputs, more on the logic ○ Learn the art of profiling, e.g. java -agentlib:hprof ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 16 16

  17. Don’t Forget Configuration Files Vulnerabilities can also exist in system configurations ● e.g. log overflow, hardcoded credentials, authorization problems ○ Makefiles and installation definitions ● Insecure compiler optimizations, e.g. dead store removal opts. ○ Using out-of-date, vulnerable dependencies ○ Also: ● I18N configurations ○ General configurations ○ Example configurations ○ Recommendation ● Bring these up in code inspections ○ Look at the defaults , and what is missing ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 17 17

  18. Other Defensive Coding via VOTD Resource exhaustion Secure logging ● ● Log overflow ○ Check the limits of your input ● (Lack of) Log neutralization ○ Integer overflows ○ Limit use of privileged ● Buffer overflows ○ features of the language Don’t trust user input ● Use the Java security ○ SQL injection ○ Manager XSS ○ Classloader override ○ OS command injection ○ Complex file system ○ Error message information interaction ● Reflection abuse ○ leakage More serialization restrictions ○ SWEN-331: Engineering Secure Software Benjamin S Meyers 18 18

  19. Concept: Attack Surface Most exploits enter in through the UI ● Often the same interface the users see ○ Hence: input validation and sanitization ○ Attack surface ● The number and nature of the inputs for a given system ○ Can be quantified ○ Usually compared ○ Attack increases with… ● More inputs, e.g. new input fields, new features ○ Larger input space for a given input, e.g. allowing a markup ○ language instead of plaintext SWEN-331: Engineering Secure Software Benjamin S Meyers 19 19

Recommend


More recommend