⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 14 - preventing sql injection attacks.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
use cookbook;DROP TABLE IF EXISTS names;CREATE TABLE names (  first VARCHAR(200),  last VARCHAR(200)) ENGINE=InnoDB;INSERT INTO names values ('Leonard', 'Richardson'),                          ('Lucas', 'Carlson'),                          ('Michael', 'Loukides');#---#!/usr/bin/ruby# no_sql_injection.rbrequire 'cookbook_dbconnect'print 'Enter a last name to search for: 'search_for = readline.chompwith_db do |db|  sql = 'select first, last from names where last=?'  db.execute(sql, [search_for]).fetch_hash do |r|    puts %{Matched "#{r['first']} #{r['last']}"}  endend#---$ ruby no_sql_injection.rbEnter a last name to search for: RichardsonMatched "Leonard Richardson"# See the Discussion if you're not sure how this attack is supposed to work.$ ruby no_sql_injection.rbEnter a last name to search for: " or 1=1#---#!/usr/bin/ruby# sql_injection.rbrequire 'cookbook_dbconnect'print "Enter a last name to search for: "search_for = readline.chompquery = %{select first, last from names where last="#{search_for}"}puts query if $DEBUGwith_db do |db|  db.execute(query).fetch_hash do |r|    puts %{Matched "#{r['first']} #{r['last']}"}  endend#---$ ruby -d sql_injection.rbEnter a last name to search for: Richardsonselect first_name, last_name from people where last_name="Richardson"Matched "Leonard Richardson"#---$ ruby -d sql_injection.rbEnter a last name to search for: " or 1=1select first_name, last_name from people where last_name="" or 1=1"Matched "Leonard Richardson"Matched "Lucas Carlson"Matched "Michael Loukides"#---require 'cookbook_dbconnect'activerecord_connectclass Name < ActiveRecord::Base  def self.by_last(name)    find_all ["last = ?", name]  endendName.by_last("Richardson").size              # => 1Name.by_last(%{" or 1=1}).size               # => 0#---class Name   def self.by_last(name)    find_all ["last = :last", {:last => name}]  endendName.by_last("Richardson").size              # => 1#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -