첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.00MB

▶ ProcessHelper.cs

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace TestProject
{
    /// <summary>
    /// 프로세스 헬퍼
    /// </summary>
    public class ProcessHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로세스 토큰 열기 - OpenProcessToken(ProcessHandle, desiredAccess, tokenHandle)

        /// <summary>
        /// 프로세스 토큰 열기
        /// </summary>
        /// <param name="ProcessHandle">프로세스 핸들</param>
        /// <param name="desiredAccess">희망 액세스</param>
        /// <param name="tokenHandle">토큰 핸들</param>
        /// <returns>처리 결과</returns>
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint desiredAccess, out IntPtr tokenHandle);

        #endregion
        #region 핸들 닫기 - CloseHandle(objectHandle)

        /// <summary>
        /// 핸들 닫기
        /// </summary>
        /// <param name="objectHandle">객체 핸들</param>
        /// <returns>처리 결과</returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr objectHandle);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// STANDARD_RIGHTS_REQUIRED
        /// </summary>
        private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;

        /// <summary>
        /// TOKEN_ASSIGN_PRIMARY
        /// </summary>
        private const int TOKEN_ASSIGN_PRIMARY = 0x1;

        /// <summary>
        /// TOKEN_DUPLICATE
        /// </summary>
        private const int TOKEN_DUPLICATE = 0x2;

        /// <summary>
        /// TOKEN_IMPERSONATE
        /// </summary>
        private const int TOKEN_IMPERSONATE = 0x4;

        /// <summary>
        /// TOKEN_QUERY
        /// </summary>
        private const int TOKEN_QUERY = 0x8;

        /// <summary>
        /// TOKEN_QUERY_SOURCE
        /// </summary>
        private const int TOKEN_QUERY_SOURCE = 0x10;

        /// <summary>
        /// TOKEN_ADJUST_GROUPS
        /// </summary>
        private const int TOKEN_ADJUST_GROUPS = 0x40;

        /// <summary>
        /// TOKEN_ADJUST_PRIVILEGES
        /// </summary>
        private const int TOKEN_ADJUST_PRIVILEGES = 0x20;

        /// <summary>
        /// TOKEN_ADJUST_SESSIONID
        /// </summary>
        private const int TOKEN_ADJUST_SESSIONID = 0x100;

        /// <summary>
        /// TOKEN_ADJUST_DEFAULT
        /// </summary>
        private const int TOKEN_ADJUST_DEFAULT = 0x80;

        /// <summary>
        /// TOKEN_ALL_ACCESS
        /// </summary>
        private const int TOKEN_ALL_ACCESS =
        (
            STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE         | TOKEN_IMPERSONATE   |
            TOKEN_QUERY              | TOKEN_QUERY_SOURCE   | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS |
            TOKEN_ADJUST_SESSIONID   | TOKEN_ADJUST_DEFAULT
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 관리자 권한 실행 여부 구하기 - IsRunningAsAdministrator(process)

        /// <summary>
        /// 관리자 권한 실행 여부 구하기
        /// </summary>
        /// <param name="process">프로세스</param>
        /// <returns>관리자 권한 실행 여부</returns>
        public static bool IsRunningAsAdministrator(Process process)
        {
            IntPtr tokenHandle = IntPtr.Zero;

            OpenProcessToken(process.Handle, TOKEN_ALL_ACCESS, out tokenHandle);

            WindowsIdentity windowsIdentity = new WindowsIdentity(tokenHandle);

            bool result = false;

            foreach(IdentityReference identityReference in windowsIdentity.Groups)
            {
                if(identityReference.IsValidTargetType(typeof(SecurityIdentifier)))
                {
                    SecurityIdentifier securityIdentifier = identityReference as SecurityIdentifier;

                    if(securityIdentifier.IsWellKnown(WellKnownSidType.AccountAdministratorSid) || securityIdentifier.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid))
                    {
                        result = true;

                        break;
                    }
                }
            }

            CloseHandle(tokenHandle);

            return result;
        }

        #endregion
    }
}

 

728x90

 

▶ Program.cs

using System;
using System.Diagnostics;
using System.Linq;

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

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Process process = Process.GetProcessesByName("notepad").FirstOrDefault();

            if(process != null)
            {
                bool result = ProcessHelper.IsRunningAsAdministrator(process);

                Console.WriteLine(result);
            }
        }

        #endregion
    }
}

※ 프로그램 실행시 관리자 권한으로 실행한다.

728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요