python拼接sql,防注入

sql注入一般都是会有一些特殊字符的,后端拿到这个参数,一定是字符串类型。
那么在处理的时候就以这个为突破点:
1. 判断数据类型,像是int,boolean,浮点型等,直接传入用户传来的参数,很容易出问题,
  执行之前可以先判断数据类型是否正确,不正确直接报错,不再执行
a = '1 or 1=1'
b = 'select * from table1 where id={}'.format(a)
print(b)


# 结果:select * from table1 where id=1 or 1=1  sql注入
2. 如果觉得每个字段都判断类型很麻烦,那么可以在传入参数的时候,加上引号
a = '1 and 1=1'
b = 'select * from table1 where id="{}"'.format(a)
print(b)

# 结果:select * from table1 where id="1 and 1=1"

3. 如果是boolean,建议自己拼接

a = '1'
b = 'select * from table1 where id="{}"'.format(a)
c = b + 'and bo=true'
print(c)



不拼接的话:
select * from temp_0716 where bo='true'
select * from temp_0716 where bo='false'

这两条语句执行的结果,查到的是同一条数据。但是把引号去掉就不是同一条了

 

 

posted @ 2022-07-16 12:26  10132714  阅读(794)  评论(0编辑  收藏  举报