openresty 防止sql注入

sql 防注入

注入示例

sql语句:select * from test where id = ?拼接语句:"sql' ".. id .."'"# 正常传入参数:id=1
拼接后的sql语句:select * from test where id = '1'# 传入参数:id = 1' or 1=1#
拼接后的sql语句:select * from test where id = '1' or 1=1#'# 使用ngx.quote_sql_str:会将id中的单引号转义 ' ==> \'
# 此时拼接后的sql执行报错,达到防注入的目的
拼接后的sql语句:select * from test where id = '1\' or 1=1#'

使用示例

default.conf

server {listen       80;server_name  localhost;location / {root   /usr/local/openresty/nginx/html;index  index.html index.htm;}location /test {content_by_lua_block {local condition = "1' or 1=1#"local sql = "select * from test where id ='"..condition.."'";local sql2 = "select * from test where id ='"..ngx.quote_sql_str(condition).."'";ngx.say(sql);ngx.say(sql2);}}location /test2 {content_by_lua_block {local mysql = require 'resty.mysql';local db, err = mysql:new();if not db then ngx.say("mysql创建失败", err);enddb:set_timeout(1000);local res, err, errcode, sqlstate = db:connect({host = "172.18.0.61", port = 3306, database = "lihu",user = "root", password = "123456"});if not res thenngx.say("连接出错", err, errcode, sqlstate);endlocal condition = "1' or 1=1#";local sql = "select * from test where id ='"..condition.."'";local sql2 = "select * from test where id ='"..ngx.quote_sql_str(condition).."'";ngx.say("数据查询:", "select * from test where id ='condition`'");res, err, errcode, sqlstate = db:query(sql);if not res thenngx.say("数据查询失败", err);endlocal cjson = require 'cjson';ngx.say("查询结果为:", cjson.encode(res));ngx.say("\n数据防注入查询:", "select * from test where id =' ..ngx.quote_sql_str(condition)'");res, err, errcode, sqlstate = db:query(sql2);if not res thenngx.say("数据查询失败", err);endngx.say("防注入查询结果为:", cjson.encode(res));}}error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/local/openresty/nginx/html;}}

创建容器

docker run -it -d --net fixed --ip 172.18.0.62 -p 5003:80 \
-v /Users/huli/lua/openresty/mysql/default.conf:/etc/nginx/conf.d/default.conf \
--name resty-mysql lihu12344/openresty

使用测试

# 查看拼接后的sql
huli@hudeMacBook-Pro mysql % curl localhost:5003/test
select * from test where id ='1' or 1=1#'
select * from test where id =''1\' or 1=1#''# 注入查询、防注入查询
huli@hudeMacBook-Pro mysql % curl localhost:5003/test2
数据查询:select * from test where id ='condition`'
查询结果为:[{"name":"gtlx","id":1},{"name":"gtlx","id":3},{"name":"gtlx","id":4},{"name":"gtlx","id":5},{"name":"gtlx","id":6},{"name":"gtlx","id":7},{"name":"gtlx","id":8},{"name":"gtlx","id":9},{"name":"gtlx","id":10},{"name":"gtlx","id":11}]数据防注入查询:select * from test where id =' ..ngx.quote_sql_str(condition)'
数据查询失败You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1\' or 1=1#''' at line 1
防注入查询结果为:null

