using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace selfverification
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (!StrongNameHelper.VerifyStrongName())
{
// The assembly is tampered so:
// Application.Exit();, or:
while (true)
{}
}
}
}
internal static class StrongNameHelper
{
private static Guid CLSID_CLRMetaHost =
new Guid
(0x9280188D, 0xE8E, 0x4867, 0xB3, 0xC, 0x7F, 0xA8, 0x38, 0x84, 0xE8, 0xDE
);
private static Guid CLSID_CLRStrongName =
new Guid
(0xb79b0acd, 0xf5cd, 0x409b, 0xb5, 0xa5, 0xa1, 0x62, 0x44, 0x61,
11, 0x92
);
private static Guid IID_CLRStrongName =
new Guid
(0x9fd93ccf, 0x3280, 0x4391, 0xb3, 0xa9,
150, 0xe1, 0xcd, 0xe7, 0x7c, 0x8d
);
private static Guid IID_ICLRMetaHost =
new Guid
(0xD332DB9E, 0xB9B3, 0x4125, 0x82, 0x07, 0xA1, 0x48, 0x84, 0xF5, 0x32, 0x16
);
private static Guid IID_ICLRRuntimeInfo =
new Guid
(0xbd39d1d2, 0xba2f, 0x486a, 0x89, 0xb0,
180, 0xb0, 0xcb,
70, 0x68, 0x91
);
[DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
internal static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool pfWasVerified);
[DllImport("mscoree.dll", EntryPoint = "CLRCreateInstance")]
private static extern int CLRCreateInstance(ref Guid clsid, ref Guid riid, ref IntPtr ppInterface);
internal static bool VerifyStrongName()
{
// NOTE:
//encodedKey == Convert.ToBase64String(YOUR_ASSEMBLY.GetName().GetPublicKeyToken());
string encodedKey = "lCvfqUGP9m8="; //encodedKey
if ((Environment.Version.Major >= 4))
return VerifyStrongNameCLR4(encodedKey);
return VerifyStrongNameCLR2(encodedKey);
}
internal static bool VerifyStrongNameCLR2(string encodedKey)
{
bool result = false;
try
{
if (!string.IsNullOrEmpty(encodedKey))
{
byte[] buffer = Convert.FromBase64String(encodedKey);
byte[] publicKeyToken = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken();
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] != publicKeyToken[i])
return false;
}
}
bool wasVerified = false;
string location = Assembly.GetExecutingAssembly().Location;
if (string.IsNullOrEmpty(location))
{
result = true;
}
else
{
bool ret = StrongNameSignatureVerificationEx(location, true, ref wasVerified);
result = wasVerified && ret;
}
}
catch (Exception)
{
}
return result;
}
internal static bool VerifyStrongNameCLR4(string encodedKey)
{
bool result = false;
try
{
if (!string.IsNullOrEmpty(encodedKey))
{
byte[] buffer = Convert.FromBase64String(encodedKey);
byte[] publicKeyToken = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken();
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] != publicKeyToken[i])
return false;
}
}
string location = Assembly.GetExecutingAssembly().Location;
result = string.IsNullOrEmpty(location) || VerifyStrongNameCLR4_internal(location);
}
catch (Exception)
{
}
return result;
}
private static bool VerifyStrongNameCLR4_internal(string path)
{
ICLRMetaHost metaHost = null;
ICLRRuntimeInfo runtimeInfo = null;
ICLRStrongName strongName = null;
try
{
IntPtr pointer = IntPtr.Zero;
if ((CLRCreateInstance(ref CLSID_CLRMetaHost, ref IID_ICLRMetaHost, ref pointer) == 0) && (pointer != IntPtr.Zero))
{
metaHost = Marshal.GetObjectForIUnknown(pointer) as ICLRMetaHost;
Marshal.Release(pointer);
string str = Environment.Version.ToString();
string version = "v" + str.Substring(0, str.LastIndexOf('.'));
pointer = metaHost.GetRuntime(version, ref IID_ICLRRuntimeInfo);
if (pointer != IntPtr.Zero)
{
runtimeInfo = Marshal.GetObjectForIUnknown(pointer) as ICLRRuntimeInfo;
Marshal.Release(pointer);
pointer = IntPtr.Zero;
pointer = runtimeInfo.GetInterface(ref CLSID_CLRStrongName, ref IID_CLRStrongName);
if (pointer != IntPtr.Zero)
{
strongName = Marshal.GetObjectForIUnknown(pointer) as ICLRStrongName;
Marshal.Release(pointer);
sbyte wasVerified = 0;
sbyte ret = strongName.StrongNameSignatureVerificationEx(path, 1, ref wasVerified);
return ((ret == 0) && (wasVerified != 0));
}
}
}
}
finally
{
if (strongName != null)
{
Marshal.ReleaseComObject(strongName);
}
if (runtimeInfo != null)
{
Marshal.ReleaseComObject(runtimeInfo);
}
if (metaHost != null)
{
Marshal.ReleaseComObject(metaHost);
}
}
return false;
}
}
[ComImport, Guid("D332DB9E-B9B3-4125-8207-A14884F53216"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ICLRMetaHost
{
...
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
internal interface ICLRRuntimeInfo
{
...
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComConversionLoss, Guid("9FD93CCF-3280-4391-B3A9-96E1CDE77C8D")]
internal interface ICLRStrongName
{
...
}
[ComImport, Guid("00000100-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IEnumUnknown
{
...
}
}