첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ 윈도우즈의 실행 파일 형식(PE32/PE32+)을 구하는 방법을 보여준다.

TestProject.zip
0.00MB

▶ Program.cs

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 이미지 아키텍처 구하기 - GetImageArchitecture(filePath)

        /// <summary>
        /// 이미지 아키텍처 구하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>이미지 아키텍처</returns>
        private static ushort GetImageArchitecture(string filePath)
        {
            using FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            using BinaryReader reader = new BinaryReader(stream);

            if(reader.ReadUInt16() != 23117)
            {
                throw new BadImageFormatException("Not a valid Portable Executable image", filePath);
            }

            stream.Seek(0x3a               , SeekOrigin.Current);
            stream.Seek(reader.ReadUInt32(), SeekOrigin.Begin  );

            if(reader.ReadUInt32() != 17744)
            {
                throw new BadImageFormatException("Not a valid Portable Executable image", filePath);
            }

            stream.Seek(20, System.IO.SeekOrigin.Current);

            return reader.ReadUInt16();
        }

        #endregion
        #region 이미지 아키텍처명 구하기 - GetImageArchitectureName(filePath)

        /// <summary>
        /// 이미지 아키텍처명 구하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>이미지 아키텍처명</returns>
        private static string GetImageArchitectureName(string filePath)
        {
            ushort imageArchitecture = GetImageArchitecture(filePath);

            if(imageArchitecture == 0x10b)
            {
                return "PE32";
            }
            else if(imageArchitecture == 0x20b)
            {
                return "PE32+";
            }
            else
            {
                return "(Unknown)";
            }
        }

        #endregion
        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            string[] filePathArray = new string[]
            {
                @"C:\Program Files\DAUM\PotPlayer\PotPlayer64.exe",
                @"C:\Program Files\Bandizip\Bandizip.exe",
                @"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe"
            };

            foreach(string filePath in filePathArray)
            {
                string imageArchitecutreName = GetImageArchitectureName(filePath);

                Console.WriteLine($"{filePath} : {imageArchitecutreName}");
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요