How to validate authenticode for Javascript in C#

后端 未结 2 1386
难免孤独
难免孤独 2021-01-22 21:36

I was able to sign a js file with PowerShell Set-AuthenticodeSignature. After that i can see signature appeared in file in form of:

// SIG // Begin signature blo         


        
相关标签:
2条回答
  • 2021-01-22 22:27

    WinVerifyTrust supports verifying files other than executables using the WTD_CHOICE_BLOB flag. Make sure you provide the WINTRUST_BLOB_INFO struct with the correct subject interface package (SIP). From what I can see, Get-AuthenticodeSignature command uses the PowerShell SIP {603bcc1f-4b59-4e08-b724-d2c6297ef351} to verify the signature. I assume Set-AuthenticodeSignature uses the same SIP to sign the script.

    0 讨论(0)
  • 2021-01-22 22:33

    I recently encountered similar problem and let me show what I did to solve this problem. Before I go , there are few assumptions I make now. Please correct me if I am wrong.

    1. wintrust is working for all other cases other than script files like .js or .vbs
    2. You might have attempted "wintrustverify" from an console application (C#)

    I figured it out this happens only with script files as I have mentioned above because wintrust behaves wierdly when its methods are being executed from free-threaded apartment model (MTA). Once it's been wrapped inside a STA thread, it started working for me. Later I came to know it is a historical issue that we should have taken a precaution when we deal with any COM components interoperations from .Net application.

    Here is the code snippet, you can replace the verifysignature with your wintrust code logic and try. I hope this helps.

                public static void CheckSignature()
                {
                    STAApartment apt = new STAApartment();
                    var result = apt.Invoke(() =>
                    {
                        return VerifySignature(@".\signedjsfile.js", false);
                    });
                    Console.WriteLine(result);
                }
    
                private static WinVerifyTrustResult VerifySignature(string filePath, bool verifySignatureOnly)
                {
    
                    using (var wtd = new WinTrustData(new WinTrustFileInfo(filePath))
                    {
                        dwUIChoice = WintrustUIChoice.WTD_UI_NONE,
                        dwUIContext = WinTrustDataUIContext.WTD_DATA_UI_EXECUTE,
                        fdwRevocationChecks = WinTrustDataRevocationChecks.WTD_REVOCATION_CHECK_WHOLECHAIN,
                        dwStateAction = WintrustAction.WTD_STATEACTION_IGNORE,
                        dwProvFlags = verifySignatureOnly ? WintrustProviderFlags.WTD_HASH_ONLY_FLAG : WintrustProviderFlags.WTD_REVOCATION_CHECK_CHAIN
                    })
                    {
                        var result = WinTrust.WinVerifyTrust(
                            WinTrust.INVALID_HANDLE_VALUE, new Guid(WinTrust.WINTRUST_ACTION_GENERIC_VERIFY_V2), wtd
                        );
                        return result;
                    }
                }
    
                public class STAApartment
                {
                    public T Invoke<T>(Func<T> func)
                    {
                        var tcs = new TaskCompletionSource<T>();
                        Thread thread = new Thread(() =>
                        {
                            try
                            {
                                tcs.SetResult(func());
                            }
                            catch (Exception e)
                            {
                                tcs.SetException(e);
                            }
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();                
                        return tcs.Task.Result;
                    }
                }
    
    0 讨论(0)
提交回复
热议问题