From d27bd1056a5c78f1e6e04d1bb18c125e51587378 Mon Sep 17 00:00:00 2001 From: ThoLe Date: Sun, 21 Oct 2018 14:36:29 +0200 Subject: [PATCH 1/4] Improve IsJavaScript() to avoid false detection of Java Script --- JSAnalysis.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/JSAnalysis.py b/JSAnalysis.py index 79fce91..dec4e3b 100644 --- a/JSAnalysis.py +++ b/JSAnalysis.py @@ -176,14 +176,13 @@ def isJavascript(content): @param content: A string @return: A boolean, True if it seems to contain Javascript code or False in the other case ''' - jsStrings = ['var ', ';', ')', '(', 'function ', '=', '{', '}', 'if ', 'else', 'return', 'while ', 'for ', + jsStrings = ['var ', ';', ')', '(', 'function ', '=', '{', '}', 'if(', 'if (', 'else{', 'else {','else if', 'return', 'while(', 'while (', 'for(', 'for (', ',', 'eval'] keyStrings = [';', '(', ')'] - reVarInit = 'var [\w0-9]+\s*?=' - reFunctionCall = '[\w0-9]+\s*?\(.*?\)\s*?;' stringsFound = [] limit = 15 - minDistinctStringsFound = 4 + #JS should at least contain ';', ')', '(', 'var', '=' + minDistinctStringsFound = 5 minRatio = 10 results = 0 length = len(content) @@ -279,4 +278,13 @@ def unescape(escapedBytes, unicode = True): unescapedBytes = escapedBytes except: return (-1, 'Error while unescaping the bytes') - return (0, unescapedBytes) \ No newline at end of file + return (0, unescapedBytes) + + +if __name__=='__main__': + s1="""2 J + 0.57 w + BT /F1 12.00 Tf ET + BT 31.19 802.04 Td (Fixedly i there up the rinieri . Portals close texture of acheron winds wherewith. Meets the bounds such gone from death from troy when. Addressed me sufficient, ere thou. Inhabitants with woe? then . Heartstruck i soon that blasphemies gainst each here? he himself, and stands. Seems, heeds not ever after many reproof before me scatterd leaves. Sweet, that forces out that part in headlong fell spirits, who thundring. Haply thoughtst me uninterrupted traverses the rough. Arguments, of showrs ceaseless, accursed, heavy, and which. Extremity arriving of authority permitted . Denying and note of themselves the loins so wake. Fate suffer not but while. Main-sail rent throughout; and showing us of justice lays chastising hand . Horn? your skirmishing secure, my judgment to west, a . Courseth down to great city i come note. At sight and what dost hope there. Grapple that, us? to shake off a sinners pine phlegyas. Arno, that trophy crownd penelope with earth puts on droppd the hideous. Hither: once pondring now passd on not beware. Spirit movd ever hath passd, still walld in pen wrote o . Comes one builds anew, another stops the lists of tyrants, who field. Dream, and court of himself distance from hoarse with words, whose eyes. Aghast the caldron down its sweeter hymns became that triumph. Knowest shouts his device, mischievous . Age were separate from renownd, learnd to nessus yet to assaild . Guard the height all wants, and though unweeting that. Encrusting hung, and be weary of sublimest song, whose sake. Prato, not mound, reard once with thee . Caitiff? on rank; democritus, who fears not leave thee. Frenchmans gold and bate their unkindly visage saves from. Taught, betwixt the rocks low melancholy sounds, made from clothd me wholly. Retrace the tyrannous gust those fewer who clear! he, rout oerthrew. Forespent, we unopposd there no little it shall strange, through its departure. Naso; lucan in neighbours are two shorter feet. Saracens or layman known . Dropsy, disproportioning the ended . Mutual war there cocytus, of host. Covertly, but fraud, because they at some compensation find. Device, mischievous as a felon deeds shew thee. Presageful, thou he brings thee stay and styx, and baptists form distorted. What, and now wax, each extremity arriving . Vexd with such now bethink. Tempers his train divided to briefly ye spirits: arrivd below were their. Back the foots sinew griping. Zeal have quelld the almighty foe to best, the drenchd in scum. Sand, its language turnd back his filth disguisd sacristy. Gurgle in tears droppd to human. Surface, scarcely firm embattled spears, and cahors. Arno, that serchios wave yet thou. Stripe! none are turnd . Rest? he hunt approach or else so therefore: but follow me . Shades, on bethought him draghinazzo haste. Next, whose heads are last a scurf from. Jason is castles were we, a thick beset. Wound, the satan! loud moans resounded through these florentines . Strove, so foul? one, impatient to descend into nearer. Drops hither, ere a soul . Seem fire-illumd are one belly . Sevral moons had but back; for plastic hand might . Along; thus fourth time of heavn dawnd through latter . Litanies on our eyes from. Cumber earth who sped lucia, of intellect sound and boding . Stain imbrud; if he, whom soon friend the plumes, that most unholy. Inventions multiform, our seeking him be pleasd. Whole must below were only. Him: capaneus! thou must needs . Toils releasd all bar his labours with silent. Ford may see or of pity most doth. Thyself if sings my dismay renews, in sounded aloud their pangs hardily. First behoves make the ten let similar, so obsequious. Shoulder joind the nails serve . Bit, and back; for water-fowl when. Visages of and, if virtue to bred and thickets close. Pack, who under close in tears ever close . Catch its mould, did space of ample. Waited nor movd to our sad reversd, and muttring thunder feeble grasping. Leprous spirit in whom hector . Rebukd him bespake: pause, and feet were. Envious, proud scorn, but she in corruptible flesh, among languishes . Acre one have their ear lopt off, who dwell not . Concealed, thus reachd; but mincius, till . Breath of brake to all . Exhald its base, as hangs the stoutest. Towerd at journey bent; for this. Gnats, or foam upon marvel where chin, the frogs, that doth. Cares not removd farthest from. Curb the power to wail, borsiere, yonder walking with shadows. Alone; so words; at judgment. Sand: look to devil black, and empedocles. Fruit i thread and names that mounted on earth; and sustain. Fable, where will instruct me, soon both what this. Muddy lees; nor spread wings and tegghiaio say, did seat . Uprearing, horrible, whom i not rack thy wishes it seems to kennd. Air, that ere vain . Mass had sustaind, whereat i prime, and tournaments, and matted thick. Justice, dooms it to many stain-- to her plastic hand. You one, impatient eagerness of way broke. Wherein thy crimes?--him death oertook him thou fresh and their. Pains rack thy eloquent persuasive tongue. Poet gaind, when him . Wrench her walls with bloody foam upon strode. Change th together short course. Sith he made mangled, then waggd the point. Lucia calling, her way aloft he said. Quitting us, if on eternal then had exempted, he whose canopy reposing. ) Tj ET""" + + isJavascript(s1) \ No newline at end of file From 2a1c9fcb575e46d2b80929988bd85ec119e90ecf Mon Sep 17 00:00:00 2001 From: ThoLe Date: Sun, 21 Oct 2018 14:44:26 +0200 Subject: [PATCH 2/4] Improve IsJavaScript() to avoid false detection of Java Script and clean up unused variables --- JSAnalysis.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/JSAnalysis.py b/JSAnalysis.py index dec4e3b..ddaf7e6 100644 --- a/JSAnalysis.py +++ b/JSAnalysis.py @@ -279,12 +279,3 @@ def unescape(escapedBytes, unicode = True): except: return (-1, 'Error while unescaping the bytes') return (0, unescapedBytes) - - -if __name__=='__main__': - s1="""2 J - 0.57 w - BT /F1 12.00 Tf ET - BT 31.19 802.04 Td (Fixedly i there up the rinieri . Portals close texture of acheron winds wherewith. Meets the bounds such gone from death from troy when. Addressed me sufficient, ere thou. Inhabitants with woe? then . Heartstruck i soon that blasphemies gainst each here? he himself, and stands. Seems, heeds not ever after many reproof before me scatterd leaves. Sweet, that forces out that part in headlong fell spirits, who thundring. Haply thoughtst me uninterrupted traverses the rough. Arguments, of showrs ceaseless, accursed, heavy, and which. Extremity arriving of authority permitted . Denying and note of themselves the loins so wake. Fate suffer not but while. Main-sail rent throughout; and showing us of justice lays chastising hand . Horn? your skirmishing secure, my judgment to west, a . Courseth down to great city i come note. At sight and what dost hope there. Grapple that, us? to shake off a sinners pine phlegyas. Arno, that trophy crownd penelope with earth puts on droppd the hideous. Hither: once pondring now passd on not beware. Spirit movd ever hath passd, still walld in pen wrote o . Comes one builds anew, another stops the lists of tyrants, who field. Dream, and court of himself distance from hoarse with words, whose eyes. Aghast the caldron down its sweeter hymns became that triumph. Knowest shouts his device, mischievous . Age were separate from renownd, learnd to nessus yet to assaild . Guard the height all wants, and though unweeting that. Encrusting hung, and be weary of sublimest song, whose sake. Prato, not mound, reard once with thee . Caitiff? on rank; democritus, who fears not leave thee. Frenchmans gold and bate their unkindly visage saves from. Taught, betwixt the rocks low melancholy sounds, made from clothd me wholly. Retrace the tyrannous gust those fewer who clear! he, rout oerthrew. Forespent, we unopposd there no little it shall strange, through its departure. Naso; lucan in neighbours are two shorter feet. Saracens or layman known . Dropsy, disproportioning the ended . Mutual war there cocytus, of host. Covertly, but fraud, because they at some compensation find. Device, mischievous as a felon deeds shew thee. Presageful, thou he brings thee stay and styx, and baptists form distorted. What, and now wax, each extremity arriving . Vexd with such now bethink. Tempers his train divided to briefly ye spirits: arrivd below were their. Back the foots sinew griping. Zeal have quelld the almighty foe to best, the drenchd in scum. Sand, its language turnd back his filth disguisd sacristy. Gurgle in tears droppd to human. Surface, scarcely firm embattled spears, and cahors. Arno, that serchios wave yet thou. Stripe! none are turnd . Rest? he hunt approach or else so therefore: but follow me . Shades, on bethought him draghinazzo haste. Next, whose heads are last a scurf from. Jason is castles were we, a thick beset. Wound, the satan! loud moans resounded through these florentines . Strove, so foul? one, impatient to descend into nearer. Drops hither, ere a soul . Seem fire-illumd are one belly . Sevral moons had but back; for plastic hand might . Along; thus fourth time of heavn dawnd through latter . Litanies on our eyes from. Cumber earth who sped lucia, of intellect sound and boding . Stain imbrud; if he, whom soon friend the plumes, that most unholy. Inventions multiform, our seeking him be pleasd. Whole must below were only. Him: capaneus! thou must needs . Toils releasd all bar his labours with silent. Ford may see or of pity most doth. Thyself if sings my dismay renews, in sounded aloud their pangs hardily. First behoves make the ten let similar, so obsequious. Shoulder joind the nails serve . Bit, and back; for water-fowl when. Visages of and, if virtue to bred and thickets close. Pack, who under close in tears ever close . Catch its mould, did space of ample. Waited nor movd to our sad reversd, and muttring thunder feeble grasping. Leprous spirit in whom hector . Rebukd him bespake: pause, and feet were. Envious, proud scorn, but she in corruptible flesh, among languishes . Acre one have their ear lopt off, who dwell not . Concealed, thus reachd; but mincius, till . Breath of brake to all . Exhald its base, as hangs the stoutest. Towerd at journey bent; for this. Gnats, or foam upon marvel where chin, the frogs, that doth. Cares not removd farthest from. Curb the power to wail, borsiere, yonder walking with shadows. Alone; so words; at judgment. Sand: look to devil black, and empedocles. Fruit i thread and names that mounted on earth; and sustain. Fable, where will instruct me, soon both what this. Muddy lees; nor spread wings and tegghiaio say, did seat . Uprearing, horrible, whom i not rack thy wishes it seems to kennd. Air, that ere vain . Mass had sustaind, whereat i prime, and tournaments, and matted thick. Justice, dooms it to many stain-- to her plastic hand. You one, impatient eagerness of way broke. Wherein thy crimes?--him death oertook him thou fresh and their. Pains rack thy eloquent persuasive tongue. Poet gaind, when him . Wrench her walls with bloody foam upon strode. Change th together short course. Sith he made mangled, then waggd the point. Lucia calling, her way aloft he said. Quitting us, if on eternal then had exempted, he whose canopy reposing. ) Tj ET""" - - isJavascript(s1) \ No newline at end of file From 3991c608d71905c461d30d621153e43ec240143a Mon Sep 17 00:00:00 2001 From: tholep Date: Mon, 22 Oct 2018 21:31:47 +0200 Subject: [PATCH 3/4] Display URIs in Output --- PDFCore.py | 4 ++++ peepdf.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/PDFCore.py b/PDFCore.py index 3b2fe00..b0267cf 100644 --- a/PDFCore.py +++ b/PDFCore.py @@ -6210,6 +6210,7 @@ def getStats (self): containingURIs = self.body[version].getContainingURIs() if len(containingURIs) > 0: statsVersion['URIs'] = [str(len(containingURIs)), containingURIs] + statsVersion['URIDisplay'] = set(self.getURIs(version=version)[0]) #only get unique URIs else: statsVersion['URIs'] = None containingJS = self.body[version].getContainingJS() @@ -6840,6 +6841,7 @@ def parse (self, fileName, forceMode = False, looseMode = False, manualAnalysis @param fileName The name of the file to be parsed @param forceMode Boolean to specify if ignore errors or not. Default value: False. @param looseMode Boolean to set the loose mode when parsing objects. Default value: False. + @param manualAnalysis Boolean to specify whether JS analysis is performed. Default value: False. @return A PDFFile instance ''' global isForceMode, pdfFile, isManualAnalysis @@ -6928,6 +6930,7 @@ def parse (self, fileName, forceMode = False, looseMode = False, manualAnalysis # Getting the number of updates in the file while fileContent.find('%%EOF') != -1: + self.readUntilSymbol(fileContent, '%%EOF') self.readUntilEndOfLine(fileContent) self.fileParts.append(fileContent[:self.charCounter]) @@ -7014,6 +7017,7 @@ def parse (self, fileName, forceMode = False, looseMode = False, manualAnalysis else: auxContent = auxContent[index+len(objectHeader):] relativeOffset += len(objectHeader) + #find object in rawObject ret = self.createPDFIndirectObject(rawObject, forceMode, looseMode) if ret[0] != -1: pdfIndirectObject = ret[1] diff --git a/peepdf.py b/peepdf.py index 7c162db..1dabd7f 100755 --- a/peepdf.py +++ b/peepdf.py @@ -664,6 +664,11 @@ def getPeepJSON(statsDict, version, revision): if statsVersion['URIs'] is not None: stats += newLine + beforeStaticLabel + '\tObjects with URIs (' + \ statsVersion['URIs'][0] + '): ' + resetColor + str(statsVersion['URIs'][1]) + stats += newLine + beforeStaticLabel + '\tFound URIs : ' + resetColor + for display in statsVersion['URIDisplay']: + display=str(display) + if "http" in display.lower(): + stats += newLine + beforeStaticLabel + '\t\t' + resetColor + display if COLORIZED_OUTPUT and not options.avoidColors: beforeStaticLabel = warningColor if statsVersion['Objects with JS code'] != None: From bdba53c64f4d66ecdb37ec0b81f65d57cf5c2cd3 Mon Sep 17 00:00:00 2001 From: tholep Date: Sat, 27 Oct 2018 10:28:00 +0200 Subject: [PATCH 4/4] Add few comments to understand codes --- PDFCore.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/PDFCore.py b/PDFCore.py index b0267cf..11a2d35 100644 --- a/PDFCore.py +++ b/PDFCore.py @@ -4448,7 +4448,6 @@ def updateStats(self, id, pdfObject, delete=False): return (0,'') - class PDFTrailer : def __init__(self, dict, lastCrossRefSection = '0', streamPresent = False): self.errors = [] @@ -6998,12 +6997,15 @@ def parse (self, fileName, forceMode = False, looseMode = False, manualAnalysis # Converting the body content in PDFObjects body = PDFBody() + # search for objects e.g. 10 0 obj rawIndirectObjects = self.getIndirectObjects(bodyContent, looseMode) if rawIndirectObjects != []: for j in range(len(rawIndirectObjects)): relativeOffset = 0 auxContent = str(bodyContent) + #raw content of object rawObject = rawIndirectObjects[j][0] + #object header e.g. 10 0 obj objectHeader = rawIndirectObjects[j][1] while True: index = auxContent.find(objectHeader) @@ -7349,6 +7351,7 @@ def createPDFStream(self, dict, stream): elements = {} rawNames = {} ret = self.readObject(dict[self.charCounter:], 'name') + if ret[0] == -1: if ret[1] != 'Empty content reading object': if isForceMode: @@ -7360,6 +7363,7 @@ def createPDFStream(self, dict, stream): name = None else: name = ret[1] + while name != None: key = name.getValue() rawNames[key] = name @@ -7388,6 +7392,7 @@ def createPDFStream(self, dict, stream): name = None else: name = ret[1] + if elements.has_key('/Type') and elements['/Type'].getValue() == '/ObjStm': try: pdfStream = PDFObjectStream(dict, stream, elements, rawNames, {}) @@ -7404,6 +7409,7 @@ def createPDFStream(self, dict, stream): if e.message != '': errorMessage += ': '+e.message return (-1, errorMessage) + self.charCounter = realCounter return (0,pdfStream)