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

« back to all changes in this revision

Viewing changes to lib/core/target.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: target.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
import os
 
28
import re
 
29
import time
 
30
 
 
31
from lib.core.common import dataToSessionFile
 
32
from lib.core.common import paramToDict
 
33
from lib.core.common import parseTargetUrl
 
34
from lib.core.common import readInput
 
35
from lib.core.convert import urldecode
 
36
from lib.core.data import conf
 
37
from lib.core.data import kb
 
38
from lib.core.data import logger
 
39
from lib.core.data import paths
 
40
from lib.core.dump import dumper
 
41
from lib.core.exception import sqlmapFilePathException
 
42
from lib.core.exception import sqlmapGenericException
 
43
from lib.core.exception import sqlmapSyntaxException
 
44
from lib.core.session import resumeConfKb
 
45
 
 
46
 
 
47
def __setRequestParams():
 
48
    """
 
49
    Check and set the parameters and perform checks on 'data' option for
 
50
    HTTP method POST.
 
51
    """
 
52
 
 
53
    __testableParameters = False
 
54
 
 
55
    # Perform checks on GET parameters
 
56
    if conf.parameters.has_key("GET") and conf.parameters["GET"]:
 
57
        parameters = conf.parameters["GET"]
 
58
        __paramDict = paramToDict("GET", parameters)
 
59
 
 
60
        if __paramDict:
 
61
            conf.paramDict["GET"] = __paramDict
 
62
            __testableParameters = True
 
63
 
 
64
    # Perform checks on POST parameters
 
65
    if conf.method == "POST" and not conf.data:
 
66
        errMsg = "HTTP POST method depends on HTTP data value to be posted"
 
67
        raise sqlmapSyntaxException, errMsg
 
68
 
 
69
    if conf.data:
 
70
        urlDecodedData = urldecode(conf.data).replace("%", "%%")
 
71
        conf.parameters["POST"] = urlDecodedData
 
72
        __paramDict = paramToDict("POST", urlDecodedData)
 
73
 
 
74
        if __paramDict:
 
75
            conf.paramDict["POST"] = __paramDict
 
76
            __testableParameters = True
 
77
 
 
78
    # Perform checks on Cookie parameters
 
79
    if conf.cookie:
 
80
        # TODO: sure about decoding the cookie?
 
81
        #urlDecodedCookie = urldecode(conf.cookie).replace("%", "%%")
 
82
        urlDecodedCookie = conf.cookie.replace("%", "%%")
 
83
        conf.parameters["Cookie"] = urlDecodedCookie
 
84
        __paramDict = paramToDict("Cookie", urlDecodedCookie)
 
85
 
 
86
        if __paramDict:
 
87
            conf.paramDict["Cookie"] = __paramDict
 
88
            __testableParameters = True
 
89
 
 
90
    # Perform checks on User-Agent header value
 
91
    if conf.httpHeaders:
 
92
        for httpHeader, headerValue in conf.httpHeaders:
 
93
            if httpHeader == "User-Agent":
 
94
                conf.parameters["User-Agent"] = urldecode(headerValue).replace("%", "%%")
 
95
 
 
96
                condition  = not conf.testParameter
 
97
                condition |= "User-Agent" in conf.testParameter
 
98
                condition |= "user-agent" in conf.testParameter
 
99
                condition |= "useragent" in conf.testParameter
 
100
                condition |= "ua" in conf.testParameter
 
101
 
 
102
                if condition:
 
103
                    conf.paramDict["User-Agent"] = { "User-Agent": headerValue }
 
104
                    __testableParameters = True
 
105
 
 
106
    if not conf.parameters:
 
107
        errMsg  = "you did not provide any GET, POST and Cookie "
 
108
        errMsg += "parameter, neither an User-Agent header"
 
109
        raise sqlmapGenericException, errMsg
 
110
 
 
111
    elif not __testableParameters:
 
112
        errMsg  = "all testable parameters you provided are not present "
 
113
        errMsg += "within the GET, POST and Cookie parameters"
 
114
        raise sqlmapGenericException, errMsg
 
115
 
 
116
 
 
117
def __setOutputResume():
 
118
    """
 
119
    Check and set the output text file and the resume functionality.
 
120
    """
 
