by WebSurfer
2013年4月12日 15:15
WebResource.axd や ScriptResource.axd のクエリ文字列のパラメータ d の値を復号化する方法を備忘録として書いておきます。
利用目的は、WebResource.axd や ScriptResource.axd に指定されているリソースを特定するためです。クエリ文字列のパラメータ d にはリソースが指定されています。しかし、パラメータ d は暗号化されているため、リソースを特定するためには復号しなければなりません。
内容は、先の記事「WebResource.axd の HTTP 404 エラー」で紹介したページのコードとほとんど同じですが、リンク切れになると困るので、主要な部分のみ抜き出しました。
上の画像を表示したコードは以下の通りです。このページを運用環境に含めたり、一般に公開したりすると、セキュリティ上の問題が生ずる恐れがありますので注意してください。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Text" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
string input = TextBox1.Text;
if (string.IsNullOrEmpty(input))
{
Label1.Text = string.Empty;
Label2.Text = string.Empty;
return;
}
string pattern = "d=(?<param>[^&]+)&";
Match match = Regex.Match(input, pattern);
if (!match.Success)
{
Label1.Text = string.Empty;
Label2.Text = string.Empty;
return;
}
GroupCollection groups = match.Groups;
string data = groups["param"].Value;
byte[] encryptedData =
HttpServerUtility.UrlTokenDecode(data);
if (encryptedData == null)
{
Label1.Text = data;
Label2.BackColor = System.Drawing.Color.Red;
Label2.Text = "無効なパラメータ";
return;
}
Type machineKeySection = typeof(MachineKeySection);
Type[] paramTypes = new Type[] { typeof(bool),
typeof(byte[]),
typeof(byte[]),
typeof(int),
typeof(int) };
MethodInfo methodInfo = machineKeySection.GetMethod(
"EncryptOrDecryptData",
BindingFlags.Static | BindingFlags.NonPublic,
null,
paramTypes,
null);
try
{
byte[] decryptedData = (byte[])methodInfo.Invoke(
null,
new object[] { false,
encryptedData,
null,
0,
encryptedData.Length });
string decrypted = Encoding.UTF8.GetString(decryptedData);
TextBox1.Text = string.Empty;
Label1.Text = data;
Label2.BackColor = System.Drawing.Color.Lime;
Label2.Text = decrypted;
}
catch (TargetInvocationException)
{
TextBox1.Text = string.Empty;
Label1.Text = data;
Label2.BackColor = System.Drawing.Color.Red;
Label2.Text = "復号に失敗";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>d パラメータを復号</title>
</head>
<body>
<form id="form1" runat="server">
<div>
URL をコピー&ペースト
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="復号" OnClick="Button1_Click" />
<hr />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</div>
</form>
</body>
</html>