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 ...
随机推荐
- U3D 动画帧事件问题
测试版本U3D5.4. 1,为一个模型导入外部动画.为动画剪辑attack在某帧添加event,事件为 public void OnAttackEvent(){},函数体不做任何事情. 结果发现,在动 ...
- PhoneGap: Android平台入门例子(Hello World)
Hello World Demo: http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html#Gett ...
- 0 bug 读后感
本书全名是 <0 bug- C/C++商用工程之道>,这是一本有争议的书,豆瓣链接: http://book.douban.com/subject/4149139/ ,建议有一些商用的开发 ...
- 论javascript中的原始值和对象
javascript将数据类型分为两类:原始值(undefined.null.布尔值.数字和字符串),对象(对象.函数和数组) 论点:原始值不可以改变,对象可以改变:对象为引用类型: '原始值不可以改 ...
- js基础知识温习:js中的对象
在JavaScript中对象是一个无序属性的集合,其属性可以包含基本值.对象或者函数. 对象最简单的创建方式 JavaScript中创建对象最简单的方式就是创建一个Object对象的实例,然后再添加属 ...
- Bootstrap系列 -- 1. 如何使用Bootstrap
一. Bootstrap 简介 Bootstrap 是一个前端框架,使用Bootstrap可以做出很多漂亮的页面,中文官网:http://www.bootcss.com/ 二. Bootstrap核心 ...
- 项目分布式部署那些事(1):ONS消息队列、基于Redis的Session共享,开源共享
因业务发展需要现在的系统不足以支撑现在的用户量,于是我们在一周之前着手项目的性能优化与分布式部署的相关动作. 概况 现在的系统是基于RabbitHub(一套开源的开发时框架)和Rabbit.WeiXi ...
- Linux 配置只安装 64 位软件包
Centos.RHEL等系统,yum 安装的时候有时候会安装32的,然而我们只需要安装64位的软件! 解决方法: yum只安装 64 位的包,只需在 /etc/yum.conf 中加入 "e ...
- Rest API 开发 学习笔记(转)
Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...
- C#高级特性_Attribute
Attribute: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microsoft .NET ...