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

Skip to content

Commit 142fc88

Browse files
committed
Fix for an Issue #129
1 parent bdbe8ff commit 142fc88

9 files changed

Lines changed: 105 additions & 95 deletions

File tree

lib/controller/controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
from lib.core.common import randomStr
3232
from lib.core.common import readInput
3333
from lib.core.common import showHttpErrorCodes
34-
from lib.core.convert import urlencode
35-
from lib.core.convert import urldecode
34+
from lib.core.common import urlencode
35+
from lib.core.common import urldecode
3636
from lib.core.data import conf
3737
from lib.core.data import kb
3838
from lib.core.data import logger

lib/core/common.py

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
from lib.core.convert import base64pickle
5353
from lib.core.convert import base64unpickle
5454
from lib.core.convert import htmlunescape
55+
from lib.core.convert import stdoutencode
5556
from lib.core.convert import unicodeencode
56-
from lib.core.convert import urldecode
57-
from lib.core.convert import urlencode
57+
from lib.core.convert import utf8encode
5858
from lib.core.decorators import cachedmethod
5959
from lib.core.enums import CHARSET_TYPE
6060
from lib.core.enums import DBMS
@@ -125,6 +125,8 @@
125125
from lib.core.settings import TEXT_TAG_REGEX
126126
from lib.core.settings import UNION_UNIQUE_FIFO_LENGTH
127127
from lib.core.settings import URI_QUESTION_MARKER
128+
from lib.core.settings import URLENCODE_CHAR_LIMIT
129+
from lib.core.settings import URLENCODE_FAILSAFE_CHARS
128130
from lib.core.threads import getCurrentThreadData
129131
from thirdparty.clientform.clientform import ParseResponse
130132
from thirdparty.clientform.clientform import ParseError
@@ -721,29 +723,10 @@ def dataToStdout(data, forceOutput=False, bold=False):
721723

722724
if not kb.get("threadException"):
723725
if forceOutput or not getCurrentThreadData().disableStdOut:
724-
try:
725-
if kb.get("multiThreadMode"):
726-
logging._acquireLock()
727-
# Reference: http://bugs.python.org/issue1602
728-
if IS_WIN:
729-
output = data.encode('ascii', "replace")
730-
731-
if output != data:
732-
warnMsg = "cannot properly display Unicode characters "
733-
warnMsg += "inside Windows OS command prompt "
734-
warnMsg += "(http://bugs.python.org/issue1602). All "
735-
warnMsg += "unhandled occurances will result in "
736-
warnMsg += "replacement with '?' character. Please, find "
737-
warnMsg += "proper character representation inside "
738-
warnMsg += "corresponding output files. "
739-
singleTimeWarnMessage(warnMsg)
740-
741-
message = output
742-
else:
743-
message = data.encode(sys.stdout.encoding)
744-
except:
745-
message = data.encode(UNICODE_ENCODING)
726+
if kb.get("multiThreadMode"):
727+
logging._acquireLock()
746728

729+
message = stdoutencode(data)
747730
sys.stdout.write(setColor(message, bold))
748731
sys.stdout.flush()
749732

@@ -2010,6 +1993,57 @@ def extractErrorMessage(page):
20101993

20111994
return retVal
20121995

