在反编译大型代码库时使用CallSite <> p__site

时间:2019-05-30 12:01:39

标签: c# decompiler

我有一个来自客户的大型代码库,该客户的开发人员拒绝提供代码。

尽管我在关注

时遇到了问题,但我已经对其进行了反编译。

我正在使用dnSpy,Just Decompile,dotPeek来反编译代码。我试图谷歌,也查看了Stackoverflow,但找不到任何可以回答我的东西。如果有人能指出正确的方向,我将不胜感激。

更新:我已粘贴完整功能

public virtual string ParseDealerInventoryLink(string toParseLinkData)
        {
            string pattern = "{([^}]+)}";
            string text = toParseLinkData;
            RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled;
            if (this != null && !string.IsNullOrWhiteSpace(text))
            {
                object dyn = new PropertyBag(this);
                Regex regex = new Regex(pattern, options);
                Dictionary<string, DealerInventoryLinkPatternParam> linkPatternMap = DealerInventoryFeedUrlParser.GetLinkPatternParamValueMap();
                text = regex.Replace(text, delegate(Match mat)
                {
                    if (mat.Success && mat.Groups.Count > 0)
                    {
                        Group group = mat.Groups[mat.Groups.Count - 1];
                        if (group.Success)
                        {
                            string value = group.Value;
                            if (linkPatternMap.ContainsKey(value))
                            {
                                if (DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site1 == null)
                                {
                                    DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site1 = CallSite<Func<CallSite, object, string>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(DealerInventoryFeedUrlParser)));
                                }
                                Func<CallSite, object, string> target = DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site1.Target;
                                CallSite <>p__Site = DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site1;
                                if (DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site2 == null)
                                {
                                    DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site2 = CallSite<Func<CallSite, Type, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(DealerInventoryFeedUrlParser), new CSharpArgumentInfo[]
                                    {
                                        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null),
                                        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                                    }));
                                }
                                Func<CallSite, Type, object, object> target2 = DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site2.Target;
                                CallSite <>p__Site2 = DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site2;
                                Type typeFromHandle = typeof(Convert);
                                if (DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site3 == null)
                                {
                                    DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site3 = CallSite<Func<CallSite, object, string, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetIndex(CSharpBinderFlags.None, typeof(DealerInventoryFeedUrlParser), new CSharpArgumentInfo[]
                                    {
                                        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                                        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null)
                                    }));
                                }
                                return target(<>p__Site, target2(<>p__Site2, typeFromHandle, DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site3.Target(DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site3, dyn, linkPatternMap[value].ColumnName)));
                            }
                        }
                    }
                    return mat.Value;
                });
            }
            return text;
        }

1 个答案:

答案 0 :(得分:0)

function onSuccess(result) {
        var GetTotal;
        if (result) {
          GetTotal = function(result) {

                var totBal = 0;
                var str = 'R';
                for (var i = 0; i < result.length; i++) {
                    totBal += result[i].Price;
                }
                str += totBal.toFixed(2);
                return str;
            };
            var span = document.getElementById("Total");
            span.innerHTML = GetTotal(result);
        }
    }

在ILSpy中进行编译然后反编译时会生成以下内容:

class DealerInventoryFeedUrlParser
{
    void ParseDealerInventoryLink()
    {
        dynamic input = new object();
        string link = input;
    }
}

...我将其归类为“足够接近”。

我没有特别的理由选择class DealerInventoryFeedUrlParser { private void ParseDealerInventoryLink() { object input = new object(); if (<>o__0.<>p__0 == null) { <>o__0.<>p__0 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(DealerInventoryFeedUrlParser))); } string link = <>o__0.<>p__0.Target(<>o__0.<>p__0, input); } } 作为分配给new object()的值。这可能是一个整数,字符串-任何东西。 dynamic初始化代码看起来相同。 CallSite中的T2总是 对象,因此除非您查看反编译源中CallSite<Func<T1, T2, TResult>>的位置,否则您将不知道真正的输入值是什么。是实际使用的。

编辑

