【UWP】修改清單脫離沙盒運行
眾所周知,UWP 是運行在沙盒里面的,所有權(quán)限都有嚴(yán)格限制,和沙盒外交互也需要特殊的通道,所以從根本杜絕了 UWP 毒瘤的存在。但是實際上 UWP 只是一個應(yīng)用模型,本身是沒有什么權(quán)限管理的,權(quán)限管理全靠 App Container 沙盒控制,如果我們脫離了這個沙盒,UWP 就會放飛自我了。那么有沒有這種可能呢?
我們打開設(shè)置應(yīng)用,通過任務(wù)管理器查看進程,就會發(fā)現(xiàn)它并沒有 Runtime Broker 存在,這個進程是用來在沙盒間代理的,這說明微軟給 UWP 開了一個后門。

那么我們是不是也有辦法脫離沙盒運行呢?Ahmed Walid 在 2023年2月 發(fā)表了這樣一個帖子:

同時他還提交了一個名為 Added a remark about uap10:TrustLevel 的 PR,在這個 PR 中明確提到了如何通過設(shè)置 Custom Capability 來修改 UWP 的 TrustLevel
Setting
uap10:TrustLevel="mediumIL"whileuap10:RuntimeBehavior="windowsApp"requires theMicrosoft.coreAppActivation_8wekyb3d8bbweCustom Capability.This is also true if
uap10:TrustLevel="mediumIL"andEntryPointis any other value than"windows.fullTrustApplication"or"windows.partialTrustApplication".You can read more about this custom capability here in Custom Capabilities.
如今這個 PR 已經(jīng)合并,現(xiàn)在可以直接在微軟文檔《應(yīng)用程序 (Windows 10)》中找到了
根據(jù)文檔描述,我們需要添加一個名為 Microsoft.coreAppActivation_8wekyb3d8bbwe 的自定義權(quán)限,然后將 uap10:TrustLevel 設(shè)置為 mediumIL 即可
首先我們在清單中加入權(quán)限
<?xml version="1.0" encoding="utf-8"?>
<Package
...
xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="... uap4 rescap">
...
<Capabilities>
...
<!-- runFullTrust 權(quán)限是必不可少的 -->
<rescap:Capability Name="runFullTrust" />
<uap4:CustomCapability Name="Microsoft.coreAppActivation_8wekyb3d8bbwe" />
</Capabilities>
</Package>
Custom Capability 不同于其他權(quán)限,這是用來給 OEM 自定義使用的,需要 SCCD 文件來證明你有使用權(quán)限的資格,所以想上架是基本沒可能了,相關(guān)內(nèi)容可以查看教程 [UWP] Custom Capability的使用
我們在項目根目錄新建一個名為 CustomCapability.SCCD 的文件,在其中寫入
<?xml version="1.0" encoding="utf-8"?>
<CustomCapabilityDescriptor xmlns="http://schemas.microsoft.com/appx/2018/sccd" xmlns:s="http://schemas.microsoft.com/appx/2018/sccd">
<CustomCapabilities>
<CustomCapability Name="Microsoft.coreAppActivation_8wekyb3d8bbwe"></CustomCapability>
</CustomCapabilities>
<AuthorizedEntities AllowAny="true"/>
<Catalog>FFFF</Catalog>
</CustomCapabilityDescriptor>
然后將該文件設(shè)置為內(nèi)容,或者選擇復(fù)制到輸出,只要最后能出現(xiàn)在安裝包里面就行了
最后我們將 uap10:TrustLevel 設(shè)置為 mediumIL
<?xml version="1.0" encoding="utf-8"?>
<Package
...
xmlns:uap10="http://schemas.microsoft.com/appx/manifest/uap/windows10/10"
IgnorableNamespaces="... uap10">
...
<Applications>
<Application
...
uap10:TrustLevel="mediumIL">
...
</Application>
</Applications>
...
</Package>
我們調(diào)用Process.GetProcesses()獲取進程列表(UAP 10.0.15138.0雖然加入了Process支持,但是并沒有實現(xiàn)Process.GetProcesses(),所以這里是運行在 .NET 8.0 上的
using Microsoft.UI.Xaml.Controls;
using System.Diagnostics;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace FullTrustUWP.Pages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage() => InitializeComponent();
public void Test()
{
// 這里使用了 Windows App SDK,實際上 WAS 是支持 UWP 的
Content = new ItemsView
{
// 必須使用 .NET Core App,因為微軟沒有給 .NET Core 5.0 實現(xiàn)這個方法
ItemsSource = Process.GetProcesses()
};
}
}
}
運行效果

如果沒有設(shè)置uap10:TrustLevel 為 mediumIL,則依然運行在沙盒中,Process.GetProcesses()只能獲取到沙盒中的進程

本文來自博客園,作者:where-where,轉(zhuǎn)載請注明原文鏈接:http://www.rzrgm.cn/wherewhere/p/18171253

浙公網(wǎng)安備 33010602011771號