1996+
def urldecode(value, encoding=None):
1997+
result = None
1998+
1999+
if value:
2000+
try:
2001+
# for cases like T%C3%BCrk%C3%A7e
2002+
value = str(value)
2003+
except ValueError:
2004+
pass
2005+
finally:
2006+
result = urllib.unquote_plus(value)
2007+
2008+
if isinstance(result, str):
2009+
result = unicode(result, encoding or UNICODE_ENCODING, "replace")
2010+
2011+
return result
2012+
2013+
def urlencode(value, safe="%&=", convall=False, limit=False):
2014+
if conf.direct or PLACE.SOAP in conf.paramDict:
2015+
return value
2016+
2017+
count = 0
2018+
result = None if value is None else ""
2019+
2020+
if value:
2021+
if convall or safe is None:
2022+
safe = ""
2023+
2024+
# corner case when character % really needs to be
2025+
# encoded (when not representing url encoded char)
2026+
# except in cases when tampering scripts are used
2027+
if all(map(lambda x: '%' in x, [safe, value])) and not kb.tamperFunctions:
2028+
value = re.sub("%(?![0-9a-fA-F]{2})", "%25", value)
2029+
2030+
while True:
2031+
result = urllib.quote(utf8encode(value), safe)
2032+
2033+
if limit and len(result) > URLENCODE_CHAR_LIMIT:
2034+
if count >= len(URLENCODE_FAILSAFE_CHARS):
2035+
break
2036+
2037+
while count < len(URLENCODE_FAILSAFE_CHARS):
2038+
safe += URLENCODE_FAILSAFE_CHARS[count]
2039+
count += 1
2040+
if safe[-1] in value:
2041+
break
2042+
else:
2043+
break
2044+
2045+
return result
2046+
20132047
def beep():
20142048
"""
20152049
Does an audible beep sound
@@ -2094,11 +2128,7 @@ def logHTTPTraffic(requestLogMsg, responseLogMsg):
20942128
dataToTrafficFile("%s%s" % (responseLogMsg, os.linesep))
20952129
dataToTrafficFile("%s%s%s%s" % (os.linesep, 76 * '#', os.linesep, os.linesep))
20962130

2097-
def getPageTemplate(payload, place):
2098-
"""
2099-
Cross-linked method
2100-
"""
2101-
2131+
def getPageTemplate(payload, place): # Cross-linked function
21022132
pass
21032133

21042134
def getPublicTypeMembers(type_, onlyValues=False):

lib/core/convert.py

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
import struct
1818
import urllib
1919

20-
from lib.core.data import conf
21-
from lib.core.data import kb
2220
from lib.core.enums import PLACE
21+
from lib.core.settings import IS_WIN
2322
from lib.core.settings import UNICODE_ENCODING
24-
from lib.core.settings import URLENCODE_CHAR_LIMIT
25-
from lib.core.settings import URLENCODE_FAILSAFE_CHARS
2623

2724
def base64decode(value):
2825
return value.decode("base64")
@@ -62,57 +59,6 @@ def sha1hash(value):
6259
else:
6360
return sha.new(value).hexdigest()
6461

65-
def urldecode(value, encoding=None):
66-
result = None
67-
68-
if value:
69-
try:
70-
# for cases like T%C3%BCrk%C3%A7e
71-
value = str(value)
72-
except ValueError:
73-
pass
74-
finally:
75-
result = urllib.unquote_plus(value)
76-
77-
if isinstance(result, str):
78-
result = unicode(result, encoding or UNICODE_ENCODING, "replace")
79-
80-
return result
81-
82-
def urlencode(value, safe="%&=", convall=False, limit=False):
83-
if conf.direct or PLACE.SOAP in conf.paramDict:
84-
return value
85-
86-
count = 0
87-
result = None if value is None else ""
88-
89-
if value:
90-
if convall or safe is None:
91-
safe = ""
92-
93-
# corner case when character % really needs to be
94-
# encoded (when not representing url encoded char)
95-
# except in cases when tampering scripts are used
96-
if all(map(lambda x: '%' in x, [safe, value])) and not kb.tamperFunctions:
97-
value = re.sub("%(?![0-9a-fA-F]{2})", "%25", value)
98-
99-
while True:
100-
result = urllib.quote(utf8encode(value), safe)
101-
102-
if limit and len(result) > URLENCODE_CHAR_LIMIT:
103-
if count >= len(URLENCODE_FAILSAFE_CHARS):
104-
break
105-
106-
while count < len(URLENCODE_FAILSAFE_CHARS):
107-
safe += URLENCODE_FAILSAFE_CHARS[count]
108-
count += 1
109-
if safe[-1] in value:
110-
break
111-
else:
112-
break
113-
114-
return result
115-
11662
def unicodeencode(value, encoding=None):
11763
"""
11864
Return 8-bit string representation of the supplied unicode value:
@@ -145,3 +91,32 @@ def htmlunescape(value):
14591
codes = (('&lt;', '<'), ('&gt;', '>'), ('&quot;', '"'), ('&nbsp;', ' '), ('&amp;', '&'))
14692
retVal = reduce(lambda x, y: x.replace(y[0], y[1]), codes, retVal)
14793
return retVal
94+
95+
def singleTimeWarnMessage(message): # Cross-linked function
96+
pass
97+
98+
def stdoutencode(data):
99+
retVal = None
100+
101+
try:
102+
# Reference: http://bugs.python.org/issue1602
103+
if IS_WIN:
104+
output = data.encode('ascii', "replace")
105+
106+
if output != data:
107+
warnMsg = "cannot properly display Unicode characters "
108+
warnMsg += "inside Windows OS command prompt "
109+
warnMsg += "(http://bugs.python.org/issue1602). All "
110+
warnMsg += "unhandled occurances will result in "
111+
warnMsg += "replacement with '?' character. Please, find "
112+
warnMsg += "proper character representation inside "
113+
warnMsg += "corresponding output files. "
114+
singleTimeWarnMessage(warnMsg)
115+
116+
retVal = output
117+
else:
118+
retVal = data.encode(sys.stdout.encoding)
119+
except:
120+
retVal = data.encode(UNICODE_ENCODING)
121+
122+
return retVal

