MySQL for Java的SQL注入测试
2019独角兽企业重金招聘Python工程师标准>>>
只要你学JDBC,基本上所有的人都会和你说,Statement不能防止SQL注入, PreparedStatement能够防止SQL注入. 基本上参加工作了一段时间之后还是这么认为的, 没错, 这句是没有问题的, 但到底如何进行SQL注入?怎么直观的去了解SQL注入?这还是需要花一定的时间去实验的. 前提:以下的测试都是在一种理想环境下 首先准备好数据库环境, 以下是数据库的schema:
- create database java_mysql;
- use java_mysql;
- drop table if exists pstest;
- create table pstest(
- id int(10) not null primary key auto_increment,
- name varchar(32),
- age int(3)
- );
- insert into pstest (name, age) values ('Tom', 23);
- insert into pstest (name, age) values ('Tom1', 23);
- insert into pstest (name, age) values ('Tom2', 23);
- insert into pstest (name, age) values ('Tom3', 23);
- insert into pstest (name, age) values ('Tom4', 23);
- insert into pstest (name, age) values ('Tom5', 23);
- public class StatementTest {
- public static void main(String[] args) throws SQLException {
- Connection con = null;
- Statement stmt = null;
- // name很强大, 传入了这么多东西
- String name = "Tom';delete from pstest;select * from pstest where name='Tom";
- String sql = createSql(name); // SQL
- System.out.println(sql);
- try {
- con = DBConn.getConnection();
- stmt = con.createStatement();
- stmt.execute(sql);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- stmt.close();
- con.close();
- }
- }
- // 根据参数的name参数查询
- private static String createSql(String name) {
- String sql = "select id, name, age from pstest ";
- // 拼接一下SQL
- if(name != null && name.length() != 0) {
- sql += "where name ='" + name + "'";
- }
- return sql;
- }
- }
- "jdbc:mysql://localhost:3306/java_mysql";
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'delete from pstest;select * from pstest where name='Tom'' at line 1
- jdbc:mysql://localhost:3306/java_mysql?allowMultiQueries=true
- select id, name, age from pstest where name ='Tom';delete from pstest;select * from pstest where name='Tom';
上面的SQL到底做了什么呢?
- mysql> select * from pstest;
- +----+------+------+
- | id | name | age |
- +----+------+------+
- | 1 | Tom | 23 |
- | 2 | Tom1 | 23 |
- | 3 | Tom2 | 23 |
- | 4 | Tom3 | 23 |
- | 5 | Tom4 | 23 |
- | 6 | Tom5 | 23 |
- +----+------+------+
- select id, name, age from pstest where name ='Tom';delete from pstest;select * from pstest where name='Tom';
- mysql> select id, name, age from pstest where name ='Tom';delete from pstest;select * from pstest where name='Tom';
- +----+------+------+
- | id | name | age |
- +----+------+------+
- | 1 | Tom | 23 |
- +----+------+------+
- 1 row in set (0.00 sec) -- 执行第一条查询SQL
- Query OK, 6 rows affected (0.05 sec) -- 执行第二条delete语句, Oh, No, 数据库全部的6条数据被删除了
- Empty set (0.00 sec) -- 执行第三条SQL查询, 没有查询到任何数据
- public class PreparedStatementTest {
- public static void main(String[] args) throws SQLException {
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- String sql = "select id, name, age from pstest where name = ? ";
- try {
- con = DBConn.getConnection();
- ps = con.prepareStatement(sql);
- ps.setString(1, "Tom';delete from pstest;select * from pstest where name='Tom");
- rs = ps.executeQuery();
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- rs.close();
- ps.close();
- con.close();
- }
- }
- }
- [mysqld]
- log=MySQL_Log # 在这里加上日志名称
这时候会在MySQL/MySQL Server 5.1/Data/目录下生成MySQL_Log文件, 里面记录的就是日志了.
- 120719 15:54:25 23 Connect root@localhost on java_mysql
- 23 Query /* mysql-connector-java-5.1.20-SNAPSHOT ( Revision: ${bzr.revision-id} ) ...
- 23 Query SHOW WARNINGS
- 23 Query /* mysql-connector-java-5.1.20-SNAPSHOT ( Revision: ${bzr.revision-id} ) */SELECT @@session.auto_increment_increment
- 23 Query SHOW COLLATION
- 23 Query SET character_set_results = NULL
- 23 Query SET autocommit=1
- 23 Query select id, name, age from pstest where name = 'Tom';delete from pstest;select * from pstest where name='Tom'
- 23 Quit
- select id, name, age from pstest where name = 'Tom';delete from pstest;select * from pstest where name='Tom'
- mysql> select id, name, age from pstest where name = 'Tom';delete from pstest;select * from pstest where name='Tom';
- Empty set (0.00 sec) -- 就执行了这一条查询的SQL语句
- mysql> select 'Tom';delete';
- +-------------+
- | Tom';delete |
- +-------------+
- | Tom';delete |
- +-------------+
- 1 row in set (0.00 sec)
- public class ResultSetRegressionTest extends BaseTestCase {
- public void testBug33678() throws Exception {
- if (!versionMeetsMinimum(4, 1)) {
- return;
- }
- createTable("testBug33678", "(field1 INT)");
- // allowMultiQueries=true设置
- Connection multiConn = getConnectionWithProps("allowMultiQueries=true");
- Statement multiStmt = multiConn.createStatement();
- try {
- multiStmt.setFetchSize(Integer.MIN_VALUE);
- // 一次性执行多条SQL语句
- multiStmt
- .execute("SELECT 1 UNION SELECT 2; INSERT INTO testBug33678 VALUES (1); UPDATE testBug33678 set field1=2; INSERT INTO testBug33678 VALUES(3); UPDATE testBug33678 set field1=2 WHERE field1=3; UPDATE testBug33678 set field1=2; SELECT 1");
- // 以下代码省略...
- }
- }
转载于:https://my.oschina.net/zchuanzhao/blog/512560
MySQL for Java的SQL注入测试相关推荐
- 怎么进行mysql注入测试_MySQL for Java的SQL注入测试
只要你学JDBC,基本上所有的人都会和你说,Statement不能防止SQL注入, PreparedStatement能够防止SQL注入. 基本上参加工作了一段时间之后还是这么认为的, 没错, 这句是 ...
- SqlMap自动化SQL注入测试工具简绍
Sqlmap是一个开源的渗透测试工具,可以自动检测和利用SQL注入漏洞并接管数据库服务器.它配备了强大的检测引擎,为终极渗透测试仪提供了许多小众功能,以及从数据库指纹识别,从数据库获取数据到访问底层文 ...
- java 防止sql注入_Java中SQL注入以及如何轻松防止它
java 防止sql注入 什么是SQL注入? (What is SQL Injection?) SQL Injection is one of the top 10 web application v ...
- SQL注入测试神器sqlmap
点击上方蓝字"开源优测"一起玩耍 声明 本公众号所有内容,均属微信公众号: 开源优测 所有,任何媒体.网站或个人未经授权不得转载.链接.转贴或以其他方式复制发布/发表.已经本公众 ...
- 如何防范SQL注入 SQL注入测试
从测试来进行测试SQL注入. 首先,看看SQL注入攻击能分为以下三种类型: Inband: 数据经由SQL代码注入的通道取出,这是最直接的一种攻击,通过SQL注入获取的信息直接反映到应用程序的Web页 ...
- golang mysql 防注入_Go,Gorm 和 Mysql 是如何防止 SQL 注入的
Go,Gorm 和 Mysql 是如何防止 SQL 注入的 SQL 注入和 SQL 预编译技术 什么是 SQL 注入 所谓SQL注入(sql inject),就是通过把SQL命令插入到Web表单提交或 ...
- 安全测试中sql注入测试思路
在找好需要测试的功能点之后,针对每种功能点(参数),sql注入测试一般遵循下面步骤: 1. 测试注入类型,数字型or字符型 如果参数中直接包含字母,那么直接可以判断是字符型参数,如id=4a. 若参数 ...
- [Mysql] 防御和检查SQL注入攻击的手段
SQL注入攻击的种类 知彼知己,方可取胜.首先要清楚SQL注入攻击有哪些种类. 1.没有正确过滤转义字符 在用户的输入没有为转义字符过滤时,就会发生这种形式的注入式攻击,它会被传递给一个SQL语句.这 ...
- 安全测试之sql注入测试
这篇文章,主要是来总结一下,sql注入应该如何去测试. 测试端:数据库服务端 测试点:sql注入 使用工具:burpsuit或Appscan工具或手工或sqlmap 测试方案: 1.使用ibatis或 ...
最新文章
- python内置库之学习ctypes库(二)
- undefined reference to cv::_InputArray::_InputArray(cv::Mat const)
- CTF题目中遇到的PHP考点总结(一)
- CentOS经常使用文件操作命令[百度博客搬家]
- python命令行tab补全_python命令行下按tab建补全的方法
- 我的世界java和基岩版哪个好玩_我的世界:Java版本好玩还是基岩版好玩?老玩家看完后沉默了...
- Spring 整合Mybatis Mapper动态代理方法
- Expected object of backend CPU but got backend CUDA for argument #4 ‘mat1‘
- DataList 编辑记录时,更新取不到值的原因。
- cs61a 课时笔记 对象的抽象
- php 国际标准时间_时区-如何在PHP中获得格林威治标准时间?
- android跑马灯效果不起作用,Android实现跑马灯效果的方法
- 转---电脑的MAC实际地址可以修改吗?
- 关于安全领域方向上学习的一点个人见解
- 模拟量输出模块支持西门子三菱PLC接DCS控制系统
- html怎么用空格占位符,HTML空格占位符
- 〖Python接口自动化测试实战篇①〗- 自动化测试基础扫盲及项目的生命周期详述
- 秒杀系统(SecKillGoods)
- 计算机统考木桶效应ppt,木桶效应 课件..ppt
- 服务器ras的性能,处理器的RAS性能的重要性
热门文章
- php 用户认证,PHP用户认证及管理完全源码
- 面向对象程序设计c 语言描述 答案,c面向对象程序设计习题解答全.doc
- maven创建Java 和 Web Project, 并导入Eclipse
- oracle的一些常见问题及处理
- sequelize 外键关联_用Sequelize计算关联条目
- rf框架搭建_Robot framework(RF)基本使用
- Hie with the Pie(poj3311)
- html字段值换行代码怎么写,HTML段落,换行,字符实体
- 遇到一个php的错误,php初学者常见的几个错误及解决方法
- vue ---- 监听器