检测受密码保护的PPT和XLS文档 [英] Detecting password protected PPT and XLS documents

查看:95
本文介绍了检测受密码保护的PPT和XLS文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个答案 https://stackoverflow.com/a/14336292/1537195 提供了一个很好的方法检测DOC和XLS文件的密码保护.

I found this answer https://stackoverflow.com/a/14336292/1537195 which gave a good way to detect password protection for DOC and XLS files.

//Flagged with password
if (bytes.Skip(0x20c).Take(1).ToArray()[0] == 0x2f) return true; //XLS 2003
if (bytes.Skip(0x214).Take(1).ToArray()[0] == 0x2f) return true; //XLS 2005
if (bytes.Skip(0x20B).Take(1).ToArray()[0] == 0x13) return true; //DOC 2005

但是,它似乎并不能覆盖所有XLS文件,我也在寻找一种以相同方式检测PPT文件的方法.反正知道这些文件类型要查看哪些字节?

However it does not seem to cover all XLS files and I am also looking for a way to detect PPT files in the same manner. Does anyway know which bytes to look at for these file types?

推荐答案

我将PowerPoint演示文稿另存为.ppt和.pptx,无论是否打开它们都需要输入密码,然后在7-Zip中打开它们,得出了初步结论那

I saved a PowerPoint presentation as .ppt and .pptx with and without a password required for opening them, opened them in 7-Zip and came to the tentative conclusion that

  • 没有密码的.pptx文件始终使用标准的.zip文件格式
  • .ppt文件是CompoundDocuments
  • .pptx文件和密码也为CompoundDocuments
  • 所有带有密码的CompoundDocuments都包含一个名为* Encrypt *
  • 的条目

要运行此代码,您需要安装NuGet软件包OpenMcdf.这是我可以找到的第一个C#库,用于阅读CompoundDocuments.

To get this code running, you need to installed the NuGet package OpenMcdf. This is the first C# library that I could find for reading CompoundDocuments.

using OpenMcdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace _22916194
{
    //http://stackoverflow.com/questions/22916194/detecing-password-protected-ppt-and-xls-documents
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var file in args.Where(File.Exists))
            {
                switch (Path.GetExtension(file))
                {
                    case ".ppt":
                    case ".pptx":
                        Console.WriteLine($"* {file} " +  (HasPassword(file) ? "is " : "isn't ") + "passworded");
                        Console.WriteLine();
                        break;

                    default:
                        Console.WriteLine($" * Unknown file type: {file}");
                        break;
                }
            }

            Console.ReadLine();

        }

        private static bool HasPassword(string file)
        {
            try
            {
                using (var compoundFile = new CompoundFile(file))
                {
                    var entryNames = new List<string>();
                    compoundFile.RootStorage.VisitEntries(e => entryNames.Add(e.Name), false);

                    //As far as I can see, only passworded files contain an entry with a name containing Encrypt
                    foreach (var entryName in entryNames)
                    {
                        if (entryName.Contains("Encrypt"))
                            return true;
                    }
                    compoundFile.Close();

                }
            }
            catch (CFFileFormatException) {
                //This is probably a .zip file (=unprotected .pptx)
                return false;
            }
            return false;
        }
    }
}

您应该能够扩展此代码以处理其他Office格式.顶部的结论应该成立,只是您需要在CompoundDocument中查找除包含* Encrypt *的文件名之外的其他数据(我快速浏览了.doc文件,但看起来工作原理并不完全相同) ).

You should be able to extend this code to handle other Office formats. The conclusions at the top should hold true, except that you need to look for some other data in the CompoundDocument than a filename containing *Encrypt* (I had a quick look at .doc files and it didn't seem to work exactly the same).

这篇关于检测受密码保护的PPT和XLS文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