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的更多相关文章

  1. 基于Spring、SpringMVC、MyBatis、Druid、Shrio构建web系统

    源码下载地址:https://github.com/shuaijunlan/Autumn-Framework 在线Demo:http://autumn.shuaijunlan.cn 项目介绍 Autu ...

  2. Druid、BoneCP、DBCP、C3P0等主流数据库对比

    关键功能 Druid BoneCP DBCP C3P0 Proxool JBoss LRU 是 否 是 否 是 是 PSCache 是 是 是 是 否 是 PSCache-Oracle-Optimiz ...

  3. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  4. SQL数据分析概览——Hive、Impala、Spark SQL、Drill、HAWQ 以及Presto+druid

    转自infoQ! 根据 O’Reilly 2016年数据科学薪资调查显示,SQL 是数据科学领域使用最广泛的语言.大部分项目都需要一些SQL 操作,甚至有一些只需要SQL. 本文涵盖了6个开源领导者: ...

  5. SpringMVC+MyBatis (druid、logback)

    数据库连接池是阿里巴巴的druid.日志框架式logback 1.整合SpringMVCspringMybatis-servlet.xml: <?xml version="1.0&qu ...

  6. 数据库连接池 - (druid、c3p0、dbcp)

    概述: 在这里所谓的数据库连接是指通过网络协议与数据库服务之间建立的TCP连接.通常,与数据库服务进行通信的网络协议无需由应用程序本身实现. 原因有三: 实现复杂度大,需要充分理解和掌握相应的通信协议 ...

  7. 时间序列数据库(TSDB)初识与选择(InfluxDB、OpenTSDB、Druid、Elasticsearch对比)

    背景 这两年互联网行业掀着一股新风,总是听着各种高大上的新名词.大数据.人工智能.物联网.机器学习.商业智能.智能预警啊等等. 以前的系统,做数据可视化,信息管理,流程控制.现在业务已经不仅仅满足于这 ...

  8. SpringBoot:整合Druid、MyBatis

    目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...

  9. JdbcTemplate 、Mybatis、ORM 、Druid 、HikariCP 、Hibernate是什么?它们有什么关系?

    JdbcTemplate .Mybatis.ORM .Druid .HikariCP .Hibernate是什么?它们有什么关系? 学完Spring和SpringMVC之后,就急于求成的开始学习起Sp ...

随机推荐

  1. .NET Core)的ZooKeeper异步客户端

    支持断线重连.永久watcher.递归操作并且能跨平台(.NET Core)的ZooKeeper异步客户端   阅读目录 什么是ZooKeeper? 项目介绍 提供的功能 使用说明 FAQ 在公司内部 ...

  2. 制作鼠标移动到div上面显示弹出框

    <div class="show-dialog hide"> <header> <div class="note"> < ...

  3. Mininet在创建拓扑的过程中为什么不打印信息了——了解Mininet的log系统

    前言 写这篇博客是为了给我的愚蠢和浪费的6个小时买单! 过程原因分析 我用Mininet创建过不少拓扑了,这次创建的拓扑非常简单,如下图,创建拓扑的代码见github.在以前的拓扑创建过程中,我都是用 ...

  4. Java:泛型

    一.序言 变化一: 在引入范型之前,Java中的类型分为原始类型.复杂类型,其中复杂类型分为数组和类:引入范型后,一个复杂类型可以细分成更多的类型. 例如,原先的List类型,现在细分成List< ...

  5. python动态网页爬取——四六级成绩批量爬取

    需求: 四六级成绩查询网站我所知道的有两个:学信网(http://www.chsi.com.cn/cet/)和99宿舍(http://cet.99sushe.com/),这两个网站采用的都是动态网页. ...

  6. OpenStack 简介

    OpenStack是IaaS(基础设施即服务)组件,让任何人都可以自行建立和提供云端运算服务. 此外,OpenStack也用作建立防火墙内的"私有云"(Private Cloud) ...

  7. MATLAB实现频数直方图——hist的使用

      "hist" is short for "Histogram(直方图.柱状图)". 1.N = hist(Y) bins the elements of Y ...

  8. NIO框架Mina学习

    前言: 找了篇文章看了看,nio框架数Mina用的最多! 代码: 服务端: package com.mina; import java.net.InetSocketAddress; import ja ...

  9. PHP-- 获取http请求头信息

    看官方文档: http://php.net/manual/zh/function.apache-request-headers.php http://php.net/manual/zh/functio ...

  10. php URL判断

    // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠)      if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a ...