026 黑客喜欢mass assignment

简介:

Hackers Love Mass Assignment

Your site may be at risk! When using mass assignment, you are giving the user complete control over that model and its associations. See how a hacker might use this vulnerability and learn how to stop it in this episode.
 
你的站点或许面临风险!当你使用mass assignment(传递整个model对象属性参数),你会把整个model及其联系的控制权交给用户。看一下hacker如何利用这来攻击你的站点,并且看看如何阻止其发生。
 
---
有一个注册页面,可以提交名字。在User这个model中有两个属性 name和admin,admin是一个boolean型的字段。
在users_controller.rb中,这么定义注册的action
def create
    @user=User.new(params[:user])
    if @user.save
        ...
    else
        ...
    end
end
那么hacker可能通过执行这条语句来直接注册一个admin用户.
curl -d "user[name]=hacker&user[admin]=1" localhost:3000/users
在日志中查看下发生了什么改变:
 
Parameters:{"user"=>{"hacker", "admin"=>"1"}, "action"=>"create", "controller"=>"users"}
 
INSERT INTO users ('name', 'admin') VALUES('hacker', 1)
这样就提交了一个admin的user对象。
 
为了防止这个情况发生,可以在user model里加上
attr_protected :admin
这样就不能从外界提交设置这个字段
在user.rb中,有has_many :comments
 
hacker可以执行这样的命令:
curl -d "user[name]=hacker&user[admin]=1&user[comment_ids][]=1&user[comment_ids][]=2" localhost:3000/users
 
admin=1这个已经无法写入数据库了,但是却把comments改成自己的了
 
用attr_accessible :name
现在就只能用params提交name这一个字段了。




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/131840,如需转载请自行联系原作者
目录
相关文章
UVa11296 - Counting Solutions to an Integral Equation(枚举技巧)
UVa11296 - Counting Solutions to an Integral Equation(枚举技巧)
64 0
UVa11714 - Blind Sorting
UVa11714 - Blind Sorting
82 0
UVa10776 - Determine The Combination(有重复元素的组合问题)
UVa10776 - Determine The Combination(有重复元素的组合问题)
57 0
《The Six Sense of Auto-driving Car —— Road to Autonomous Driving》电子版地址
The Six Sense of Auto-driving Car —— Road to Autonomous Driving
81 0
《The Six Sense of Auto-driving Car —— Road to Autonomous Driving》电子版地址
|
Unix Go Python
Assignment 2
unix diff two files 3. Task specifications Write a program stored in a file named diff.py that implements three classes, • DiffCommands, • DiffCommandsError and • OriginalNewFiles, the second one deriving from Exception. DiffCommands does not provide any method in its public interface. It builds
1085. Perfect Sequence (25)
#include #include #include using namespace std; int main() { int n; long p; cin >> n >> p; v...
883 0
|
Scala C++ 编译器
C++雾中风景5:Explicit's better than implicit.聊聊Explicit.
关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议。那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊。
1417 0