首页 > 数据库 >防止SQL注入笔记类1

防止SQL注入笔记类1

时间:2023-11-30 09:59:11浏览次数:32  
标签:args org SQL 笔记 sql apache import class 注入

import com.alibaba.druid.wall.Violation;

import com.alibaba.druid.wall.WallCheckResult;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
import com.alibaba.druid.wall.violation.SyntaxErrorViolation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;


/**
* @date 2023-08-16 sql 注入检测
* @author tom
*/

@Component
@Intercepts({@Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class SqlInjInterceptor implements Interceptor {

private static final Pattern PATTERN = Pattern.compile("[\t\r\n]");
private final static WallProvider PROVIDER = new MySqlWallProvider(new WallConfig(MySqlWallProvider.DEFAULT_CONFIG_DIR));



public SqlInjInterceptor() {
}

public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement)args[0];
Object parameter = args[1];
BoundSql boundSql;
if (args.length == 4) {
boundSql = ms.getBoundSql(parameter);
} else {
boundSql = (BoundSql)args[5];
}

sqlInj(boundSql.getSql());
return invocation.proceed();
}

public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

public void setProperties(Properties properties) {
}

private static void sqlInj(String sql) throws SQLException {
sql = removeDataAuthFlag(sql);
WallCheckResult checkResult = PROVIDER.check(sql);
List<Violation> violations = checkResult.getViolations();

if (violations.size() == 0){
return;
}
Violation firstViolation = violations.get(0);
if (violations.get(0) instanceof SyntaxErrorViolation) {
SyntaxErrorViolation violation = (SyntaxErrorViolation) violations.get(0);
throw new SQLException("sql injection violation: " + firstViolation.getMessage(),
violation.getException());
} else {
throw new SQLException("sql injection violation: " + firstViolation.getMessage());
}
}


// 数据权限拦截误判
private static String removeDataAuthFlag(String sql) {
if (StringUtils.isBlank(sql)) {
return sql;
}
if (!sql.contains("(@") && !sql.contains("@)")) {
return sql;
}
sql = sql.toLowerCase();
for (;;) {
if (sql.contains("(@") && sql.contains("@)")) {
String front = sql.substring(0, sql.indexOf("(@"));
String after = sql.substring(sql.indexOf("@)") + 2);
// and 处理:
// 若 (@ 前面是 where, @) 后方是 and, 要去除 后方的 and
// 若 (@ 前面是 and, 移除前方的 and
if (front.trim().endsWith("where") && after.trim().startsWith("and")) {
after = after.substring(after.indexOf("and") + 3);
}
if (front.trim().endsWith("and")) {
front = front.substring(0, front.lastIndexOf("and"));
}
sql = front + after;
} else {
break;
}
}
return sql;
}


public static void main(String[] args) {

String sql = "select 1 from dual\n" +
"where (@{\"columnCode\":\"wh_code\",\"tableName\":\"cd_warehouse\",\"custom\":\"rh.wh_code\",\"dataCode\":\"114444411\"}@) " +
"AND 1=2 \r\n\t and (@{\"columnCode\":\"wh_code\",\"tableName\":\"cd_warehouse\",\"custom\":\"rh.wh_code\",\"dataCode\":\"22882222\"}@) " +
" and 1 = 2 \n order by =sleep(1)";

System.out.println(sql);
sql = removeDataAuthFlag(sql);
System.out.println(sql);
}

}

标签:args,org,SQL,笔记,sql,apache,import,class,注入
From: https://www.cnblogs.com/huangwentian/p/17866581.html

相关文章

  • 防止SQL注入笔记类2
    importcom.mideaframework.core.web.JsonResponse;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.http.HttpStatus;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.ControllerAdvi......
  • sqlsever单表改名及备份
    --改名EXECsp_rename'old_table_name','new_table_name';--备份select*intodbo.t_scs_0304fromdbo.t_scs;对表结构字段进行修改:添加列:altertable表名add列名varchar(55)删除列:altertable表名dropcolumn列名改列类型:altertable表名altercolumn列名varc......
  • 2024年系统节假日sql
    CREATETABLE`system_workday`(`day_id`int(11)NOTNULLAUTO_INCREMENT,`day_day`int(11)DEFAULTNULLCOMMENT'时间年月日',`day_type`int(4)DEFAULT'0'COMMENT'类型0.工作日1周末休息2节假日',PRIMARYKEY(`day_id`)) COMMENT=......
  • ubuntu server 22 LTS 安装MySQL8(二进制源码方式)
    原作来源:https://github.com/aminglinux/daily_shell/blob/main/29.sh根据我自己情况稍作修改mysql下载地址:https://downloads.mysql.com/archives/community/ 按照顺序执行逐行执行注意执行过程的提示,报错需处理:tar-xvfmysql-8.0.34-linux-glibc2.17-x86_64.tarsudo......
  • 《软件工程:一种实践方法》读书笔记三
    第五章:软件开发过程中的质量质量定义:在软件开发中,质量是指满足明确或隐含的需求的能力或特性。这包括产品的正确性、可靠性、可维护性、可重用性、可扩展性和易用性等。质量的重要性:如果一个软件产品在质量方面存在问题,可能会导致失败,给组织带来严重的影响。因此,在软件开发过程......
  • enote笔记法之附录2——5w1h2k关联词(ver0.22)
    enote笔记法之附录2——5w1h2k关联词(ver0.22)最上面的是截屏的完整版,分割线下面的是纯文字版本:  作者姓名(本人的真实姓名):胡佳吉 居住地:上海作者网名:EverSteins版权声明:enote笔记法之附录2——5w1h2k关联词(ver0.22)的发表日期为为2023年11月29日。以上的所有内容全部都是......
  • mysql 页级锁
    页级锁是MySQL中锁定粒度介于行级锁和表级锁中间的一种锁。表级锁速度快,但冲突多,行级冲突少,但速度慢。因此,采取了折衷的页级锁,一次锁定相邻的一组记录。BDB引擎支持页级锁。 从上到下,锁的粒度逐渐细粒化,但实现开销逐渐增大。 同时我们也要须知,表锁,页锁,行锁并不是一个具......
  • 【Python爬虫】第13篇:scrapy项目配置和数据获取。从0到scrapy高手笔记(附代码,可自取)
    本文主要学习一下关于爬虫的相关前置知识和一些理论性的知识,通过本文我们能够知道什么是爬虫,都有那些分类,爬虫能干什么等,同时还会站在爬虫的角度复习一下http协议。全套笔记和代码自取地址:请移步这里感兴趣的小伙伴可以自取哦,欢迎大家点赞转发~共8章,37子模块scrapy爬......
  • 第三次python笔记
    python中的conditions:1.什么是contions?:所谓的condition即是条件变量,这种机制是在满足了特定的条件后,线程才可以访问相关的数据。这种同步机制就是一个线程等待特定的条件,另一个线程通知它条件已经发生。一旦条件发生,该线程就会获取锁,从而独占共享资源的访问。 Condition......
  • dwva 的SQL注入一关解决” Illegal mix of collations for operation ‘UNION’“ 问
    我是在phpstudy上面搭建的dvwa背景是我在一天晚上练习SQL注入出现了”Illegalmixofcollationsforoperation‘UNION’“问题就在网上搜索解决办法,发现是因为users表和table表的编码方式不一样导致的网上查到了table_name的排序编码,然后下载解压phpMyAdmin放到WWW目录下......