Modern PDF security

Command Line – PDF Security

© Lead Image © Bram Janssens, 123RF.com

© Lead Image © Bram Janssens, 123RF.com

Article from Issue 255/2022
Author(s):

PDFs, the preferred format for file sharing, only offer primitive privacy and security measures. With these command-line tools, you can help your PDFs meet modern security requirements.

First released in 1992, the Portable Document Format (PDF) shows no signs of disappearing. The format has become a business standard and the preferred format for sending files to a print shop. Closely related to PostScript (PS), PDF has the advantages of ensuring that files are seen exactly as the user intended and of eliminating concerns about the supported formats or available fonts on the recipient's word processor. Yet for all these advantages, the PDF format comes from an era less concerned with privacy and security. It does include privacy and security permissions, but these are primitive by modern standards. In fact, these tools are literally a joke. As I have heard several times, PDFs exist in one of two states: compromised and about to be compromised. Fortunately, the means exist to address issues that the PDF format itself does not. In addition to the countless scripts available for editing PDF content and structure, there are also a growing number that enhance privacy and security.

PDF password protection continues to be used for the simple reason that it is widely available. LibreOffice users, for example, can set it by selecting File | Export As | Export As PDF… | Security. From this path, a password can be set to open a PDF file, as well as grant permission for if and how the file can be printed, edited, or copied. These settings will control a PDF file's use by unsophisticated users, but they are no match for modern cracking tools. Aside from the laxness with which many users handle security, if the file is read in an environment in which the reader can control permissions (i.e., in most modern operating systems), the password protection is easily and quickly bypassed [1]. LibreOffice itself provides stronger protection with the option of using a digital signature or a personal GPG key by selecting File | Export As | Export As PDF… | PDF Options | Digital Signatures (Figure 1). While this option guarantees the sender's identity, it does not cover every circumstance.

Figure 1: LibreOffice can protect PDF files with a digital certificate or encryption key.

Depending on your purpose, you may want to use one of the scripts found in the repositories of Debian and other major distributions. Some of these scripts can carry out numerous functions, but here I will only detail their privacy and security functions. Note that several have no man page, instead offering only a brief help option, which should be enough to figure out their use.

pdfcrack

Whenever passwords are used, some users are bound to forget them. These users provide a legitimate reason for an administrative tool such as pdfcrack [2], although these tools are, of course, open to abuse. You can use pdfcrack in two ways: with a search string using the option --charset STRING (-c STRING) (Figure 2) or with --wordlist=FILE (-w=FILE). Using a wordlist – any number of which are available online – is generally the most efficient, unless you have some idea of what the password might be, and you can use regular expressions to set a search range. The search can be further limited by skipping passwords with fewer characters than a limit defined with --minpw NUMBER (-n NUMBER) or omitting any password length greater than the number defined with --maxpw=NUMBER (-m NUMBER). Yet another way to limit the attempt is to use --owner (-o). The initial letter case can also be ignored using --permutate (-s), an option that is presumably intended at some point to search for a permutation of a password (e.g., search and searching) simultaneously. However, that functionality remains to be implemented.

Figure 2: pdfcrack uses a search string to detect a password.

poppler-utils

The poppler-utils package [3] is a collection of several of the most common tools for PDF manipulation. When checking a file for privacy or security, you can use a number of these tools to list or extract various objects to be sure that you check them: pdfdetach for embedded files, pdfimages for images, pdftotext to read the contents easily, and pdfinfo for document extraction. However, for security, the most useful poppler-utils tool is pdfsig (Figure 3), which verifies digital signatures by searching for the NSS certificate database in the default Firefox profile, as well as searching /etc/pki/nssdb. In addition, pdfsig shows each signer's identity plus the time and date of the signature. Unfortunately, it is not set up to check GPG private and public keys.

Figure 3: Part of the poppler-utils, pdfsig detects digital signatures.

QPDF

Contrary to the impression you sometimes get from the Internet, QPDF [4] is not primarily intended to crack PDF files. More accurately, qpdf, a multipurpose command, converts one type of a PDF file into another. Among its functions are linearization (optimization for the web); creation of new files; rotating, merging, and splitting files; and selecting a range of pages to copy to a new file. Decryption and encryption are only part of QPDF's functions, offering enhanced permissions and higher security than PDF's default permissions. The --help option displays a help page similar to a man page.

QPDF can be compiled with several different options for encryption [5]. The command can be run using --password=PASSWORD. If a file has more than one password, the option must be used for each password, unless all are the same. Complete decryption is as easy as --decrypt, but encryption can be more complicated. The simplest way is to copy settings from another file with --copy-encryption=FILE, which creates a new file even if the original's password is not known. More often, though, --encrypt OPTIONS is used. An encrypted file can be created with:

qpdf --encrypt  KEY-LENGTH KEY-LENGTH-FLAGS FILE
qpdf --encrypt USER-PASSWORD OWNER-PASSWORD KEY-LENGTH-- ./INPUT.PDF ./OUTPUT.pdf

This command creates the output file with no output to the screen. KEY-LENGTH can be 40, 128, or 256, with each key length offering different flags before the input file, as described in the --help option, as well as extra security. For example, a key length of 40 allows the default permissions included in the PDF standard (ISO 32000). A key length of 128 includes all the options of a key length of 40, plus permissions for other alterations, such as permissions for forms or the use of AES encryption. To these permissions, a length of 256 adds support for V4 and the deprecated RC5 encryption. All these options and flags can be used with --password-mode, which sets how passwords should be read: literally, as UTF-8 encoded, hex-encoded, or automatically as needed. Any of these options can be used with QPDF's other options, to create, for example, an encrypted file with rotated pages or only a selection of pages.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Reconstruction

    Use QPDF to easily make structural changes to your PDFs, including reorganizing pages, creating watermarks, setting encryption options, and changing permissions.

  • LibreOffice Writer PDF Security

    Depending on your needs, LibreOffice Writer offers varying degrees of security for PDFs.

  • EncryptPad

    EncryptPad provides symmetric text encryption directly from the editor. You can also use EncryptPad to encrypt binary data.

  • NEWS

    In the news: LibreOffice 6.4 Released; Official Evernote Client Coming to Linux; Thanks to Linux, Google and Valve are Bringing Steam to Chromebooks; Nine-Year-Old Bug Found and Fixed in Sudo; and Systemd-homed Is Coming to a Linux Distribution Near You.

  • Clear-text Passwords In Web Sites
comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News