Skip to content

Commit

Permalink
[Minor] Pdf: Do not fire PDF_SUSPICIOUS on legit escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Jan 11, 2021
1 parent 1a30d7d commit 1108444
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions lualib/lua_content/pdf.lua
Expand Up @@ -38,7 +38,7 @@ local pdf_patterns = {
patterns = {
[[netsh\s]],
[[echo\s]],
[[\/[A-Za-z]*#\d\d(?:[#A-Za-z<>/\s])]], -- Hex encode obfuscation
[=[\/[A-Za-z]*#\d\d[#A-Za-z<>/\s]]=], -- Hex encode obfuscation
}
},
start_object = {
Expand Down Expand Up @@ -1326,16 +1326,33 @@ processors.suspicious = function(input, task, positions, pdf_object, pdf_output)
suspicious_factor = suspicious_factor + 0.5
elseif match[2] == 2 then
nexec = nexec + 1
else
nencoded = nencoded + 1
elseif match[2] == 3 then
local enc_data = input:sub(match[1] - 2, match[1] - 1)
local legal_escape = false

if enc_data then
enc_data = enc_data:strtoul()

if last_encoded then
if match[1] - last_encoded < 8 then
-- likely consecutive encoded chars, increase factor
close_encoded = close_encoded + 1
if enc_data then
-- Legit encode cases are non printable characters (e.g. spaces)
if enc_data < 0x21 or enc_data >= 0x7f then
legal_escape = true
end
end
end
last_encoded = match[1]

if not legal_escape then
nencoded = nencoded + 1

if last_encoded then
if match[1] - last_encoded < 8 then
-- likely consecutive encoded chars, increase factor
close_encoded = close_encoded + 1
end
end
last_encoded = match[1]

end
end
end

Expand Down

0 comments on commit 1108444

Please sign in to comment.