lib/core/option.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lib.core.common
2121
import lib.core.threads
22+
import lib.core.convert
2223

2324
from lib.controller.checks import checkConnection
2425
from lib.core.common import Backend
@@ -46,9 +47,10 @@
4647
from lib.core.common import runningAsAdmin
4748
from lib.core.common import sanitizeStr
4849
from lib.core.common import setOptimize
50+
from lib.core.common import singleTimeWarnMessage
4951
from lib.core.common import UnicodeRawConfigParser
50-
from lib.core.convert import urldecode
51-
from lib.core.convert import urlencode
52+
from lib.core.common import urldecode
53+
from lib.core.common import urlencode
5254
from lib.core.data import conf
5355
from lib.core.data import kb
5456
from lib.core.data import logger
@@ -1970,6 +1972,7 @@ def __basicOptionValidation():
19701972
def __resolveCrossReferences():
19711973
lib.core.threads.readInput = readInput
19721974
lib.core.common.getPageTemplate = getPageTemplate
1975+
lib.core.convert.singleTimeWarnMessage = singleTimeWarnMessage
19731976

19741977
def init(inputOptions=AttribDict(), overrideOptions=False):
19751978
"""

lib/core/target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from lib.core.common import paramToDict
1919
from lib.core.common import readInput
2020
from lib.core.common import resetCookieJar
21-
from lib.core.convert import urldecode
21+
from lib.core.common import urldecode
2222
from lib.core.data import cmdLineOptions
2323
from lib.core.data import conf
2424
from lib.core.data import kb

lib/request/connect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
from lib.core.common import stdev
3333
from lib.core.common import urlEncodeCookieValues
3434
from lib.core.common import wasLastRequestDelayed
35-
from lib.core.convert import unicodeencode
36-
from lib.core.convert import urlencode
35+
from lib.core.common import unicodeencode
36+
from lib.core.common import urlencode
3737
from lib.core.data import conf
3838
from lib.core.data import kb
3939
from lib.core.data import logger

lib/utils/checkpayload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99

1010
from lib.core.common import readXmlFile
11-
from lib.core.convert import urldecode
11+
from lib.core.common import urldecode
1212
from lib.core.data import paths
1313
from lib.core.data import logger
1414

lib/utils/google.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
from lib.core.common import getUnicode
1616
from lib.core.common import readInput
17-
from lib.core.convert import urldecode
18-
from lib.core.convert import urlencode
17+
from lib.core.common import urldecode
18+
from lib.core.common import urlencode
1919
from lib.core.data import conf
2020
from lib.core.data import kb
2121
from lib.core.data import logger

thirdparty/ansistrm/ansistrm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import os
66
import re
77

8+
from lib.core.convert import stdoutencode
9+
810
class ColorizingStreamHandler(logging.StreamHandler):
911
# color names to indices
1012
color_map = {
@@ -45,7 +47,7 @@ def is_tty(self):
4547

4648
def emit(self, record):
4749
try:
48-
message = self.format(record)
50+
message = stdoutencode(self.format(record))
4951
stream = self.stream
5052

5153
if not self.is_tty:

0 commit comments

Comments
 (0)