SQLChop、SQLWall(Druid)、PHP Syntax Parser Analysis
catalog
. introduction
. sqlchop sourcecode analysis
. SQLWall(Druid)
. PHP Syntax Parser
. SQL Parse and Compile: Parse and compose
. sql-parser
. PEAR SQL_Parser
1. introduction
SQLCHOP, This awesome new tool, sqlchop, is a new SQL injection detection engine, using a pipeline of smart recursive decoding, lexical analysis and semantic analysis. It can detect SQL injection query with extremely high accuracy and high recall with 0day SQLi detection ability, far better than nowadays' SQL injection detection tools, most of which based on regex rules. We proposed a novel algorithm to achieve both blazing fast speed and accurate detection ability using SQL syntax analysis.
0x1: Description
SQLChop is a novel SQL injection detection engine built on top of SQL tokenizing and syntax analysis. Web input (URLPath, body, cookie, etc.) will be first decoded to the raw payloads that web app accepts, then syntactical analysis will be performed on payload to classify result. The algorithm behind SQLChop is based on compiler knowledge and automata theory, and runs at a time complexity of O(N).
0x2: installation
//If using python, you need to install protobuf-python, e.g.:
. wget https://bootstrap.pypa.io/get-pip.py
. python get-pip.py
. sudo pip install protobuf //If using c++, you need to install protobuf, protobuf-compiler and protobuf-devel, e.g.:
. sudo yum install protobuf protobuf-compiler protobuf-devel //make
. Download latest release at https://github.com/chaitin/sqlchop/releases
. Make
. Run python2 test.py or LD_LIBRARY_PATH=./ ./sqlchop_test
Relevant Link:
http://sqlchop.chaitin.com/demo
http://sqlchop.chaitin.com/
https://www.blackhat.com/us-15/arsenal.html#yusen-chen
https://pip.pypa.io/en/stable/installing.html
2. sqlchop sourcecode analysis
The SQLChop alpha testing release includes the c++ header and shared object, a python library, and also some sample usages.
0x1: c++ header
/*
* Copyright (C) 2015 Chaitin Tech.
*
* Licensed under:
* https://github.com/chaitin/sqlchop/blob/master/LICENSE
*
*/ #ifndef __SQLCHOP_SQLCHOP_H__
#define __SQLCHOP_SQLCHOP_H__ #define SQLCHOP_API __attribute__((visibility("default"))) #ifdef __cplusplus
extern "C" {
#endif struct sqlchop_object_t; enum {
SQLCHOP_RET_SQLI = ,
SQLCHOP_RET_NORMAL = ,
SQLCHOP_ERR_SERIALIZE = -,
SQLCHOP_ERR_LENGTH = -,
}; SQLCHOP_API int sqlchop_init(const char config[],
struct sqlchop_object_t **obj);
SQLCHOP_API float sqlchop_score_sqli(const struct sqlchop_object_t *obj,
const char buf[], size_t len);
SQLCHOP_API int sqlchop_classify_request(const struct sqlchop_object_t *obj,
const char request[], size_t rlen,
char *payloads, size_t maxplen,
size_t *plen, int detail); SQLCHOP_API int sqlchop_release(struct sqlchop_object_t *obj); #ifdef __cplusplus
}
#endif #endif // __SQLCHOP_SQLCHOP_H__
Relevant Link:
https://github.com/chaitin/sqlchop/releases
https://github.com/chaitin/sqlchop
3. SQLWall(Druid)
0x1: Introduction
git clone https://github.com/alibaba/druid.git
cd druid && mvn install
Druid提供了WallFilter,它是基于SQL语义分析来实现防御SQL注入攻击的,通过将SQL语句解析为AST语法树,基于语法树规则进行恶意语义分析,得出SQL注入判断
0x2: Test Example
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.test.wall; import java.io.File;
import java.io.FileInputStream; import junit.framework.TestCase; import com.alibaba.druid.util.Utils;
import com.alibaba.druid.wall.Violation;
import com.alibaba.druid.wall.WallCheckResult;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.spi.MySqlWallProvider; public class MySqlResourceWallTest extends TestCase { private String[] items; protected void setUp() throws Exception {
// File file = new File("/home/wenshao/error_sql");
File file = new File("/home/wenshao/scan_result");
FileInputStream is = new FileInputStream(file);
String text = Utils.read(is);
is.close();
items = text.split("\\|\\n\\|");
} public void test_false() throws Exception {
WallProvider provider = new MySqlWallProvider(); provider.getConfig().setConditionDoubleConstAllow(true); provider.getConfig().setUseAllow(true);
provider.getConfig().setStrictSyntaxCheck(false);
provider.getConfig().setMultiStatementAllow(true);
provider.getConfig().setConditionAndAlwayTrueAllow(true);
provider.getConfig().setNoneBaseStatementAllow(true);
provider.getConfig().setSelectUnionCheck(false);
provider.getConfig().setSchemaCheck(true);
provider.getConfig().setLimitZeroAllow(true);
provider.getConfig().setCommentAllow(true); for (int i = ; i < items.length; ++i) {
String sql = items[i];
if (sql.indexOf("''=''") != -) {
continue;
}
// if (i <= 121) {
// continue;
// }
WallCheckResult result = provider.check(sql);
if (result.getViolations().size() > ) {
Violation violation = result.getViolations().get();
System.out.println("error (" + i + ") : " + violation.getMessage());
System.out.println(sql);
break;
}
}
System.out.println(provider.getViolationCount());
// String sql = "SELECT name, '******' password, createTime from user where name like 'admin' AND (CASE WHEN (7885=7885) THEN 1 ELSE 0 END)"; // Assert.assertFalse(provider.checkValid(sql));
} }
Relevant Link:
https://raw.githubusercontent.com/alibaba/druid/master/src/test/java/com/alibaba/druid/test/wall/MySqlResourceWallTest.java
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilter
https://github.com/alibaba/druid
http://www.cnblogs.com/LittleHann/p/3495602.html
http://www.cnblogs.com/LittleHann/p/3514532.html
4. PHP Syntax Parser
<?php
require_once('php-sql-parser.php'); $sql = "select name, sum(credits) from students where name='Marcin' and lvID='42509';";
echo $sql . "\n";
$start = microtime(true);
$parser = new PHPSQLParser($sql, true);
var_dump($parser->parsed);
echo "parse time simplest query:" . (microtime(true) - $start) . "\n";
?>
Relevant Link:
http://files.cnblogs.com/LittleHann/php-sql-parser-20131130.zip
5. SQL Parse and Compile: Parse and compose
This package can be used to parse and compose SQL queries programatically.
It can take an SQL query and parse it to extract the different parts of the query like the type of command, fields, tables, conditions, etc..
It can also be used to do the opposite, i.e. compose SQL queries from values that define each part of the query.
0x1: Features
I. Parser
- insert
- replace
- update
- delete
- select
- union
- subselect
- recognizes flow control function (IF, CASE - WHEN - THEN)
- recognition of many sql functions II. Composer (Compiler)
- insert
- replace
- update
- delete
- select
- union III. Wrapper SQL
- object oriented writing of SQL statements from the scratch
0x2: Example
#################################################
$insertObject = new Sql();
$insertObject
->setCommand("insert")
->addTableNames("employees")
->addColumnNames(array("LastName","FirstName"))
->addValues(
array(
array("Value"=>"Davolio","Type"=>"text_val"),
array("Value"=>"Nancy","Type"=>"text_val"),
)
);
$sqlout = $insertObject->compile();
################################################# result:
echo $sqlout;
#################################################
INSERT INTO employees (LastName, FirstName) VALUES ('Davolio', 'Nancy')
#################################################
Relevant Link:
http://www.phpclasses.org/package/5007-PHP-Parse-and-compose-SQL-queries-programatically.html
6. sql-parser
A validating SQL lexer and parser with a focus on MySQL dialect
Relevant Link:
https://github.com/dmitry-php/sql-parser
https://github.com/udan11/sql-parser/wiki/Overview
https://github.com/udan11/sql-parser/wiki/Examples
7. PEAR SQL_Parser
Relevant Link:
https://pear.php.net/package/SQL_Parser/docs/latest/elementindex_SQL_Parser.html
https://pear.php.net/package/SQL_Parser/docs/latest/__filesource/fsource_SQL_Parser__SQL_Parser-0.6.0SQLParserDialectANSI.php.html
https://pear.php.net/package/SQL_Parser/docs/latest/SQL_Parser/SQL_Parser_Compiler.html
https://pear.php.net/package/SQL_Parser/docs/latest/__filesource/fsource_SQL_Parser__SQL_Parser-0.6.0SQLParserCompiler.php.html
https://pear.php.net/package/SQL_Parser/docs/latest/__filesource/fsource_SQL_Parser__SQL_Parser-0.6.0SQLParser.php.html
Copyright (c) 2015 LittleHann All rights reserved
SQLChop、SQLWall(Druid)、PHP Syntax Parser Analysis的更多相关文章
- 基于Spring、SpringMVC、MyBatis、Druid、Shrio构建web系统
源码下载地址:https://github.com/shuaijunlan/Autumn-Framework 在线Demo:http://autumn.shuaijunlan.cn 项目介绍 Autu ...
- Druid、BoneCP、DBCP、C3P0等主流数据库对比
关键功能 Druid BoneCP DBCP C3P0 Proxool JBoss LRU 是 否 是 否 是 是 PSCache 是 是 是 是 否 是 PSCache-Oracle-Optimiz ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- SQL数据分析概览——Hive、Impala、Spark SQL、Drill、HAWQ 以及Presto+druid
转自infoQ! 根据 O’Reilly 2016年数据科学薪资调查显示,SQL 是数据科学领域使用最广泛的语言.大部分项目都需要一些SQL 操作,甚至有一些只需要SQL. 本文涵盖了6个开源领导者: ...
- SpringMVC+MyBatis (druid、logback)
数据库连接池是阿里巴巴的druid.日志框架式logback 1.整合SpringMVCspringMybatis-servlet.xml: <?xml version="1.0&qu ...
- 数据库连接池 - (druid、c3p0、dbcp)
概述: 在这里所谓的数据库连接是指通过网络协议与数据库服务之间建立的TCP连接.通常,与数据库服务进行通信的网络协议无需由应用程序本身实现. 原因有三: 实现复杂度大,需要充分理解和掌握相应的通信协议 ...
- 时间序列数据库(TSDB)初识与选择(InfluxDB、OpenTSDB、Druid、Elasticsearch对比)
背景 这两年互联网行业掀着一股新风,总是听着各种高大上的新名词.大数据.人工智能.物联网.机器学习.商业智能.智能预警啊等等. 以前的系统,做数据可视化,信息管理,流程控制.现在业务已经不仅仅满足于这 ...
- SpringBoot:整合Druid、MyBatis
目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...
- JdbcTemplate 、Mybatis、ORM 、Druid 、HikariCP 、Hibernate是什么?它们有什么关系?
JdbcTemplate .Mybatis.ORM .Druid .HikariCP .Hibernate是什么?它们有什么关系? 学完Spring和SpringMVC之后,就急于求成的开始学习起Sp ...
随机推荐
- OS X运行AFNI的AlphaSim提示libgomp.1.dylib找不到的解决办法
运行环境:OS X Mavericks 10.9.4,AFNI 问题描述: 运行AlphaSim命令时,提示以下信息dyld: Library not loaded: /usr/local/lib/l ...
- JS明确指定函数的接受者
由于方法和值为对象的属性值没什么区别,因此很容易提取对象的方法作为回调函数直接传递给高阶函数.但这也很容易忘记应明确指定方法的接受者.例如,一个字符串缓冲对象使用数组来存储字符串. var buffe ...
- ModernUI教程:使用WPF4.0
Modern UI 同时支持WPF4.0和4.5.下载包中包含了这两个版本的程序集.当你使用Nuget下载时,会根据你选择的运行时版本自动选择对应的版本下载.而Visual Studio2012的模板 ...
- iostat命令详解
iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devic ...
- javascript 连等赋值问题(这是从SegmentFault转过来的一个问题)
var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);// - ...
- [转]run for a girl
上了四个星期决不情愿的自习,终于找到了她上自习的规律, 每个星期五肯定在主教楼525第三排中间,其实第一眼看见她已经喜欢她了,在她周围坐了十天之后更觉得她是我喜欢的那种女孩, (不是每天都能见到她), ...
- 阿里巴巴高新能数据源com.alibaba.druid.pool.DruidDataSource的jar包配置
aspectjweaver-1.7.4.jar druid-0.2.9.jar 两个包,用于提供com.alibaba.druid.pool.DruidDataSource
- python 英文字串首字母改为大写
#英文字串首字母改为大写 st = "string" St = st[0].upper() + st[1:] 2016-10-22 后来了解到 python 内部有相关实现,感觉 ...
- 一个奇怪的MySQL错误返回
(0, '') python查询结果,乍一看还以为是下标出错了 一番调试,发现是因为 mysql数据库连接关闭上出了错. 结尾 在对数据库进行操作的时候要注意连接的开启和关闭动作规范
- ios8调用相机报警告: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure you(转)
我这也报了这个警告,但按他的方法并没有起作用,把写到这个地方看是否其他人用的到 错误代码:Snapshotting a view that has not been rendered results ...