openresty 防止sql注入相关推荐

  1. bypass最新版d盾mysql_Bypass 护卫神SQL注入防御(多姿势)

    0x00 前言 ​ 护卫神一直专注服务器安全领域, 其中有一款产品,护卫神·入侵防护系统 ,提供了一些网站安全防护的功能,在IIS加固模块中有一个SQL防注入功能. 这边主要分享一下几种思路,Bypa ...

  2. 万字讲解9种Web应用攻击与防护安全。XSS、CSRF、SQL注入等是如何实现的

    OWASP(开放Web软体安全项目- Open Web Application Security Project) 是一个开源的.非盈利的全球性安全组织,致力于应用软件的安全研究.使命 是使应用软件更 ...

  3. php mysql 防 sql注入_php 防sql注入方法

    php防sql注入的方法:1.使用mysql_real_escape_string方法转义SQL语句中使用的字符串中的特殊字符:2.打开magic_quotes_gpc来防止SQL注入:3.通过自定义 ...

  4. resultset mysql_MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)...

    [声明] 欢迎转载,但请保留文章原始出处→_→ [正文] 一.ResultSet接口的介绍: 对数据库的查询操作,一般需要返回查询结果,在程序中,JDBC为我们提供了ResultSet接口来专门处理查 ...

  5. mybaits的模糊查询_mybatis模糊查询防止SQL注入(很详细)

    SQL注入,大家都不陌生,是一种常见的攻击方式.攻击者在界面的表单信息或URL上输入一些奇怪的SQL片段(例如"or '1'='1'"这样的语句),有可能入侵参数检验不足的应用程序 ...

  6. SQL注入漏洞全接触--入门篇

    随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进 ...

  7. Sql注入和Html注入

    举例说,有一间公司的网页服务器上有一个留言板的代码,用来让用户发表简短的口信,例如: hello word!!!! 不过,这个代码原来有漏洞.一个意图入侵者得悉这间公司采用了有问题的代码,于是试图通过 ...

  8. mysql注入实例获取答案_本文实例讲述了MySQL解决SQL注入的另类方法。分享给大家供大家参考,具体如下:问题解读我觉得,这个问题每年带来的成本可以高达数十亿美元了。本文就来谈谈,...

    本文实例讲述了MySQL解决SQL注入的另类方法.分享给大家供大家参考,具体如下: 问题解读 我觉得,这个问题每年带来的成本可以高达数十亿美元了.本文就来谈谈,假定我们有如下 SQL 模板语句: se ...

  9. 【数据库】 兴唐第二十七节课只sql注入

    首先来一个用户登录程序 public static void login(String username, String password) {Connection conn = null;State ...

最新文章

  1. 关于arguments
  2. Swift 协议protocol
  3. Python程序开发——第四章 字典与集合
  4. [CareerCup] 9.4 Subsets 子集合
  5. Javascipt数组去重的几种方式
  6. php修改ini文件内容,php上传大文件需要修改的php.ini配置文件
  7. C函数形参列表与汇编寄存器的对应关系
  8. python os.path.split_Python中split()和os.path.split()
  9. c# 数据库操作学习
  10. Unity中UI框架初试探
  11. 射频功率放大器 RF放大器概念
  12. Fluent 旋转机械 风机 二维情况下的力矩计算算法
  13. css怎么修改图片像素,怎么改变图片宽度_word怎么改变图片像素大小
  14. 新款 Mac mini(2018) 性能及接口分析
  15. 苏宁618强势出圈,差异化竞争能力是杀手锏
  16. 香侬科技提出中文字型的深度学习模型Glyce,横扫13项中文NLP记录
  17. c++实现图书管理系统v2.0
  18. 计算机基本原理 学习笔记(八)
  19. 空间金字塔池化Spatial Pyramid Pooling
  20. 字符串 Z 字形变换(Java)

热门文章

  1. 6个常用第三方网站统计工具特点优势比较 - 站长应该选择谁?
  2. 我的 40 篇精选原创文章
  3. 乘风破浪,迎接大二——暑假生活总结
  4. Conditional注解
  5. 权威认可 | 通付盾再次入选工信部“CAPPVD安全漏洞库技术支撑单位”!
  6. 2023江苏安全员(B证)模拟考试试卷
  7. 在openwrt上实现用手机进行3G拨号
  8. NET START MSSQLSERVER 服务名无效解决办法
  9. 视频传输——1.AVC/H.264
  10. 关于K8s中资源服务质量管理Resource Qos的一些笔记整理