CSIT561 Module5 Program Security
CSIT561 Module5 Program Security
Bharath K. Samanthula
Department of Computer Science
Montclair State University
Slides are adopted from Chapter 12, Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043)..
2
Objectives
• Learn about memory organization, buffer
overflows, and relevant countermeasures
• Common programming bugs, such as off-by-one
errors, race conditions, and incomplete mediation
• Survey of past malware and malware capabilities
• Virus detection
• Tips for programmers on writing code for security
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
3
Programs
• Programs and their computer code are the basis of computing
• Programs consist of elementary (primitive) machine
commands, such as move one data item or compare two data
items
• Example: Intel 32- and 64-bit instruction set has about 30 basic
primitives
• Primitive commands implement higher-level programming
constructs, such as conditional loops, arithmetic and string
operations
• Programmers often use code libraries to build complex
programs
• Errors or flaws in programs can range from insignificant to
catastrophic
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
4
Program Flaws
Program flaws can have two kinds of security implications:
• A program flaw can be a fault affecting the correctness of
the program’s result
• Incorrect operations is an integrity failure
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
5
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
6
Memory Allocation
High addresses
Stack
0!1C0A
Execute instruction
“Jump forward 10
bytes”
Memory
The same hex value in the same spot in memory can either be a
meaningful data value or a meaningful instruction depending on
whether the computer treats it as code or data. This will be the basis
of the attacks in the following slides.
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
8
Buffer Overflows
• Occur when data is written beyond the space allocated for
it, such as a 10th byte in a 9-byte array
• In a typical exploitable buffer overflow, an attacker’s
inputs are expected to go into regions of memory
allocated for data, but those inputs are instead allowed to
overwrite memory holding executable code
• The trick for an attacker is finding buffer overflow
opportunities that lead to overwritten memory being
executed, and finding the right code to input
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
9
int i;
sample[10] = ‘B’;
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
10
Memory Organization
High addresses
Stack
Heap
Local Data
Program Code
System Data
System Code
Low addresses
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
11
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
12
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
13
The Stack
Stack
P3
P2
Direction of
growth P1
Prog Ctr
Stack Ptr
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
14
Stack
Procedure A P3 call C
P2
call B P1
Prog Ctr
Stack Ptr Procedure C
P2
P1
Prog Ctr
Stack Ptr
NOTE: When procedure A calls procedure B, procedure B gets added to the stack along with a pointer back to procedure
A. In this way, when procedure B is finished running, it can get popped off the stack, and procedure A will just continue
executing where it left off.
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
15
Stack
Procedure A P3 call C
P2
call B P1
Prog Ctr
Stack Ptr Procedure C
code
code
Prog Ctr
Stack Ptr
NOTE: Instead of pointing at procedure B in this case, the program counter is pointing at code that’s been placed on
the stack as a result of an overflow.
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
16
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
17
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
18
Overflow Countermeasures
• Staying within bounds
• Check lengths before writing
• Confirm that array subscripts are within limits
• Double-check boundary condition code for off-by-one errors
• Limit input to the number of acceptable characters
• Limit programs’ privileges to reduce potential harm
https://www.us-cert.gov/bsi
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
19
Incomplete Mediation
• Mediation: Verifying that the subject is
authorized to perform the operation on an
object
• Preventing incomplete mediation:
• Validate all input
• Limit users’ access to sensitive data and
functions
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
20
Time-of-Check to Time-of-Use
• Mediation performed with a “bait and switch” in the middle
• Between access check and use, data must be protected against change
File: Action:
my_file Change byte 4 to A
File: Action:
your_file Delete file
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
21
Race Conditions
A Seat available? Book seat
Yes
Reservation system
B Seat available? No
Time
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
22
Race Conditions
Situation in which program behavior depends on the order in
which two procedures execute
Reservation system
Time
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
23
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
24
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
25
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
26
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
27
Malware
• MALicious softWARE - Programs planted by an agent
with malicious intent to cause unanticipated or undesired
effects
• In May 2010, Roger Thompson from AVG detected
malicious code (a hidden call to Ukraine’s website) at the
website of the U.S. Bureau of Engraving and Printing
• Used the Eleonore attack toolkit – click and run application against
a targeted website
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
28
Malware
Three most popular forms
• Virus
• A program that can replicate itself and pass on malicious code to
other nonmalicious programs by modifying them
• Worm
• A program that spreads copies of itself through a network
• Trojan horse
• Code that, in addition to its stated effect, has a second,
nonobvious, malicious effect
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
29
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
30
Types of Malware
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
31
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
32
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
33
History of Malware
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
34
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
35
Zero-Day Attack
• Active malware exploiting a product vulnerability for which
the manufacturer has no countermeasure available
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
36
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
37
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
38
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
39
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
40
Interesting Question
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
41
Malware Activation
• One-time execution (implanting)
• User clicks to download a file or opens an attachment
• Boot sector viruses
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
42
Malware Activation
• Memory-resident viruses
• Attach virus to resident code (such as routine that interprets keys
pressed)
• Example: a boot sector virus after activation might attach itself to a
piece of resident code
• Application files
• If there is a flaw in pdf interpreter à execution of malicious code
• Virus macros in applications, such as spreadsheets, can adds itself
to the startup directives that are execute every time
• Code libraries
• Compilers, linkers, runtime debuggers are good candidates for
hosting viruses as they are widely shared
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
43
Malware Stealth
• Most viruses maintain stealth by concealing their action,
not announcing their presence, and disguising their
appearance – during installation, while executing, or even
at rest in storage
• Installation Stealth: for example, downloading as a result of loading
a web page
• Execution Stealth: OS supports dozens of concurrent processes
which have unrecognizable names and functions. Hard for users to
distinguish legitimate programs from malware code
• Stealth in Storage: produce unique copy for every user
• Rearrange order of modules/instructions
• Insert instructions that have no impact
• Insert random strings
• Replace instructions with others of equivalent effect
• Insert instructions that are never executed
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
44
Virus Effects
• Virus Effect How It Is Caused
Attach to executable • Modify file directory
program • Write to executable program file
Attach to data or • Modify directory
control file • Rewrite data
• Append to data
• Append data to self
Remain in memory • Intercept interrupt by modifying interrupt
handler address table
• Load self in non-transient memory area
Infect disks • Intercept interrupt
• Intercept operating system call (to format
disk, for example)
• Modify system file
• Modify ordinary executable program
Conceal self • Intercept system calls that would reveal
self and falsify result
• Classify self as “hidden” file
Spread infection • Infect boot sector
• Infect systems program
• Infect ordinary program
• Infect data ordinary program reads to
control its execution
Prevent deactivation • Activate before deactivating program and
block deactivation
• Store copy to reinfect after deactivation
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
45
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
46
Virus Detection
• Virus scanners look for signs of malicious code infection using
signatures in program files and memory
• Traditional virus scanners have trouble keeping up with new
malware—detect about 45% of infections
• Detection mechanisms:
• Known string patterns in files or memory
• Effective iff virus scanner is up-to-date with latest information on current
viruses
• Execution patterns – Code Analysis
• Often difficult as all the structure and documentation are lost during
compilation
• Storage patterns – where the virus code is located relative to the
infected program
• Changes to file sizes or program’s functionalities can also be used
• Look for suspicious patterns, such as JUMP instruction as the first
instruction – Possibly a virus
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
47
Virus Signatures
IF (--)
Attached Recognizable
JUMP
Virus Code signature elements
Original
Program
Original
Program
Separate
Virus
Module
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
48
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
49
Code Testing
Testing – goal is to make the product failure free (not really
possible, only reduces the likelihood of failures)
• Unit testing – Each module/component is tested separately
• Integration testing – Components work together!
• Function testing – evaluate for requirement specifications
• Performance testing – check for overall software and hardware
requirements
• Acceptance testing – system is checked against customer’s
requirements description
• Installation testing – testing under real environment
• Regression testing – done after a fix to ensure functions are
working and performance has not been degraded
• Penetration testing – look for possible vulnerabilities
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
50
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
51
Other Countermeasures
• Good
• Penetration Testing – experts trying to crack the system being tested
• Proofs of program correctness—where possible
• Initial assertions about the program’s inputs and see if the desired output is
generated, check the logical flow of the program
• Defensive programming
• Anticipate what could go wrong (e.g., appropriate data types, values out of
range, incorrect number of parameters)
• Design by contract
• Checking Preconditions, postconditions and invariants
• Bad
• Penetrate-and-patch – fixing one problem causes failure at other place
• Security by obscurity – assuming system is secure by hiding internal
mechanisms, such as hiding account passwords in binary files
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.
52
Summary
• Buffer overflow attacks can take advantage of the fact that
code and data are stored in the same memory in order to
maliciously modify executing programs
• Programs can have a number of other types of
vulnerabilities, including off-by-one errors, incomplete
mediation, and race conditions
• Malware can have a variety of harmful effects depending
on its characteristics, including resource usage, infection
vector, and payload
• Developers can use a variety of techniques for writing and
testing code for security
From Security in Computing, Fifth Edition, by Charles P. Pfleeger, et al. (ISBN: 9780134085043). Copyright 2015 by Pearson Education, Inc. All rights reserved.