使用perl识别加密的Word文档

时间:2017-03-30 20:47:37

标签: perl encryption ms-word

我有一系列需要密码才能解码的Word文档,并且希望能够将它们识别为在一系列未加密的Word文档中使用密码。

我已遵循此stackoverflow链接提供的大部分指导:

How to detect if a Word document is password protected before uploading the file to server?

以下是我目前使用的代码:

 sub _is_encrypted_doc {
   my ($data) = @_;

 if (_is_office_doc($data)) {
     if ($data =~ /(?:<encryption xmlns)/i) {
       return 1;
     }
     if (index($data, "\x13") == 523) {
       return 1;
     }
     if (index($data, "\x2f") == 532) {
       return 1;
     }
     if (index($data, "\xfe") == 520) {
       return 1;
     }
     my $tdata = substr $data, 2000;
     $tdata =~ s/\\0/ /g;
     if (index($tdata, "E n c r y p t e d P a c k a g e") > -1) {
       return 1;
     }
   }
 }

但是,我的代码似乎并不能识别我们收到的所有变体。我在这里上传了一个这样的文件:

https://www.dropbox.com/s/uk51fixkc12aove/g9kfak164.docx?dl=0

使用&#34; NZlttUtz&#34;解码。我对此比较陌生,所以我希望有人可以在这里指导我。是否有更全面的代码可以执行此操作?

1 个答案:

答案 0 :(得分:1)

就我在你引用的链接中所看到的,通常以ÐÏ前缀开头的文件都受到保护,&amp;我没有找到任何以该前缀开头的未受保护的word文档。所以除非你需要区分例如Excel和Word以及文档的版本(通过文件ext可以更好地处理),您只需测试以Ð开头的文件。

use Fcntl qw(:seek);

my($fh, $FILE, $byte_position, $byte_value);

$FILE      = "path/to/file";
$byte_position = 0;

open($fh, "<", $FILE)
  || die "can't open $FILE: $!";

binmode($fh)
  || die "can't binmode $FILE";

sysseek($fh, $byte_position, SEEK_CUR)  # NB: 0-based
  || die "couldn't see to byte $byte_position in $FILE: $!";

sysread($fh, $byte_value, 1) == 1
  || die "couldn't read byte from $FILE: $!";

if (ord($byte_value) == 208){
  return 1;
}
else {
  return 0;
}

即。读取文件的第一个字节以查看它是否为D0hex(ASCIIÐ),如果为受保护文档返回1,否则返回0.