python sql注入检测_在源代码中检测SQL注入

博客介绍了使用Python的pyparsing库进行SQL注入检测的方法。通过解析传递给cursor.execute的参数,查找存在风险的参数形式,如使用字符串插值、字符串与变量名串联等。还给出了具体的代码示例,并展示了如何扫描示例代码并报告不安全的声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不确定这将如何与其他包进行比较,但在某种程度上,您需要解析传递给cursor.execute的参数。这段pyparsing代码寻找:使用字符串插值的参数

使用字符串与变量名串联的参数

只是变量名的参数

但是有时参数使用字符串连接只是为了将一个长字符串分解成-如果表达式中的所有字符串都是被加在一起的文本,就没有SQL注入的风险。在

此pyparsing片段将查找对光标.执行,然后查找存在风险的论点形式:from pyparsing import *

import re

identifier = Word(alphas, alphanums+'_')

integer = Word(nums)

LPAR,RPAR,PLUS,PERCENT = map(Literal, '()+%')

stringInterpRE = re.compile(r"%-?\d*\*?\.?\d*\*?s")

def containsStringInterpolation(s,l,tokens):

if not stringInterpRE.search(tokens[0]):

raise ParseException(s,l,"No string interpolation")

tupleContents = identifier | integer

tupleExpr = LPAR + delimitedList(tupleContents) + RPAR

stringInterpArg = identifier | tupleExpr

interpolatedString = originalTextFor(quotedString.copy().setParseAction(containsStringInterpolation) +

PERCENT + stringInterpArg)

stringTerm = interpolatedString | OneOrMore(quotedString.copy()) | identifier

stringTerm.setName("stringTerm")

unsafeStringExpr = (stringTerm + OneOrMore(PLUS + stringTerm)) | identifier | interpolatedString

def unsafeExpr(s,l,tokens):

if not any(term == interpolatedString or term == identifier

for term in tokens):

raise ParseException(s,l,"No unsafe string terms")

unsafeStringExpr.setParseAction(unsafeExpr)

unsafeStringExpr.setName("unsafeExpr")

func = Literal("cursor.execute")

statement = func + LPAR + unsafeStringExpr + RPAR

statement.setName("execute stmt")

#statement.ignore(pythonComment)

for tokens in statement.searchString(sample):

print ' '.join(tokens.asList())

这将扫描以下示例:

^{pr2}$

并报告这些不安全的声明:cursor.execute ( "SELECT * FROM TEST WHERE ID = '%s' -- UNSAFE" % id )

cursor.execute ( "SELECT * FROM TEST WHERE ID = '" + id + "' -- UNSAFE" )

cursor.execute ( sqlVar + " -- UNSAFE" )

cursor.execute ( "SELECT * FROM TEST " "WHERE ID = " + "'%s' -- UNSAFE" % name )

您也可以让pyparsing报告找到的行的位置,使用scanString而不是searchString。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值