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

Skip to content

Commit 6d0ea86

Browse files
committed
Fixes #59 - proper customizable redirect (302 and 301)
1 parent 417f7fa commit 6d0ea86

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

lib/core/option.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@
6969
from lib.parse.queriesfile import queriesParser
7070
from lib.request.proxy import ProxyHTTPSHandler
7171
from lib.request.certhandler import HTTPSCertAuthHandler
72+
from lib.request.redirecthandler import SmartRedirectHandler
7273
from lib.utils.google import Google
7374

7475
authHandler = urllib2.BaseHandler()
7576
proxyHandler = urllib2.BaseHandler()
77+
redirectHandler = SmartRedirectHandler()
7678

7779
def __urllib2Opener():
7880
"""
@@ -81,6 +83,7 @@ def __urllib2Opener():
8183

8284
global authHandler
8385
global proxyHandler
86+
global redirectHandler
8487

8588
debugMsg = "creating HTTP requests opener object"
8689
logger.debug(debugMsg)
@@ -89,7 +92,7 @@ def __urllib2Opener():
8992
opener = urllib2.build_opener(proxyHandler, authHandler)
9093
else:
9194
conf.cj = cookielib.LWPCookieJar()
92-
opener = urllib2.build_opener(proxyHandler, authHandler, urllib2.HTTPCookieProcessor(conf.cj))
95+
opener = urllib2.build_opener(proxyHandler, authHandler, urllib2.HTTPCookieProcessor(conf.cj), redirectHandler)
9396

9497
urllib2.install_opener(opener)
9598

lib/request/certhandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323
"""
2424

25-
import sys
2625
import httplib
2726
import urllib2
27+
import sys
2828

2929
from lib.core.data import conf
3030

lib/request/connect.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ def getPage(**kwargs):
125125
req = urllib2.Request(url, post, headers)
126126
conn = urllib2.urlopen(req)
127127

128+
if hasattr(conn, "redurl"):
129+
infoMsg = "connection redirected, going to use "
130+
infoMsg += "%s as target address" % conn.redurl
131+
logger.info(infoMsg)
132+
133+
conf.url = conn.redurl
134+
135+
return Connect.__getPageProxy(**kwargs)
136+
128137
# Reset the number of connection retries
129138
conf.retriesCount = 0
130139

@@ -163,7 +172,7 @@ def getPage(**kwargs):
163172
code = conn.code
164173
status = conn.msg
165174
responseHeaders = conn.info()
166-
175+
167176
encoding = responseHeaders.get("Content-Encoding")
168177
page = decodePage(page, encoding)
169178

lib/request/redirecthandler.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
$Id$
5+
6+
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
7+
8+
Copyright (c) 2007-2010 Bernardo Damele A. G. <[email protected]>
9+
Copyright (c) 2006 Daniele Bellucci <[email protected]>
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+
import urllib2
26+
27+
class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
28+
def http_error_301(self, req, fp, code, msg, headers):
29+
result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
30+
31+
if "location" in headers:
32+
result.redurl = headers.getheaders("location")[0].split("?")[0]
33+
elif "uri" in headers:
34+
result.redurl = headers.getheaders("uri")[0].split("?")[0]
35+
36+
return result
37+
38+
def http_error_302(self, req, fp, code, msg, headers):
39+
result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
40+
41+
if "location" in headers:
42+
result.redurl = headers.getheaders("location")[0].split("?")[0]
43+
elif "uri" in headers:
44+
result.redurl = headers.getheaders("uri")[0].split("?")[0]
45+
46+
return result

0 commit comments

Comments
 (0)