cs 152: programming language paradigms

31
CS 152: Programming Language Paradigms Prof. Tom Austin San José State University Dynamic Code Evaluation & Taint Analysis

Upload: others

Post on 18-Apr-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 152: Programming Language Paradigms

CS 152: Programming Language Paradigms

Prof. Tom AustinSan José State University

Dynamic Code Evaluation &Taint Analysis

Page 2: CS 152: Programming Language Paradigms

Dynamic code evaluation

Page 3: CS 152: Programming Language Paradigms

eval• Executes dynamically• Typically, eval takes a string:eval "puts 2+3"• Popular feature–especially in JavaScript• Richards et al. The Eval that Men Do, 2011

• Source of security problems

Page 4: CS 152: Programming Language Paradigms

Parsing JSON(in-class)

Page 5: CS 152: Programming Language Paradigms

Quick and Dirty Parsing of JSON(Never do this)

var emps = eval(jsonStr);for (var i in emps) {var emp = emps[i];console.log(emp.name);

}

Page 6: CS 152: Programming Language Paradigms

Sample JSON

[{ name: 'Philip J. Fry',age: 1000,job: 'delivery boy' },

{ name: 'Bender Rodriguez',age: 42,job: 'bending unit' }]

Page 7: CS 152: Programming Language Paradigms

Sample JSON

[{ name: 'Philip J. Fry',age: 1000,job: 'delivery boy' },

{ name: (function(){console.log('***All glory' +

'to the Hypnotoad!***')})() },

{ name: 'Bender Rodriguez',age: 42,job: 'bending unit' }]

Page 8: CS 152: Programming Language Paradigms

Review: additional Ruby eval methods

• instance_eval evaluates code within the body of an object.• class_eval evaluates code

within the body of a class.• These methods can take a string

or (more safely) a block of code.

Page 9: CS 152: Programming Language Paradigms

class_eval example(in class)

Page 10: CS 152: Programming Language Paradigms

class Classdef my_attr_accessor(*args)args.each do |prop|self.class_eval("def #{prop}; @#{prop}; end")

self.class_eval("def #{prop}=(v);@#{prop}=v;end")

endendend

Page 11: CS 152: Programming Language Paradigms

Using my_attr_accessor

class Musicianmy_attr_accessor :name, :genre

end

m = Musician.newm.name = "Norah Jones"puts m.name

Page 12: CS 152: Programming Language Paradigms
Page 13: CS 152: Programming Language Paradigms

The mind of a developer

What does my code

need to do?Stupid documentation

Hmm… I wonder if my code is secure

Page 14: CS 152: Programming Language Paradigms

Web Security in the News

Page 15: CS 152: Programming Language Paradigms

How do companies/developers cope?

• Train/shame developers to follow best practices.• Hire security experts• Use analysis tools• Hush up mistakes• Budget to handle emergencies• Bury their heads in the sand.

Page 16: CS 152: Programming Language Paradigms

Secure By Architecture

Developers make mistakes.

Can we design tools to create secure systems, despite developer mistakes?

Page 17: CS 152: Programming Language Paradigms

Success story: memory-safe languages

• Buffer overflows were once ubiquitous• Memory-safe languages manage

memory automatically–Developer focus on functionality–Security-critical bugs are eliminated

• Buffer overflows have virtually disappeared–Except in your OS, web browser, etc.

Page 18: CS 152: Programming Language Paradigms

Two Security Mechanisms

• Taint analysis:–protect critical fields from

"dirty" data• Information flow analysis: –Prevent secrets from leaking.

Page 19: CS 152: Programming Language Paradigms

Taint Analysis:Protecting against dirty data

Page 20: CS 152: Programming Language Paradigms

Taint analysis

• Taint analysis focuses on integrity:–does "dirty" data corrupt trusted data?

• Integrated into Perl and Ruby• Handles explicit flows only–direct assignment–passing parameters

Page 21: CS 152: Programming Language Paradigms

Attacks preventable by taint analysis

• Data under the control of the user may pose a security risk–SQL injection–cross-site scripting (XSS)–cross-site request forgery (CSRF)

• Taint tracking tracks untrusted variables and prevents then from being used in unsafe operations

Page 22: CS 152: Programming Language Paradigms

Taint Tracking History

• 1989 – Perl 3 support for a taint mode• 1996 – Netscape included support for a

taint mode in server-side JavaScript–Later abandoned

• Ruby later implemented a taint mode;we'll review in more depth.

Page 23: CS 152: Programming Language Paradigms

Taint Mode in Ruby

• Protect against integrity attacks.–E.g. Data pulled from an HTML form

cannot be passed to eval.

• Cannot taint booleans or ints.• Multiple ways to run in safe mode:–Use –T command line flag.–Include $SAFE variable in code.

Page 24: CS 152: Programming Language Paradigms

# Mark a string as tainteds = "puts 4-3".taint

# Can't eval tainted data$SAFE = 1

# Removes taint from datas.untaint

# Test if string is tainted# (Works even if $SAFE is not set).puts s.tainted?

Page 25: CS 152: Programming Language Paradigms

"Exploits of a Mom", http://xkcd.com/327/

SQL injection

Page 26: CS 152: Programming Language Paradigms

An improperly sanitized query

<?phppg_query($con,

"INSERT INTO STUDENTS (name)". " VALUES('" . $_POST['name']."')";

?>

name = "Joe"

INSERT INTO STUDENTS (name)VALUES('Joe')

Page 27: CS 152: Programming Language Paradigms

An improperly sanitized query

<?phppg_query($con,

"INSERT INTO STUDENTS (name)". " VALUES('" . $_POST['name']."')";

?>

name = "Robert');DROP TABLE STUDENTS;--"

INSERT INTO STUDENTS (name)VALUES('Robert');

DROP TABLE STUDENTS;--')

Quote ends data: now in SQL

Remainder commented out.

Page 28: CS 152: Programming Language Paradigms

Correctly sanitized query

<?phppg_query_params($con,

"INSERT INTO STUDENTS (name)". " VALUES($1)",ARRAY($_POST['name']);

?>

name = "Robert');DROP TABLE STUDENTS;--"

INSERT INTO STUDENTS (name)VALUES('Robert'');

DROP TABLE STUDENTS;--')

Quote escaped.

Page 29: CS 152: Programming Language Paradigms

# Data from webs = "Robert'); DROP TABLE " +

"STUDENTS;--"s.taintexec_query("SELECT *" +

" FROM STUDENTS" + " WHERE NAME='" +s + "';"

Page 30: CS 152: Programming Language Paradigms

class Recorddef exec_query(query_str)

if query_str.tainted?puts "Err: tainted string"else

# Perform the query...

endend

end

Page 31: CS 152: Programming Language Paradigms

Lab: Taint tracking

Today's lab explores taint tracking in Ruby.Starter code is available on the course website.Details in Canvas.