getmimetypes-by-magic-numbers
Service icon

GetMimeTypes by Magic Numbers

Stable version 1.1.0 (Compatible with OutSystems 11)
Uploaded
 on 23 Jul (3 weeks ago)
 by 
4.8
 (16 ratings)
getmimetypes-by-magic-numbers

GetMimeTypes by Magic Numbers

Compatible with:
Created on OutSystems 11

Version 1.1.0

Stable
Current
Uploaded on 23 Jul (3 weeks ago) by 
Compatible with:
Version 11
Database:
All
Release notes:

Added a new function to check whether office files (Excel, Powerpoint, and Word files) are password-encrypted or not. This will be used to validate if these files are being identified as OLE files:

        /// <summary>

        /// Action to check whether a file (Excel, Powerpoint, and Word files) are password-encrypted or not. This will be used to validate if these files are being identified as OLE files.

        /// </summary>

        /// <param name="ssContent">File to be checked</param>

        /// <param name="ssIsFilePasswordProtected"></param>

        /// <param name="ssErrorMessage"></param>


        public void MssCheckPasswordProtection(byte[] ssContent, out bool ssIsFilePasswordProtected, out string ssErrorMessage) {

            ssIsFilePasswordProtected = false;

            ssErrorMessage = "";


            try

            {

                // Check for OLE magic number

                if (ssContent.Length > 8 &&

                    ssContent[0] == 0xD0 &&

                    ssContent[1] == 0xCF &&

                    ssContent[2] == 0x11 &&

                    ssContent[3] == 0xE0 &&

                    ssContent[4] == 0xA1 &&

                    ssContent[5] == 0xB1 &&

                    ssContent[6] == 0x1A &&

                    ssContent[7] == 0xE1)

                {

                    using (MemoryStream ms = new MemoryStream(ssContent))

                    {

                        CompoundFile cf = new CompoundFile(ms);


                        // Excel

                        if (cf.RootStorage.TryGetStream("Workbook", out CFStream excelStream))

                        {

                            byte[] data = excelStream.GetData();

                            if (data.Length > 0x214)

                            {

                                // Password flag is at offset 0x214 in the stream

                                ushort flags = BitConverter.ToUInt16(data, 0x214);

                                if ((flags & 0x0001) != 0)

                                    ssIsFilePasswordProtected = true;

                            }

                        }


                        // Word

                        else if (cf.RootStorage.TryGetStream("WordDocument", out CFStream wordStream))

                        {

                            byte[] data = wordStream.GetData();

                            if (data.Length > 0x200)

                            {

                                // Protection flag at offset 0x0B (sometimes 0x22, varies slightly)

                                // Simplest reliable method: look for the FEncrypted bit at offset 0x0B in older docs

                                if ((data[0x0B] & 0x01) != 0)

                                    ssIsFilePasswordProtected = true;

                            }

                        }


                        // PowerPoint

                        else if (cf.RootStorage.TryGetStream("PowerPoint Document", out CFStream pptStream))

                        {

                            byte[] data = pptStream.GetData();

                            if (data.Length > 0x200)

                            {

                                // Not always reliable, but encrypted PowerPoint often has this header flag

                                if (data[0x0F] == 0xF0 || data[0x10] == 0x0F)

                                    ssIsFilePasswordProtected = true;

                            }

                        }


                        cf.Close();

                    }

                }

            }

            catch (Exception ex)

            {

                ssErrorMessage = $"Error: {ex.Message}";

            }

        } // MssCheckPasswordProtection


Application Objects:
GetMimeTypes by Magic Numbers has 0 AOs.

Version 1.0.2

Stable
icon_trusted
See documentation
Uploaded on 11 October 2019 by 
Compatible with:
Version 11
Database:
All
Release notes:

Added the .OAP for the Demo.

Application Objects:
GetMimeTypes by Magic Numbers has 0 AOs.

Version 1.0.1

Stable
Uploaded on 11 October 2019 by 
Compatible with:
Version 11
Database:
All
Release notes:

Added .OAP file in order to make everyone's life easier.

Application Objects:
GetMimeTypes by Magic Numbers has 0 AOs.