2012-07-21 00:59:10 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2012-07-21 00:59:10 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2012-07-21 00:59:10 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.MaxtoCode {
|
|
|
|
|
class StringDecrypter {
|
|
|
|
|
DecrypterInfo decrypterInfo;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef decryptMethod;
|
2012-07-21 00:59:10 +08:00
|
|
|
|
string[] decryptedStrings;
|
|
|
|
|
Encoding encoding;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
get { return decryptMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return decryptMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StringDecrypter(DecrypterInfo decrypterInfo) {
|
|
|
|
|
this.decrypterInfo = decrypterInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
if (decrypterInfo == null)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decryptMethod = FindDecryptMethod(decrypterInfo.mainType.Type);
|
2012-07-21 00:59:10 +08:00
|
|
|
|
if (decryptMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef FindDecryptMethod(TypeDef type) {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
return null;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || !method.IsStatic || method.IsPrivate)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.String", "(System.UInt32)"))
|
2012-07-21 00:59:10 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(method, "System.String System.Runtime.InteropServices.Marshal::PtrToStringAnsi(System.IntPtr)"))
|
2012-07-21 00:59:10 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize(Encoding encoding) {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
this.encoding = encoding;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeStrings() {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
if (decryptedStrings != null)
|
|
|
|
|
return;
|
|
|
|
|
var peImage = decrypterInfo.peImage;
|
|
|
|
|
var peHeader = decrypterInfo.peHeader;
|
|
|
|
|
var mcKey = decrypterInfo.mcKey;
|
|
|
|
|
var fileData = decrypterInfo.fileData;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var stringsRva = peHeader.GetRva(0x0AF0, mcKey.ReadUInt32(0x46));
|
2012-07-21 00:59:10 +08:00
|
|
|
|
if (stringsRva == 0)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int stringsOffset = (int)peImage.RvaToOffset(stringsRva);
|
2012-07-21 00:59:10 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int numStrings = peImage.ReadInt32(stringsRva) ^ (int)mcKey.ReadUInt32(0);
|
2012-07-21 00:59:10 +08:00
|
|
|
|
decryptedStrings = new string[numStrings];
|
|
|
|
|
for (int i = 0, ki = 2, soffs = stringsOffset + 4; i < numStrings; i++) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int stringLen = BitConverter.ToInt32(fileData, soffs) ^ (int)mcKey.ReadUInt32(ki);
|
2012-07-21 00:59:10 +08:00
|
|
|
|
ki += 2;
|
|
|
|
|
if (ki >= 0x1FF0)
|
|
|
|
|
ki = 0;
|
|
|
|
|
soffs += 4;
|
|
|
|
|
var bytes = new byte[stringLen];
|
|
|
|
|
for (int j = 0; j < stringLen; j++, soffs++) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte b = (byte)(fileData[soffs] ^ mcKey.ReadByte(ki));
|
|
|
|
|
ki = Add(ki, 1);
|
2012-07-21 00:59:10 +08:00
|
|
|
|
bytes[j] = b;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decryptedStrings[i] = Decode(bytes);
|
2012-07-21 00:59:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
string Decode(byte[] bytes) {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
string s = encoding.GetString(bytes);
|
|
|
|
|
int len = s.Length;
|
|
|
|
|
if (len == 0 || s[len - 1] != 0)
|
|
|
|
|
return s;
|
|
|
|
|
for (; len > 0; len--) {
|
|
|
|
|
if (s[len - 1] != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (len <= 0)
|
|
|
|
|
return string.Empty;
|
|
|
|
|
return s.Substring(0, len);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static int Add(int ki, int size) {
|
2012-07-21 00:59:10 +08:00
|
|
|
|
return (ki + size) % 0x1FF0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string Decrypt(uint id) {
|
|
|
|
|
InitializeStrings();
|
2012-07-21 00:59:10 +08:00
|
|
|
|
return decryptedStrings[(int)id - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|