/// <summary> /// 通用(特征码替换) /// </summary> public void CommonPatch() { if (FileCommonModifyInfo == null) { throw new Exception("特征码替换:缺失对应特征码信息"); } // 1. 拷贝一份临时文件 File.Copy(FilePath, fileReplacedPath, true); // 2. 读取整个临时文件 byte[] fileByteArray = File.ReadAllBytes(fileReplacedPath); // 3. 循环查找所有未替换的替换点 int needReplaceNum = 0; List <Change> changes = new List <Change>(); foreach (ReplacePattern pattern in FileCommonModifyInfo.ReplacePatterns) { int[] indexs = FuzzyMatcher.MatchNotReplaced(fileByteArray, pattern.Search, pattern.Replace); if (indexs.Length == 1) { needReplaceNum++; changes.Add(new Change(indexs[0], pattern.Replace)); } } // 判断是否可以使用特征码替换的方式 if (needReplaceNum == 0) { // 查找所有替换点 int matchNum = 0; foreach (ReplacePattern pattern in FileCommonModifyInfo.ReplacePatterns) { int[] indexs = FuzzyMatcher.MatchAll(fileByteArray, pattern.Search); if (indexs.Length == 1) { matchNum++; } } if (matchNum == FileCommonModifyInfo.ReplacePatterns.Count) { throw new BusinessException("already_replace", "特征码替换:当前应用已经防撤回"); } else { throw new BusinessException("not_found_to_replace", "特征码替换:没有搜索到撤回的相关特征"); } } else if (needReplaceNum == FileCommonModifyInfo.ReplacePatterns.Count) { // 正常情况下每个替换点都能找到 // 3. 替换所有未替换的替换点 FileUtil.EditMultiHex(fileReplacedPath, changes); // 4. 覆盖特征码替换后的文件 File.Copy(fileReplacedPath, FilePath, true); } else { throw new BusinessException("found_num_err", "特征码替换:可替换的特征数不正确"); } }
private void btnSearch_Click(object sender, EventArgs e) { byte[] fileByteArray = File.ReadAllBytes(@""); byte[] searchBytes = ByteUtil.HexStringToByteArray("1C E9 9D 00 00 00 8B 45 E8 8D 55 EC 52 89 5D EC 68 3F 3F 3F 54 8B 08 50 FF 51 78 85 C0 79 2D 8D 45 0C C7 45 0C"); byte[] replaceBytes = ByteUtil.HexStringToByteArray("1C E9 9D 00 00 00 8B 45 E8 8D 55 EC 52 89 5D EC EB 09 90 90 90 8B 08 50 FF 51 78 85 C0 79 2D 8D 45 0C C7 45 0C"); //int[] indexs = FuzzyMatcher.MatchAll(fileByteArray, searchBytes); int[] indexs = FuzzyMatcher.MatchNotReplaced(fileByteArray, searchBytes, replaceBytes); txtInfo.AppendText("查找结果位置:" + string.Join(",", indexs) + Environment.NewLine); // 371130 List <Change> changes = ComputChanges(indexs, searchBytes, replaceBytes); foreach (Change c in changes) { txtInfo.AppendText("替换位置:" + Convert.ToString(c.Position, 16) + " 替换内容:" + ByteUtil.ByteArrayToHexString(c.Content) + Environment.NewLine); } }