SQLChop、SQLWall(Druid)、PHP Syntax Parser Analysis

來源:互聯網
上載者:User

標籤:

catalog

1. introduction2. sqlchop sourcecode analysis3. SQLWall(Druid)4. PHP Syntax 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.:1. wget https://bootstrap.pypa.io/get-pip.py2. python get-pip.py3. sudo pip install protobuf//If using c++, you need to install protobuf, protobuf-compiler and protobuf-devel, e.g.:1. sudo yum install protobuf protobuf-compiler protobuf-devel//make1. Download latest release at https://github.com/chaitin/sqlchop/releases2. Make3. Run python2 test.py or LD_LIBRARY_PATH=./ ./sqlchop_test

Relevant Link:

http://sqlchop.chaitin.com/demohttp://sqlchop.chaitin.com/https://www.blackhat.com/us-15/arsenal.html#yusen-chenhttps://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 __cplusplusextern "C" {#endifstruct sqlchop_object_t;enum {  SQLCHOP_RET_SQLI = 1,  SQLCHOP_RET_NORMAL = 0,  SQLCHOP_ERR_SERIALIZE = -1,  SQLCHOP_ERR_LENGTH = -2,};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/releaseshttps://github.com/chaitin/sqlchop

 

3. SQLWall(Druid)

0x1: Introduction

git clone https://github.com/alibaba/druid.gitcd 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 = 0; i < items.length; ++i) {            String sql = items[i];            if (sql.indexOf("‘‘=‘‘") != -1) {                continue;            }//            if (i <= 121) {//                continue;//            }            WallCheckResult result = provider.check(sql);            if (result.getViolations().size() > 0) {                Violation violation = result.getViolations().get(0);                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.javahttps://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilterhttps://github.com/alibaba/druidhttp://www.cnblogs.com/LittleHann/p/3495602.htmlhttp://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

 

Copyright (c) 2015 LittleHann All rights reserved

 

SQLChop、SQLWall(Druid)、PHP Syntax Parser Analysis

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.