1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12 Nov 20, 2012 Malicious Code Vulnerability related to String,

Slides:



Advertisements
Similar presentations
Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
Advertisements

Computer Science CSC 474Dr. Peng Ning1 CSC 474 Information Systems Security Topic 5.1 Malicious Logic.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Lecture 16 Buffer Overflow modified from slides of Lawrie Brown.
Lecturer: Fadwa Tlaelan
Chapter 3 (Part 1) Network Security
CS526: Information Security Chris Clifton November 25, 2003 Malicious Code.
November 1, 2004Introduction to Computer Security ©2004 Matt Bishop Slide #9-1 Chapter 19: Malicious Logic What is malicious logic Types of malicious logic.
Stack-Based Buffer Overflows Attacker – Can take over a system remotely across a network. local malicious users – To elevate their privileges and gain.
________________ CS3235, Nov 2002 Viruses Adapted from Pfleeger[Chap 5]. A virus is a program [fragment] that can pass on malicious code [usually itself]
Gabe Kanzelmeyer CS 450 4/14/10.  What is buffer overflow?  How memory is processed and the stack  The threat  Stack overrun attack  Dangers  Prevention.
Teaching Buffer Overflow Ken Williams NC A&T State University.
1 Pertemuan 05 Malicious Software Matakuliah: H0242 / Keamanan Jaringan Tahun: 2006 Versi: 1.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
1 Malicious Logic CSSE 490 Computer Security Mark Ardis, Rose-Hulman Institute March 25, 2004.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
Strings COEN 296A Spring Strings Strings are a fundamental concept, but they are not a built-in data type in C/C++. C-Strings C-style string: character.
Secure Coding in C and C++ String Vulnerabilities
Henric Johnson1 Chapter 10 Malicious Software Henric Johnson Blekinge Institute of Technology, Sweden
Video Following is a video of what can happen if you don’t update your security settings! security.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
1 Chap 10 Malicious Software. 2 Viruses and ”Malicious Programs ” Computer “Viruses” and related programs have the ability to replicate themselves on.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Buffer overflows.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
More Network Security Threats Worm = a stand-alone program that can replicate itself and spread Worms can also contain manipulation routines to perform.
BLENDED ATTACKS EXPLOITS, VULNERABILITIES AND BUFFER-OVERFLOW TECHNIQUES IN COMPUTER VIRUSES By: Eric Chien and Peter Szor Presented by: Jesus Morales.
Lecture 14 Overview. Program Flaws Taxonomy of flaws: – how (genesis) – when (time) – where (location) the flaw was introduced into the system 2 CS 450/650.
Buffer Overflows Lesson 14. Example of poor programming/errors Buffer Overflows result of poor programming practice use of functions such as gets and.
Let’s look at an example I want to write an application that reports the course scores to you. Requirements: –Every student can only get his/her score.
Structure Classifications &
1 Higher Computing Topic 8: Supporting Software Updated
1 Chap 10 Virus. 2 Viruses and ”Malicious Programs ” Computer “Viruses” and related programs have the ability to replicate themselves on an ever increasing.
Mitigation of Buffer Overflow Attacks
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 3: Operating-System Structures System Components Operating System Services.
Secure Coding in C and C++ String Vulnerabilities Lecture 4 Jan 25, 2011 Acknowledgement: These slides are based on author Seacord’s original presentation.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Malicious Logic and Defenses. Malicious Logic Trojan Horse – A Trojan horse is a program with an overt (documented or known) effect and covert (undocumented.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
VIRUS.
Computer Systems Viruses. Virus A virus is a program which can destroy or cause damage to data stored on a computer. It’s a program that must be run in.
JMU GenCyber Boot Camp Summer, Introduction to Penetration Testing Elevating privileges – Getting code run in a privileged context Exploiting misconfigurations.
Application Vulnerabilities and Attacks COEN 351.
Buffer overflow and stack smashing attacks Principles of application software security.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Protection.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 11 Dec 8, 2009 Vulnerability related Integers. String, Race.
Let’s look at an example
Mitigation against Buffer Overflow Attacks
Introduction to Operating Systems
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
IS 2150 / TEL 2810 Introduction to Security
Text Book: Security in Computing
Introduction to Operating Systems
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Chap 10 Malicious Software.
IS 2150 / TEL 2810 Information Security & Privacy
IS 2150 / TEL 2810 Introduction to Security
Chapter 22: Malicious Logic
Chap 10 Malicious Software.
Malicious Program and Protection
Chapter 14: Protection.
Return-to-libc Attacks
Presentation transcript:

1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12 Nov 20, 2012 Malicious Code Vulnerability related to String, Race Conditions

2 Objectives Understand/explain issues related to malicious code and programming related vulnerabilities and buffer overflow String related Race Conditions

3 Malicious Code

4 What is Malicious Code? Set of instructions that causes a security policy to be violated unintentional mistake Tricked into doing that? “unwanted” code Generally relies on “legal” operations Authorized user could perform operations without violating policy Malicious code “mimics” authorized user

5 Types of Malicious Code Trojan Horse What is it? Virus What is it? Worm What is it?

6 Trojan Horse Program with an overt (expected) and covert (unexpected) effect Appears normal/expected Covert effect violates security policy User tricked into executing Trojan horse Expects (and sees) overt behavior Covert effect performed with user’s authorization Trojan horse may replicate Create copy on execution Spread to other users/systems

7 Example Perpetrator cat >/homes/victim/ls <<eof cp /bin/sh /tmp/.xxsh chmod u+s,o+x /tmp/.xxsh rm./ls ls $* eof Victim ls What happens? How to replicate this?

8 Virus Self-replicating code A freely propagating Trojan horse some disagree that it is a Trojan horse Inserts itself into another file Alters normal code with “infected” version Operates when infected code executed If spread condition then For target files if not infected then alter to include virus Perform malicious action Execute normal program

9 Virus Types Boot Sector Infectors (The Brain Virus) Problem: How to ensure virus “carrier” executed? Solution: Place in boot sector of disk Run on any boot Propagate by altering boot disk creation Executable infector The Jerusalem Virus, Friday 13 th, not 1987 Multipartite virus : boot sector + executable infector

10 Virus Types/Properties Terminate and Stay Resident Stays active in memory after application complete Allows infection of previously unknown files Stealth (an executable infector) Conceal Infection Encrypted virus Prevents “signature” to detect virus [Deciphering routine, Enciphered virus code, Deciphering Key] Polymorphism Change virus code to something equivalent each time it propagates

11 Virus Types/Properties Macro Virus Composed of a sequence of instructions that is interpreted rather than executed directly Infected “executable” isn’t machine code Relies on something “executed” inside application Example: Melissa virus infected Word 97/98 docs Otherwise similar properties to other viruses Architecture-independent Application-dependent

12 Worms Replicates from one computer to another Self-replicating: No user action required Virus: User performs “normal” action Trojan horse: User tricked into performing action Communicates/spreads using standard protocols

13 Other forms of malicious logic We’ve discussed how they propagate But what do they do? Rabbits/Bacteria Exhaust system resources of some class Denial of service ; e.g., While (1) {mkdir x; chdir x} Logic Bomb Triggers on external event Date, action Performs system-damaging action Often related to event Others?

14 We can’t detect it: Now what? Detection Signature-based antivirus Look for known patterns in malicious code Great business model! Checksum (file integrity, e.g. Tripwire) Maintain record of “good” version of file Validate action against specification Including intermediate results/actions N-version programming: independent programs A fault-tolerance approach (diversity)

15 Detection Proof-carrying code Code includes proof of correctness At execution, verify proof against code If code modified, proof will fail Statistical Methods High/low number of files read/written Unusual amount of data transferred Abnormal usage of CPU time

16 Defense Clear distinction between data and executable Virus must write to program Write only allowed to data Must execute to spread/act Data not allowed to execute Auditable action required to change data to executable

17 Defense Information Flow Control Limits spread of virus Problem: Tracking information flow Least Privilege Programs run with minimal needed privilege

18 Defense Sandbox / Virtual Machine Run in protected area Libraries / system calls replaced with limited privilege set Use Multi-Level Security Mechanisms Place programs at lowest level Don’t allow users to operate at that level Prevents writes by malicious code

19 String Vulnerabilities

20 C-Style Strings Strings are a fundamental concept in software engineering, but they are not a built-in type in C or C++. C-style strings consist of a contiguous sequence of characters terminated by and including the first null character. A pointer to a string points to its initial character. String length is the number of bytes preceding the null character The string value is the sequence of the values of the contained characters, in order. The number of bytes required to store a string is the number of characters plus one (x the size of each character) hello\0 length

21 Common String Manipulation Errors Common errors include Unbounded string copies Null-termination errors Truncation Write outside array bounds Off-by-one errors Improper data sanitization

22 Unbounded String Copies Occur when data is copied from an unbounded source to a fixed length character array 1. int main(void) { 2. char Password[80]; 3. puts("Enter 8 character password:"); 4. gets(Password); } 1. #include 2. int main(void) { 3. char buf[12]; 4. cin >> buf; 5. cout<<"echo: "<<buf<<endl; 6. }

23 Simple Solution Test the length of the input using strlen() and dynamically allocate the memory 1. int main(int argc, char *argv[]) { 2. char *buff = (char *)malloc(strlen(argv[1])+1); 3. if (buff != NULL) { 4. strcpy(buff, argv[1]); 5. printf("argv[1] = %s.\n", buff); 6. } 7. else { /* Couldn't get the memory - recover */ 8. } 9. return 0; 10. }

24 Null-Termination Errors Another common problem with C-style strings is a failure to properly null terminate int main(int argc, char* argv[]) { char a[16]; char b[16]; char c[32]; strcpy(a, " abcdef”); strcpy(b, " abcdef"); strcpy(c, a); } Neither a[] nor b[] are properly terminated

25 String Truncation Functions that restrict the number of bytes are often recommended to mitigate against buffer overflow vulnerabilities Example: strncpy() instead of strcpy() Strings that exceed the specified limits are truncated Truncation results in a loss of data, and in some cases, to software vulnerabilities

26 Improper Data Sanitization An application inputs an address from a user and writes the address to a buffer [Viega 03] sprintf(buffer, "/bin/mail %s < /tmp/ ", addr ); The buffer is then executed using the system() call. The risk is, of course, that the user enters the following string as an address: cat /etc/passwd | mail [Viega 03] Viega, J., and M. Messier. Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Networking, Input Validation & More. Sebastopol, CA: O'Reilly, 2003.

27 What is a Buffer Overflow? A buffer overflow occurs when data is written outside of the boundaries of the memory allocated to a particular data structure Destination Memory Source Memory Allocated Memory (12 Bytes) Other Memory 16 Bytes of Data Copy Operation

28 Buffer Overflows Caused when buffer boundaries are neglected and unchecked Buffer overflows can be exploited to modify a variable data pointer function pointer return address on the stack

29 Smashing the Stack This is an important class of vulnerability because of their frequency and potential consequences. Occurs when a buffer overflow overwrites data in the memory allocated to the execution stack. Successful exploits can overwrite the return address on the stack allowing execution of arbitrary code on the targeted machine.

30 Program Stacks A program stack is used to keep track of program execution and state by storing return address in the calling function arguments to the functions local variables (temporary) The stack is modified during function calls function initialization when returning from a subroutine Code Data Heap Stack

31 Stack Segment The stack supports nested invocation calls Information pushed on the stack as a result of a function call is called a frame Stack frame for main() Low memory High memory Stack frame for a() Stack frame for b() Unallocated b() {…} a() { b(); } main() { a(); } A stack frame is created for each subroutine and destroyed upon return

32 Stack Frames The stack is used to store return address in the calling function actual arguments to the subroutine local (automatic) variables The address of the current frame is stored in a register (EBP on Intel architectures) The frame pointer is used as a fixed point of reference within the stack

33 push 4 Push 1 st arg on stack call function (411A29h) Push the return address on stack and jump to address Subroutine Calls function(4, 2); push 2 Push 2 nd arg on stack

Subroutine Initialization void function(int arg1, int arg2) { push ebp Save the frame pointer mov ebp, esp Frame pointer for subroutine is set to current stack pointer sub esp, 44h Allocates space for local variables

35 Subroutine Return return(); mov esp, ebp Restore the stack pointer pop ebp Restore the frame pointer ret Pops return address off the stack and transfers control to that location

Return to Calling Function function(4, 2); push 2 push 4 call function (411230h) add esp,8 Restore stack pointer

37 Example Program bool IsPasswordOK(void) { char Password[12]; // Memory storage for pwd gets(Password); // Get input from keyboard if (!strcmp(Password,"goodpass")) return(true); // Password Good else return(false); // Password Invalid } void main(void) { bool PwStatus; // Password Status puts("Enter Password:"); // Print PwStatus=IsPasswordOK(); // Get & Check Password if (PwStatus == false) { puts("Access denied"); // Print exit(-1); // Terminate Program } else puts("Access granted");// Print }

38 Stack Before Call to IsPasswordOK() Storage for PwStatus (4 bytes) Caller EBP – Frame Ptr OS (4 bytes) Return Addr of main – OS (4 Bytes) … puts("Enter Password:"); PwStatus=IsPasswordOK(); if (PwStatus==false) { puts("Access denied"); exit(-1); } else puts("Access granted"); Stack ESP Code EIP

39 Stack During IsPasswordOK() Call Storage for Password (12 Bytes) Caller EBP – Frame Ptr main (4 bytes) Return Addr Caller – main (4 Bytes) Storage for PwStatus (4 bytes) Caller EBP – Frame Ptr OS (4 bytes) Return Addr of main – OS (4 Bytes) … puts("Enter Password:"); PwStatus=IsPasswordOK(); if (PwStatus==false) { puts("Access denied"); exit(-1); } else puts("Access granted"); bool IsPasswordOK(void) { char Password[12]; gets(Password); if (!strcmp(Password, "goodpass")) return(true); else return(false) } Note: The stack grows and shrinks as a result of function calls made by IsPasswordOK(void) Stack ESP Code EIP

40 Stack After IsPasswordOK() Call puts("Enter Password:"); PwStatus = IsPasswordOk(); if (PwStatus == false) { puts("Access denied"); exit(-1); } else puts("Access granted"); Storage for Password (12 Bytes) Caller EBP – Frame Ptr main (4 bytes) Return Addr Caller – main (4 Bytes) Storage for PwStatus (4 bytes) Caller EBP – Frame Ptr OS (4 bytes) Return Addr of main – OS (4 Bytes) … Stack EIP Code ESP

41 The Buffer Overflow 1 What happens if we input a password with more than 11 characters ? * CRASH *

42 The Buffer Overflow 2 bool IsPasswordOK(void) { char Password[12]; gets(Password); if (!strcmp(Password,"badprog")) return(true); else return(false) } Storage for Password (12 Bytes) “ ” Caller EBP – Frame Ptr main (4 bytes) “3456” Return Addr Caller – main (4 Bytes) “7890” Storage for PwStatus (4 bytes) “\0” Caller EBP – Frame Ptr OS (4 bytes) Return Addr of main – OS (4 Bytes) … Stack The return address and other data on the stack is over written because the memory space allocated for the password can only hold a maximum 11 character plus the NULL terminator. EIP ESP

43 The Vulnerability A specially crafted string “ j ► *!” produced the following result. What happened ?

44 What Happened ? “ j ► *!” overwrites 9 bytes of memory on the stack changing the callers return address skipping lines 3-5 and starting execuition at line 6 Storage for Password (12 Bytes) “ ” Caller EBP – Frame Ptr main (4 bytes) “3456” Return Addr Caller – main (4 Bytes) “j ► *!” (return to line 7 was line 3) Storage for PwStatus (4 bytes) “\0” Caller EBP – Frame Ptr OS (4 bytes) Return Addr of main – OS (4 Bytes) Stack Statement 1puts("Enter Password:"); 2PwStatus=ISPasswordOK(); 3if (PwStatus == true) 4 puts("Access denied"); 5 exit(-1); 6} 7else puts("Access granted"); Note: This vulnerability also could have been exploited to execute arbitrary code contained in the input string.

45 Race conditions

46 Concurrency and Race condition Concurrency Execution of Multiple flows (threads, processes, tasks, etc) If not controlled can lead to nondeterministic behavior Race conditions Software defect/vulnerability resulting from unanticipated execution ordering of concurrent flows E.g., two people simultaneously try to modify the same account (withrawing money)

47 Race condition Necessary properties for a race condition Concurrency property At least two control flows executing concurrently Shared object property The concurrent flows must access a common shared race object Change state property Atleast one control flow must alter the state of the race object

48 Race window A code segment that accesses the race object in a way that opens a window of opportunity for race condition Sometimes referred to as critical section Traditional approach Ensure race windows do not overlap Make them mutually exclusive Language facilities – synchronization primitives (SP) Deadlock is a risk related to SP Denial of service

49 Time of Check, Time of Use Source of race conditions Trusted (tightly coupled threads of execution) or untrusted control flows (separate application or process) ToCTToU race conditions Can occur during file I/O Forms a RW by first checking some race object and then using it

50 Example Assume the program is running with an effective UID of root int main(int argc, char *argv[]) { FILE *fd; if (access(“/some_file”, W_OK) == 0) { printf("access granted.\n"); fd = fopen(“/some_file”, "wb+"); /* write to the file */ fclose(fd); } else { err(1, "ERROR"); } return 0; } Figure 7-1 int main(int argc, char *argv[]) { FILE *fd; if (access(“/some_file”, W_OK) == 0) { printf("access granted.\n"); fd = fopen(“/some_file”, "wb+"); /* write to the file */ fclose(fd); } else { err(1, "ERROR"); } return 0; } Figure 7-1

51 TOCTOU Following shell commands during RW rm /some_file ln /myfile /some_file Mitigation Replace access() call by code that does the following Drops the privilege to the real UID Open with fopen() & Check to ensure that the file was opened successfully