Skip to content

Commit

Permalink
try to detect encoding when loading file
Browse files Browse the repository at this point in the history
  • Loading branch information
xmendez committed Nov 9, 2018
1 parent 91fae58 commit e68ca23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
'pyparsing>=2.2.0',
'future>=0.16.0',
'six>=1.10.0',
'configparser>=3.5.0'
'configparser>=3.5.0',
'chardet',
]

setup(
Expand Down
3 changes: 2 additions & 1 deletion src/wfuzz/plugins/payloads/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from wfuzz.externals.moduleman.plugin import moduleman_plugin
from wfuzz.exception import FuzzExceptBadFile
from wfuzz.plugin_api.base import BasePayload
from wfuzz.utils import open_file_detect_encoding


@moduleman_plugin
Expand All @@ -25,7 +26,7 @@ def __init__(self, params):
BasePayload.__init__(self, params)

try:
self.f = open(self.find_file(self.params["fn"]), "r")
self.f = open_file_detect_encoding(self.find_file(self.params["fn"]))
except IOError as e:
raise FuzzExceptBadFile("Error opening file. %s" % str(e))

Expand Down
22 changes: 22 additions & 0 deletions src/wfuzz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import six
from chardet.universaldetector import UniversalDetector


def json_minify(string, strip_space=True):
Expand Down Expand Up @@ -130,3 +131,24 @@ def convert_to_unicode(text):
return text.encode("utf-8", errors='ignore')
else:
return text


def open_file_detect_encoding(file_path):
def detect_encoding(file_path):
detector = UniversalDetector()
detector.reset()

with open(file_path, mode='rb') as file_to_detect:
for line in file_to_detect:
detector.feed(line)
if detector.done:
break
detector.close()

print(detector.result)
return detector.result

if sys.version_info >= (3, 0):
return open(file_path, "r", encoding=detect_encoding(file_path).get('encoding', 'utf-8'))
else:
return open(file_path, "r")

0 comments on commit e68ca23

Please sign in to comment.