Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to check if pdf is password protected using ghostscript?

Tags:

ghostscript

is it possible to check if pdf is password protected using ghostscript? what would be the command? I know you can strip pdf password using ghostscript, but all I want to do is just checking if PDF is password protected or security enabled.

like image 793
Aman Avatar asked Oct 28 '10 04:10

Aman


2 Answers

checkuserpasswdPDF.sh:

#!/bin/sh

GS=~/gs/bin/gs
output=`$GS -dBATCH -sNODISPLAY "$1" 2>&1`
gsexit=$?

if [ "$gsexit" == "0" ]; then
  echo "Not user-password protected"
  exit 0;
else
  found=`echo "$output" |grep -o "This file requires a password"`
  if [ -z "$found" ]; then
    echo "Failed to invoke gs" 
    exit $gsexit
  else
    echo "Protected"
    exit 0;
  fi  
fi

Checks for user-password protected PDFs: checkuserpasswdPDF.sh test.pdf.

GS disregards owner-passwords (see this).

like image 151
khachik Avatar answered Dec 01 '22 20:12

khachik


With pdftk it is possible to detect a user password or owner password by just trying to do a dump_data operation.

 protected=0
 pdftk "input.pdf" dump_data output /dev/null dont_ask || protected=1

The problem here is that you don't know what the password prevents: reading, extracting data, modifying...?

like image 27
Benoit Avatar answered Dec 01 '22 20:12

Benoit