Skip to content

Commit b47c1ba

Browse files
committed
fix: bug fixes in base64 processing
Former-commit-id: 195c592 Former-commit-id: 7b95ee8
1 parent 98730f3 commit b47c1ba

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

yarGen.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def filter_string_set(string_set):
756756
# Base64
757757
if re.search(r'^(?:[A-Za-z0-9+/]{4}){30,}(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$', string) and \
758758
re.search(r'[A-Za-z]', string) and re.search(r'[0-9]', string):
759-
localStringScores[string] += 6
759+
localStringScores[string] += 7
760760
# Base64 Executables
761761
if re.search(r'(TVqQAAMAAAAEAAAA//8AALgAAAA|TVpQAAIAAAAEAA8A//8AALgAAAA|TVqAAAEAAAAEABAAAAAAAAAAAAA|'
762762
r'TVoAAAAAAAAAAAAAAAAAAAAAAAA|TVpTAQEAAAAEAAAA//8AALgAAAA)', string):
@@ -888,10 +888,12 @@ def filter_string_set(string_set):
888888
# Base64
889889
if args.trace:
890890
print("Starting Base64 string analysis ...")
891-
for m_string in (string, string[1:], string[1:] + "=", string + "=", string + "=="):
891+
for m_string in (string, string[1:], string[:-1], string[1:] + "=", string + "=", string + "=="):
892892
if is_base_64(m_string):
893-
decoded_string = base64.b64decode(m_string)
894-
# print decoded_string
893+
try:
894+
decoded_string = base64.b64decode(m_string, validate=False)
895+
except binascii.Error as e:
896+
continue
895897
if is_ascii_string(decoded_string, padding_allowed=True):
896898
# print "match"
897899
localStringScores[string] += 10
@@ -1572,9 +1574,9 @@ def get_rule_strings(string_elements, opcode_elements):
15721574
string = string[8:]
15731575
enc = " wide"
15741576
if string in base64strings:
1575-
base64comment = " /* base64 encoded string '%s' */" % base64strings[string]
1577+
base64comment = " /* base64 encoded string '%s' */" % base64strings[string].decode()
15761578
if string in hexEncStrings:
1577-
hexEncComment = " /* hex encoded string '%s' */" % removeNonAsciiDrop(hexEncStrings[string])
1579+
hexEncComment = " /* hex encoded string '%s' */" % removeNonAsciiDrop(hexEncStrings[string]).decode()
15781580
if string in pestudioMarker and args.score:
15791581
pestudio_comment = " /* PEStudio Blacklist: %s */" % pestudioMarker[string]
15801582
if string in reversedStrings:
@@ -1765,7 +1767,6 @@ def get_file_range(size):
17651767
max_size = int(round(max_size, -3))
17661768
elif len(str(max_size)) >= 5:
17671769
max_size = int(round(max_size, -3))
1768-
print(max_size)
17691770
size_string = "filesize < {0}KB".format(max_size)
17701771
if args.debug:
17711772
print("File Size Eval: SampleSize (b): {0} SizeWithMultiplier (b/Kb): {1} / {2} RoundedSize: {3}".format(
@@ -1818,6 +1819,7 @@ def is_hex_encoded(s, check_length=True):
18181819
return False
18191820

18201821

1822+
# TODO: Still buggy after port to Python3
18211823
def extract_hex_strings(s):
18221824
strings = []
18231825
hex_strings = re.findall(b"([a-fA-F0-9]{10,})", s)
@@ -1836,16 +1838,14 @@ def extract_hex_strings(s):
18361838
try:
18371839
if len(string) % 2 != 0 or len(string) < 8:
18381840
continue
1841+
# Skip
1842+
if b'0000' in string:
1843+
continue
18391844
dec = string.replace(b'00', b'')
1840-
#print("Testing: %s" % string)
1841-
#print("Decoded: %s" % dec)
18421845
if is_ascii_string(dec, padding_allowed=False):
1843-
#print("CAN USE >>>>>>>>>>>>>>>>>>>>>>>> %s" % string)
18441846
strings.append(string)
18451847
except Exception as e:
18461848
traceback.print_exc()
1847-
#print len(hex_strings)
1848-
#sys.exit(0)
18491849
return strings
18501850

18511851

0 commit comments

Comments
 (0)