Modern PDF security
Command Line – PDF Security

© Lead Image © Bram Janssens, 123RF.com
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.
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.
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.
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.