0

I was trying to gather information about password used to protect a PDF. I used PeePDF and xxd editor to view all the objects of password protected PDF.I came to know password info is stored in trailer part of PDF structure.When I run this command , I got

xxd my_encrypted.pdf | tail -n 4

00022c60: 5d0a 2f49 6e66 6f20 3220 3020 520a 2f45  ]./Info 2 0 R./E
00022c70: 6e63 7279 7074 2034 2030 2052 0a3e 3e0a  ncrypt 4 0 R.>>.
00022c80: 7374 6172 7478 7265 660a 3134 3139 3934  startxref.141994
00022c90: 0a25 2545 4f46 0a                        .%%EOF.

So, I understood that /Encrypt dictionary is in object 4. Now using PeePDF, I tried

PPDF> object 4

<< /O ��%�}�&��
v����o
      B��z���B�

/Filter /Standard
/Length 128
/V 2
/U ZM����S��3�
fmL
/R 3
/P -1 >>

/O is owner password

/U is user password of PDF

PPDF>  info 4

Offset: 699
Size: 206
MD5: 8a74ac53f9e6c1f4da44bcdbb65509e9
Object: dictionary
References: []

I got this info . I didn't even get the hash of password. What is that junk text represent ? Is it encrypted password ? What is MD5 hash represent ?

please tell me if there are any other tools that could analyse PDFs and get hash of password that is protecting PDF. Thank you

  • Passwords are padded with bytes defined in the PDF standard. Why is getting the hash of the password important? Why not use a PDF library to do this for you? – Ryan Sep 10 '19 at 05:19

1 Answers1

0

If nothing else helps, read the specification.

In the case at hand the specification to read is the PDF specification, i.e. ISO 32000-1 and ISO 32000-2.

A copy of ISO 32000-1 is published by Adobe at https://www.adobe.com/go/pdfreference/ (beware, the exact location has moved around a bit over the years) - it is complete and merely has the official ISO headers replaced by Adobe headers. Read section 7.6 Encryption therein.

In case of your PDF the encryption dictionary entry V with value 2 indicates the use of "Algorithm 1: Encryption of data using the RC4 or AES algorithms" in 7.6.2, "General Encryption Algorithm," but permitting encryption key lengths greater than 40 bits. The entry R with value 3 indicates the revision of the standard security handler. This algorithm/handler pair already is explained in ISO 32000-1.

In particular you can read there how the O and U values are calculated:

Algorithm 3: Computing the encryption dictionary’s O (owner password) value

a)Pad or truncate the owner password string as described in step (a) of "Algorithm 2: Computing an encryption key". If there is no owner password, use the user password instead.

b)Initialize the MD5 hash function and pass the result of step (a) as input to this function.

c)(Security handlers of revision 3 or greater) Do the following 50 times: Take the output from the previous MD5 hash and pass it as input into a new MD5 hash.

d)Create an RC4 encryption key using the first n bytes of the output from the final MD5 hash, where n shall always be 5 for security handlers of revision 2 but, for security handlers of revision 3 or greater, shall depend on the value of the encryption dictionary’s Length entry.

e)Pad or truncate the user password string as described in step (a) of "Algorithm 2: Computing an encryption key".

f)Encrypt the result of step (e), using an RC4 encryption function with the encryption key obtained in step (d).

g)(Security handlers of revision 3 or greater) Do the following 19 times: Take the output from the previous invocation of the RC4 function and pass it as input to a new invocation of the function; use an encryption key generated by taking each byte of the encryption key obtained in step (d) and performing an XOR (exclusive or) operation between that byte and the single-byte value of the iteration counter (from 1 to 19).

h)Store the output from the final invocation of the RC4 function as the value of the O entry in the encryption dictionary.

and

Algorithm 5: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 3 or greater)

a)Create an encryption key based on the user password string, as described in "Algorithm 2: Computing an encryption key".

b)Initialize the MD5 hash function and pass the 32-byte padding string shown in step (a) of "Algorithm 2: Computing an encryption key" as input to this function.

c)Pass the first element of the file’s file identifier array (the value of the ID entry in the document’s trailer dictionary; see Table 15) to the hash function and finish the hash.

d)Encrypt the 16-byte result of the hash, using an RC4 encryption function with the encryption key from step (a).

e)Do the following 19 times: Take the output from the previous invocation of the RC4 function and pass it as input to a new invocation of the function; use an encryption key generated by taking each byte of the original encryption key obtained in step (a) and performing an XOR (exclusive or) operation between that byte and the single-byte value of the iteration counter (from 1 to 19).

f)Append 16 bytes of arbitrary padding to the output from the final invocation of the RC4 function and store the 32-byte result as the value of the U entry in the encryption dictionary.

In both cases step (a) of "Algorithm 2: Computing an encryption key" is referenced:

Algorithm 2: Computing an encryption key

a)Pad or truncate the password string to exactly 32 bytes. If the password string is more than 32 bytes long, use only its first 32 bytes; if it is less than 32 bytes long, pad it by appending the required number of additional bytes from the beginning of the following padding string:

< 28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08
2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A >

That is, if the password string is n bytes long, append the first 32 - n bytes of the padding string to the end of the password string. If the password string is empty (zero-length), meaning there is no user password, substitute the entire padding string in its place.

...

mkl
  • 90,588
  • 15
  • 125
  • 265
  • 1
    @MartinThoma *"The link is dead"* - I updated it. Considering its appearance now one can hope it remains valid for some time to come. – mkl Jun 12 '22 at 12:35