11using System ;
2+ using System . Diagnostics ;
23using System . IO ;
4+ using System . Linq ;
5+ using System . Text . Json ;
6+ using System . Threading ;
37using Microsoft . Security . Extensions ;
48
59namespace OpenClaw . Connection ;
@@ -11,9 +15,51 @@ internal readonly record struct AuthenticodeTrustResult(bool IsTrusted, string?
1115 public static AuthenticodeTrustResult Rejected ( string detail ) => new ( false , detail ) ;
1216}
1317
18+ /// <summary>
19+ /// Minimal projection of a Windows AppX/MSIX package as reported by
20+ /// <c>Get-AppxPackage</c>, used to corroborate genuine Microsoft WSL binaries that
21+ /// ship without a per-file Authenticode/catalog signature (MSIX packages are
22+ /// trusted at the package level, not per-file).
23+ /// </summary>
24+ internal readonly record struct AppxPackageInfo (
25+ string ? PackageFamilyName ,
26+ string ? Publisher ,
27+ string ? SignatureKind ) ;
28+
1429internal static class WindowsAuthenticodeVerifier
1530{
16- public static AuthenticodeTrustResult VerifyMicrosoftSignedFile ( string path )
31+ // Modern WSL ships as an MSIX package. The trailing suffix is a hash derived
32+ // from the publisher's signing certificate/public key, so an impostor package
33+ // re-signed with a different certificate would get a different family name.
34+ private const string WslPackageFamilyName =
35+ "MicrosoftCorporationII.WindowsSubsystemForLinux_8wekyb3d8bbwe" ;
36+
37+ public static AuthenticodeTrustResult VerifyMicrosoftSignedFile ( string path ) =>
38+ VerifyMicrosoftSignedFile ( path , LookupWslAppxPackageViaPowerShell ) ;
39+
40+ /// <summary>
41+ /// Test seam: allows callers to substitute a fake WSL AppX package lookup
42+ /// instead of shelling out to PowerShell.
43+ /// </summary>
44+ internal static AuthenticodeTrustResult VerifyMicrosoftSignedFile (
45+ string path ,
46+ Func < AppxPackageInfo ? > lookupWslPackage )
47+ {
48+ var primary = VerifyAuthenticodeSignature ( path ) ;
49+ if ( primary . IsTrusted )
50+ return primary ;
51+
52+ // MSIX packages (modern WSL) don't carry a per-file Authenticode/catalog
53+ // signature, so a failed classic check isn't conclusive for wslrelay.exe.
54+ // Only consult the package-level fallback for the WSL binaries this
55+ // codebase actually cares about; never widen trust for arbitrary files.
56+ if ( ! string . Equals ( Path . GetFileName ( path ) , "wslrelay.exe" , StringComparison . OrdinalIgnoreCase ) )
57+ return primary ;
58+
59+ return VerifyWslPackageFallback ( primary , lookupWslPackage ) ;
60+ }
61+
62+ private static AuthenticodeTrustResult VerifyAuthenticodeSignature ( string path )
1763 {
1864 try
1965 {
@@ -45,6 +91,50 @@ public static AuthenticodeTrustResult VerifyMicrosoftSignedFile(string path)
4591 }
4692 }
4793
94+ private static AuthenticodeTrustResult VerifyWslPackageFallback (
95+ AuthenticodeTrustResult primaryFailure ,
96+ Func < AppxPackageInfo ? > lookupWslPackage )
97+ {
98+ AppxPackageInfo ? package ;
99+ try
100+ {
101+ package = lookupWslPackage ( ) ;
102+ }
103+ catch
104+ {
105+ package = null ;
106+ }
107+
108+ if ( package is not { } info )
109+ {
110+ // No genuine WSL AppX package installed to corroborate; surface the
111+ // original Authenticode diagnostic rather than a new, less useful one.
112+ return primaryFailure ;
113+ }
114+
115+ if ( ! string . Equals ( info . PackageFamilyName , WslPackageFamilyName , StringComparison . Ordinal ) )
116+ {
117+ // Doesn't match the well-known family name (a different/impostor
118+ // package can't corroborate this binary); surface the original detail.
119+ return primaryFailure ;
120+ }
121+
122+ if ( string . IsNullOrEmpty ( info . SignatureKind ) ||
123+ string . Equals ( info . SignatureKind , "None" , StringComparison . OrdinalIgnoreCase ) )
124+ {
125+ return AuthenticodeTrustResult . Rejected (
126+ "WSL package signature verification failed: the installed WSL package is unsigned." ) ;
127+ }
128+
129+ if ( info . Publisher is null || ! HasMicrosoftPublisherIdentity ( info . Publisher ) )
130+ {
131+ return AuthenticodeTrustResult . Rejected (
132+ "WSL package signature verification failed: the installed WSL package's publisher is not Microsoft Corporation." ) ;
133+ }
134+
135+ return AuthenticodeTrustResult . Trusted ( ) ;
136+ }
137+
48138 internal static bool HasMicrosoftPublisherIdentity ( string subject ) =>
49139 subject . Split ( ',' )
50140 . Select ( part => part . Trim ( ) )
@@ -53,4 +143,110 @@ internal static bool HasMicrosoftPublisherIdentity(string subject) =>
53143 part ,
54144 "O=Microsoft Corporation" ,
55145 StringComparison . OrdinalIgnoreCase ) ) ;
146+
147+ private static AppxPackageInfo ? LookupWslAppxPackageViaPowerShell ( )
148+ {
149+ Process ? process = null ;
150+ try
151+ {
152+ var psi = new ProcessStartInfo
153+ {
154+ FileName = ResolvePowerShellPath ( ) ,
155+ UseShellExecute = false ,
156+ CreateNoWindow = true ,
157+ RedirectStandardOutput = true ,
158+ RedirectStandardError = true ,
159+ } ;
160+ psi . ArgumentList . Add ( "-NoProfile" ) ;
161+ psi . ArgumentList . Add ( "-NonInteractive" ) ;
162+ psi . ArgumentList . Add ( "-Command" ) ;
163+ psi . ArgumentList . Add (
164+ "Get-AppxPackage -Name 'MicrosoftCorporationII.WindowsSubsystemForLinux' | " +
165+ "Select-Object -First 1 PackageFamilyName, Publisher, SignatureKind | " +
166+ "ConvertTo-Json -Compress" ) ;
167+
168+ process = Process . Start ( psi ) ;
169+ if ( process is null )
170+ return null ;
171+
172+ using var timeout = new CancellationTokenSource ( TimeSpan . FromSeconds ( 8 ) ) ;
173+ var stdoutTask = process . StandardOutput . ReadToEndAsync ( timeout . Token ) ;
174+ var stderrTask = process . StandardError . ReadToEndAsync ( timeout . Token ) ;
175+
176+ if ( ! process . WaitForExit ( 8_000 ) )
177+ {
178+ try { process . Kill ( entireProcessTree : true ) ; } catch { }
179+ return null ;
180+ }
181+
182+ string output ;
183+ try
184+ {
185+ output = stdoutTask . GetAwaiter ( ) . GetResult ( ) ;
186+ _ = stderrTask . GetAwaiter ( ) . GetResult ( ) ;
187+ }
188+ catch
189+ {
190+ return null ;
191+ }
192+
193+ if ( process . ExitCode != 0 || string . IsNullOrWhiteSpace ( output ) )
194+ return null ;
195+
196+ return ParseAppxPackageJson ( output ) ;
197+ }
198+ catch
199+ {
200+ return null ;
201+ }
202+ finally
203+ {
204+ process ? . Dispose ( ) ;
205+ }
206+ }
207+
208+ private static string ResolvePowerShellPath ( )
209+ {
210+ var systemRoot = Environment . GetFolderPath ( Environment . SpecialFolder . Windows ) ;
211+ if ( string . IsNullOrWhiteSpace ( systemRoot ) )
212+ systemRoot = Environment . GetEnvironmentVariable ( "SystemRoot" ) ;
213+ if ( string . IsNullOrWhiteSpace ( systemRoot ) )
214+ systemRoot = @"C:\Windows" ;
215+ return Path . Combine ( systemRoot , "System32" , "WindowsPowerShell" , "v1.0" , "powershell.exe" ) ;
216+ }
217+
218+ private static AppxPackageInfo ? ParseAppxPackageJson ( string json )
219+ {
220+ try
221+ {
222+ using var document = JsonDocument . Parse ( json ) ;
223+ var root = document . RootElement ;
224+ if ( root . ValueKind == JsonValueKind . Array )
225+ {
226+ if ( root . GetArrayLength ( ) == 0 )
227+ return null ;
228+ root = root [ 0 ] ;
229+ }
230+ if ( root . ValueKind != JsonValueKind . Object )
231+ return null ;
232+
233+ var familyName = GetStringProperty ( root , "PackageFamilyName" ) ;
234+ if ( familyName is null )
235+ return null ;
236+
237+ return new AppxPackageInfo (
238+ familyName ,
239+ GetStringProperty ( root , "Publisher" ) ,
240+ GetStringProperty ( root , "SignatureKind" ) ) ;
241+ }
242+ catch
243+ {
244+ return null ;
245+ }
246+ }
247+
248+ private static string ? GetStringProperty ( JsonElement element , string propertyName ) =>
249+ element . TryGetProperty ( propertyName , out var value ) && value . ValueKind == JsonValueKind . String
250+ ? value . GetString ( )
251+ : null ;
56252}
0 commit comments