9月 21, 2016

指定應用程式的執行權限

        Windows系統從Vista之後,安全規範便有些改變與XP不盡相同,對使用者來說最直觀的差異便在於有時需要以系統管理員身分執行程序才能順便完成一些操作,在此紀錄下如何強制以系統管理員身分執行。


因個人基於LabVIEW開發程序,先提供兩個技術網頁供參考


Deploying LabVIEW Applications on Windows Vista Operating


其中第一個技術網頁主要說明了LabVIEW開發之應用程序該如何製作 *.manifest  檔案,簡單來說這是編輯應用程序權限的說明檔案,可以用TXT文本將code紀錄後修改附檔名即可製作完成,其中需要輸入的是 "檔案名稱" 與 "執行權限",文末另外附上範例檔案供參考。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="6.1.4.1"
     processorArchitecture="X86"
     name="檔案名稱"
     type="win32"/>
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>

另外要注意檔案名稱不能有空格或特殊字串符號,按編者的經驗修改完執行權限後的程序仍可以修改名稱,而檔案權限的編輯主要有三個層級如下
  • asInvoker (Default) The stand-alone application executes with the same access privileges as the user, either standard or administrator. The application does not display a dialog box prompting the user for consent before it runs. If the application runs as standard user, the application cannot write to protected directories. If the application runs as administrator, the application can write to protected directories.
  • highestAvailable The stand-alone application executes with the highest access privileges possible. If the user has only standard user privileges, the application does not display a dialog box prompting the user for consent, but the application cannot write to protected directories. If the user has administrative privileges, the application displays a dialog box prompting the user to either consent or to enter the administrator password and can write to protected directories.
  • requireAdministrator The stand-alone application executes at the administrator level. If the user has only standard privileges, the application displays a dialog box prompting the user to enter the administrator password and can write to protected directories. If the user has administrator privileges, the application displays a dialog box prompting the user for consent before it runs. The application can write to protected directories.
一般預設使用 "asInvoker",也建議使用此設定即可,因程序會依據使用者本身的權限來運行,若有需要提高權限可在運行前另外以滑鼠右擊選擇系統管理員權限運行

若已知程序一定會涉及保護路徑的檔案讀寫或其他需要權限的功能,可以改為"highestAvailable",當使用者有權限時會顯示提醒視窗,若使用者無權限則等同預設層級

最後是"requireAdministrator"層級,若使用者並不具備較高權限,但已知管理員登入密碼可以使用此權限,應可用於資料庫、管理系統等程序開發

接著是實際操作可參考第二個技術網頁,在同時請同步準備好 mt.exe,這個程序來自Microsoft Software Development Kit可以在Microsoft Web site上找到,並將mt.exe、*.manifest、*.exe三個檔案(e.g. Queue_Make.exe、Queue_Make.manifest)放在同一個路徑資料夾 (e.g. C:\test)

操作步驟如下
  1. 開啟windows cmd(命令提示字元)
  2. 將提示路徑移到目標路徑下如上述舉例 C:\test
  3. 輸入命令做manifest檔案檢查
    mt.exe -manifest Application Name.manifest -validate_manifest(e.g. mt.exe -manifest Queue_Make.manifest -validate_manifest)
  4. 輸入命令做檔案權限變更
    mt.exe -manifest Application Name.manifest -outputresource:directory path\Application Name.exe;#1
    (e.g. mt.exe -manifest Queue_make.manifest -outputresource:Queue_make.exe;#1)

    這邊因為已經將cmd路徑移至目標路徑所以省略了 directory path
  5. 稍等一下若沒有顯示錯誤信息代表成功可以看一下執行程序已帶有盾牌(管理者權限)

File List






沒有留言:

張貼留言