121
 
 
122
    if conf.sessionFile and os.path.exists(conf.sessionFile):
 
123
        readSessionFP = open(conf.sessionFile, "r")
 
124
        lines = readSessionFP.readlines()
 
125
 
 
126
        for line in lines:
 
127
            if line.count("][") == 4:
 
128
                line = line.split("][")
 
129
 
 
130
                if len(line) != 5:
 
131
                    continue
 
132
 
 
133
                url, _, _, expression, value = line
 
134
 
 
135
                if not value:
 
136
                    continue
 
137
 
 
138
                if url[0] == "[":
 
139
                    url = url[1:]
 
140
 
 
141
                if value[-1] == "\n":
 
142
                    value = value[:-1]
 
143
 
 
144
                if url != conf.url:
 
145
                    continue
 
146
 
 
147
                if url not in kb.resumedQueries.keys():
 
148
                    kb.resumedQueries[url] = {}
 
149
                    kb.resumedQueries[url][expression] = value
 
150
 
 
151
                resumeConfKb(expression, url, value)
 
152
 
 
153
                if expression not in kb.resumedQueries[url].keys():
 
154
                    kb.resumedQueries[url][expression] = value
 
155
                elif len(value) >= len(kb.resumedQueries[url][expression]):
 
156
                    kb.resumedQueries[url][expression] = value
 
157
 
 
158
        readSessionFP.close()
 
159
 
 
160
    if conf.sessionFile:
 
161
        try:
 
162
            conf.sessionFP = open(conf.sessionFile, "a")
 
163
            dataToSessionFile("\n[%s]\n" % time.strftime("%X %x"))
 
164
        except IOError:
 
165
            errMsg = "unable to write on the session file specified"
 
166
            raise sqlmapFilePathException, errMsg
 
167
 
 
168
 
 
169
def __createFilesDir():
 
170
    """
 
171
    Create the file directory.
 
172
    """
 
173
 
 
174
    if not conf.rFile:
 
175
        return
 
176
 
 
177
    conf.filePath = paths.SQLMAP_FILES_PATH % conf.hostname
 
178
 
 
179
    if not os.path.isdir(conf.filePath):
 
180
        os.makedirs(conf.filePath, 0755)
 
181
 
 
182
 
 
183
def __createDumpDir():
 
184
    """
 
185
    Create the dump directory.
 
186
    """
 
187
 
 
188
    if not conf.dumpTable and not conf.dumpAll:
 
189
        return
 
190
 
 
191
    conf.dumpPath = paths.SQLMAP_DUMP_PATH % conf.hostname
 
192
 
 
193
    if not os.path.isdir(conf.dumpPath):
 
194
        os.makedirs(conf.dumpPath, 0755)
 
195
 
 
196
 
 
197
def initTargetEnv():
 
198
    """
 
199
    Initialize target environment.
 
200
    """
 
201
 
 
202
    if conf.multipleTargets:
 
203
        conf.paramDict    = {}
 
204
        conf.parameters   = {}
 
205
        kb.dbms           = None
 
206
        kb.dbmsDetected   = False
 
207
        kb.dbmsVersion    = None
 
208
        kb.injParameter   = None
 
209
        kb.injPlace       = None
 
210
        kb.injType        = None
 
211
        kb.parenthesis    = None
 
212
        kb.unionComment   = ""
 
213
        kb.unionCount     = None
 
214
        kb.unionPosition  = None
 
215
 
 
216
    parseTargetUrl()
 
217
    __setRequestParams()
 
218
    __setOutputResume()
 
219
 
 
220
 
 
221
def createTargetDirs():
 
222
    """
 
223
    Create the output directory.
 
224
    """
 
225
 
 
226
    conf.outputPath = "%s%s%s" % (paths.SQLMAP_OUTPUT_PATH, os.sep, conf.hostname)
 
227
 
 
228
    if not os.path.isdir(paths.SQLMAP_OUTPUT_PATH):
 
229
        os.makedirs(paths.SQLMAP_OUTPUT_PATH, 0755)
 
230
 
 
231
    if not os.path.isdir(conf.outputPath):
 
232
        os.makedirs(conf.outputPath, 0755)
 
233
 
 
234
    dumper.setOutputFile()
 
235
 
 
236
    __createDumpDir()
 
237
    __createFilesDir()