当前位置:
首页 > Python基础教程 >
-
汽车之家配置页面 破解伪元素和混淆JS(2)
"))
117 {
118 var tmp = Regex.Match(tmp13[j], @"return\s{0,2}'([0-9]|[,]|[;]){1,10}'").Value.ToLower().Replace("return", "").Replace("'", "").Trim();
119 sb2.Append(tmp);
120 }
121 else if (Regex.IsMatch(tmp13[j], @"\(\)"))
122 {
123 tmp13[j] = tmp13[j].Substring(0, tmp13[j].IndexOf("()") + 2);
124 sb2.Append(dc[tmp13[j].Replace("()", "")]);
125 }
126 else if (Regex.IsMatch(tmp13[j], @"\S{3}") && tmp13[j].IndexOf("'") < 0)
127 {
128 sb2.Append(dc[tmp13[j]]);
129 }
130 else if (tmp13[j].Split(new string[] { "'" }, StringSplitOptions.None).Length > 2)
131 {
132 sb2.Append(tmp13[j].Replace("'", "").Trim());
133 }
134 else if (tmp13[j].Trim() == "''")
135 {
136 continue;
137 }
138 else
139 {
140 sb2.Append("X");
141 }
142 }
143 }
144 catch (Exception ex)
145 {
146 throw;
147 }
148
149 #endregion
150
151 #region 生成字典
152 List<string> list = new List<string>();
153 try
154 {
155 foreach (var item in sb2.ToString().Split(';'))
156 {
157 var numlist = item.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
158 StringBuilder sbresult = new StringBuilder();
159 foreach (var num in numlist)
160 {
161 var tmpstr = sb.ToString()[Cvt.ToInt32(num)];
162 sbresult.Append(tmpstr);
163 }
164 list.Add(sbresult.ToString());
165 }
166 }
167 catch (Exception e)
168 {
169
170 throw;
171 }
172
173 #endregion
174
175 if (i == 0) keyLink = list.ToArray();
176 else if (i == 1) configpl = list.ToArray();
177 else if (i == 2) optionpl = list.ToArray();
178 }
179 }
1 /// <summary> 2 /// 格式化字符串 3 /// </summary> 4 /// <param name="str"></param> 5 /// <param name="resultKey"></param> 6 /// <param name="resultValue"></param> 7 public void getStr(string str, ref string resultKey, ref string resultValue) 8 { 9 try 10 { 11 if (str.IndexOf("var") > 0) 12 { 13 str = str.Substring(0, str.IndexOf("var")); 14 } 15 resultKey = str.Split(new string[] { "()" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault().Replace("function", "").Trim(); 16 resultValue = JSHelper.ExecJs(str + " " + resultKey + "();").ToString(); 17 return; 18 } 19 catch (Exception ex) 20 { 21 throw; 22 } 23 } 24 public void getStr2(string str, ref string resultKey, ref string resultValue) 25 { 26 try 27 { 28 string[] str2 = str.Replace("var", "").Replace("\'", "").Trim().Split('='); 29 resultKey = str2[0]; 30 resultValue = str2[1]; 31 } 32 catch (Exception ex) 33 { 34 throw ex; 35 } 36 } 37 public void getStr3(string str, ref string resultKey, ref string resultValue) 38 { 39 try 40 {//var AC_=function(){'AC_';var _A=function(){return '格';}; return _A();} 41 string[] str2 = str.Replace("var", "").Trim().Split('='); 42 resultKey = str2[0]; 43 if (str.Split(new string[] { "function" }, StringSplitOptions.None).Length > 2) 44 { 45 string str3 = Regex.Match(str, @"var\s?\S\S_=\s?function\s?\(\S{0,5}\)\s?\{.*?return.*?\}").Value;//取出赋值函数 46 string str4 = str3.Substring(str3.IndexOf("return") + 6); 47 string[] str5 = str4.Split(new string[] { "\'" }, StringSplitOptions.None); 48 resultValue = str5[1]; 49 } 50 else 51 { 52 string str3 = str2[str2.Length - 1].Substring(str2[str2.Length - 1].LastIndexOf("return")); 53 string[] str4 = str3.Split('\''); 54 resultValue = str4[1]; 55 } 56 } 57 catch (Exception ex) 58 { 59 throw ex; 60 } 61 }
文中部分解析直接将变量丢进了JS里执行,这个破解比较早,用的JScript,现在推荐使用MSScriptControl.ScriptControl,这个是com组件里的。
1 using Microsoft.JScript; 2 using Microsoft.JScript.Vsa; 3 using System; 4 using System.CodeDom.Compiler; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Reflection; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace library 12 { 13 public static class JSHelper 14 { 15 static VsaEngine Engine = VsaEngine.CreateEngine(); 16 public static object ExecJs(string str) 17 { 18 return EvalJScript(str); 19 } 20 public static object EvalJScript(string JScript) 21 { 22 object Result = null; 23 try 24 { 25 Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine); 26 } 27 catch (Exception ex) 28 { 29 return ex.Message; 30 } 31 return Result; 32 33 } 34 } 35 }
这种稍微复杂点的爬虫真的十分锻炼分析能力和耐心,这也是笔者认为开发者十分重要的一种能力,而对于.net这种门槛较低,技术能力金字塔分布的开发群体,真的需要我们好好钻研技术。
如有不明或更好的建议,欢迎留言交流。
作者:Grom
出处:http://www.cnblogs.com/grom/