2008-10-15 19:38:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2008-10-15 19:56:32 +04:00
|
|
|
$Id$
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
02/2006 Will Holcomb <wholcomb@gmail.com>
|
|
|
|
|
2008-12-21 19:35:03 +03:00
|
|
|
Reference: http://odin.himinbi.org/MultipartPostHandler.py
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2008-12-21 19:35:03 +03:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import mimetools
|
|
|
|
import mimetypes
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import sys
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
|
|
|
|
from lib.core.exception import sqlmapDataException
|
|
|
|
|
|
|
|
|
|
|
|
class Callable:
|
|
|
|
def __init__(self, anycallable):
|
|
|
|
self.__call__ = anycallable
|
|
|
|
|
|
|
|
# Controls how sequences are uncoded. If true, elements may be given
|
|
|
|
# multiple values by assigning a sequence.
|
|
|
|
doseq = 1
|
|
|
|
|
|
|
|
|
|
|
|
class MultipartPostHandler(urllib2.BaseHandler):
|
|
|
|
handler_order = urllib2.HTTPHandler.handler_order - 10 # needs to run first
|
|
|
|
|
|
|
|
def http_request(self, request):
|
|
|
|
data = request.get_data()
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if data is not None and type(data) != str:
|
|
|
|
v_files = []
|
|
|
|
v_vars = []
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
try:
|
2009-04-28 03:05:11 +04:00
|
|
|
for(key, value) in data.items():
|
|
|
|
if type(value) == file:
|
|
|
|
v_files.append((key, value))
|
|
|
|
else:
|
|
|
|
v_vars.append((key, value))
|
2008-10-15 19:38:22 +04:00
|
|
|
except TypeError:
|
|
|
|
systype, value, traceback = sys.exc_info()
|
|
|
|
raise sqlmapDataException, "not a valid non-string sequence or mapping object", traceback
|
|
|
|
|
|
|
|
if len(v_files) == 0:
|
|
|
|
data = urllib.urlencode(v_vars, doseq)
|
|
|
|
else:
|
|
|
|
boundary, data = self.multipart_encode(v_vars, v_files)
|
|
|
|
contenttype = 'multipart/form-data; boundary=%s' % boundary
|
|
|
|
#if (request.has_header('Content-Type') and request.get_header('Content-Type').find('multipart/form-data') != 0):
|
|
|
|
# print "Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data')
|
|
|
|
request.add_unredirected_header('Content-Type', contenttype)
|
|
|
|
|
|
|
|
request.add_data(data)
|
|
|
|
return request
|
|
|
|
|
|
|
|
def multipart_encode(vars, files, boundary = None, buffer = None):
|
|
|
|
if boundary is None:
|
|
|
|
boundary = mimetools.choose_boundary()
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if buffer is None:
|
|
|
|
buffer = ''
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
for(key, value) in vars:
|
|
|
|
buffer += '--%s\r\n' % boundary
|
|
|
|
buffer += 'Content-Disposition: form-data; name="%s"' % key
|
|
|
|
buffer += '\r\n\r\n' + value + '\r\n'
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
for(key, fd) in files:
|
|
|
|
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
|
|
|
|
filename = fd.name.split('/')[-1]
|
|
|
|
contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
|
|
|
buffer += '--%s\r\n' % boundary
|
|
|
|
buffer += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename)
|
|
|
|
buffer += 'Content-Type: %s\r\n' % contenttype
|
|
|
|
# buffer += 'Content-Length: %s\r\n' % file_size
|
|
|
|
fd.seek(0)
|
|
|
|
buffer += '\r\n' + fd.read() + '\r\n'
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
buffer += '--%s--\r\n\r\n' % boundary
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
return boundary, buffer
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
multipart_encode = Callable(multipart_encode)
|
|
|
|
|
|
|
|
https_request = http_request
|