现在已经发布了完整的源代码,让我们尝试将各个部分放在一起:

DealerInventoryFeedUrlParser.<ParseDealerInventoryLink>o__SiteContainer0.<>p__Site1

class DealerInventoryFeedUrlParser { static Dictionary<string, DealerInventoryLinkPatternParam> GetLinkPatternParamValueMap() => throw new NotImplementedException(); public virtual string ParseDealerInventoryLink(string toParseLinkData) { string pattern = "{([^}]+)}"; string text = toParseLinkData; RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled; if (/*this != null &&*/ !string.IsNullOrWhiteSpace(text)) { dynamic dyn = new PropertyBag(this); Regex regex = new Regex(pattern, options); Dictionary<string, DealerInventoryLinkPatternParam> linkPatternMap = GetLinkPatternParamValueMap(); text = regex.Replace(text, delegate (Match mat) { if (mat.Success && mat.Groups.Count > 0) { Group group = mat.Groups[mat.Groups.Count - 1]; if (group.Success) { string value = group.Value; if (linkPatternMap.ContainsKey(value)) { return Convert.ToString(dyn[linkPatternMap[value].ColumnName]); } } } return mat.Value; }); } return text; } } class PropertyBag { private readonly object Owner; public PropertyBag(object obj) { Owner = obj; } } class DealerInventoryLinkPatternParam { public string ColumnName { get; set; } } 编译/反编译:

ParseDealerInventoryLink

您可以替换编译器生成的名称,以检查是否一切正确。根据我对public virtual string ParseDealerInventoryLink(string toParseLinkData) { string pattern = "{([^}]+)}"; string text = toParseLinkData; RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled; if (!string.IsNullOrWhiteSpace(text)) { dynamic dyn = new PropertyBag(this); Regex regex = new Regex(pattern, options); Dictionary<string, DealerInventoryLinkPatternParam> linkPatternMap = DealerInventoryFeedUrlParser.GetLinkPatternParamValueMap(); text = regex.Replace(text, delegate(Match mat) { if (mat.Success && mat.Groups.Count > 0) { Group group = mat.Groups[mat.Groups.Count - 1]; if (group.Success) { string value = group.Value; if (linkPatternMap.ContainsKey(value)) { if (<>o__1.<>p__2 == null) { <>o__1.<>p__2 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(DealerInventoryFeedUrlParser))); } Func<CallSite, object, string> target = <>o__1.<>p__2.Target; CallSite<Func<CallSite, object, string>> <>p__ = <>o__1.<>p__2; if (<>o__1.<>p__1 == null) { <>o__1.<>p__1 = CallSite<Func<CallSite, Type, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(DealerInventoryFeedUrlParser), new CSharpArgumentInfo[2] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) })); } Func<CallSite, Type, object, object> target2 = <>o__1.<>p__1.Target; CallSite<Func<CallSite, Type, object, object>> <>p__2 = <>o__1.<>p__1; Type typeFromHandle = typeof(Convert); if (<>o__1.<>p__0 == null) { <>o__1.<>p__0 = CallSite<Func<CallSite, object, string, object>>.Create(Binder.GetIndex(CSharpBinderFlags.None, typeof(DealerInventoryFeedUrlParser), new CSharpArgumentInfo[2] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null) })); } return target(<>p__, target2(<>p__2, typeFromHandle, <>o__1.<>p__0.Target(<>o__1.<>p__0, dyn, linkPatternMap[value].ColumnName))); } } } return mat.Value; }); } return text; } 的有限理解,我所做的只是一个诚实但非常非常快的尝试,可以猜测我在看什么。现在,您可以检查所有内容,验证正确性并在必要时进行修复。

P.S。 dynamic校验位使我有些困惑。必须是反编译器的怪癖。我已将其注释掉,因为它似乎并不重要。

P.P.S。反编译和重新编译二进制文件是一个非常肮脏且容易出错的过程。如果您需要进行的更改很小,请考虑研究.NET二进制修补程序(Google it),即使用ILSpy + Reflexil。

祝你好运。