1. Preface
Analysis Trojan often encountered a lot of configuration information is encrypted, although it is not directly analyzed, but through the Wireshark such as direct reading records.
2017 gh0st Sample A large number of new, through the Trojan source analysis also found that the use of GH0ST encryption way to spread the back door in the source code.
2, encryption ideas
-
Control side: XOR, SHIFT, BASE64 encoding for strings
-
Server: Decode, SHIFT, XOR, or Base64 the string
3. Practice Code
Base64 coded static char base64[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; int C mimic gh0st encryption on-line address dlg::base64_encode (const void *data, int size, char **str) {char *s, *p; int i; int C; const unsigned char *q; p = s = (char*) malloc (Size * 4/3 + 4); if (p = = NULL) return-1; Q = (const unsigned char*) data; i = 0; for (i = 0; i < size;) {c = q[i++]; c *= 256; if (i < size) c + = Q[i]; i++; c *= 256; if (i < size) c + = Q[i]; i++; P[0] = base64[(C & 0x00fc0000) >> 18]; Base64 p[1] = base64[(C & 0x0003f000) >> 12]; P[2] = base64[(C & 0x00000fc0) >> 6]; P[3] = base64[(C & 0x0000003f) >> 0]; if (i > Size) p[3] = ' = '; if (i > Size + 1) p[2] = ' = '; p + = 4; } *p = 0; *str = s; return strlen (s);} cryptographic function char * C mimics gh0st encrypted on-line address Dlg::myencOde (char *str) {int i, Len; Char *s, *data; String length len = strlen (str) + 1; Open a string corresponding to the memory space s = (char *) malloc (len); Copy the string into the open pointer memcpy (s, str, len); XOR operation for (i = 0; i < len; i++) {s[i] ^= 0x19; S[i] + = 0x86; }//BASE64 encoded Base64_encode (s, Len, &data); Free (s); return data;}
Base64 key static char base64[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";//decode Key int C Mimic gh0st encrypted on-line address DLG::p os (char c) {char *p; for (p = base64; *p; p++) if (*p = = c) return p-base64; return-1;} BASE64 decoding int c mimics gh0st encrypted on-line address Dlg::base64_decode (const char *STR, char **data) {const char *s, *p; unsigned char *q; int C; int x; int done = 0; int Len; s = (const char *) malloc (strlen (str)); Q = (unsigned char *) s; for (p = str; *p &&!done; p + = 4) {x = pos (p[0]); if (x >= 0) c = x; else {done = 3; Break } c *= 64; x = pos (p[1]); if (x >= 0) C + = x; else return-1; C *= 64; if (p[2] = = ' = ') done++; else {x = pos (p[2]); if (x >= 0) C + = x; else return-1; } c *= 64; if (P[3] = = ' = ') done++; else {if (done) return-1; x = pos (p[3]); if (x >= 0) C + = x; else return-1; } if (done < 3) *q++ = (C & 0x00ff0000) >> 16; if (done < 2) *q++ = (C & 0x0000ff00) >> 8; if (done < 1) *q++ = (C & 0x000000ff) >> 0; Len = q-(unsigned char*) (s); *data = (char*) realloc ((void *) s, Len); return Len;} Decryption function char* c mimics gh0st encrypted on-line address Dlg::mydecode (char *str) {int i, Len; char *data = NULL; len = Base64_decode (str, &data); for (i = 0; i < len; i++) {data[i]-= 0x86; Data[i] ^= 0x19; } return data;
Then is the content of the encryption and decryption interface.
Function code:
- Generate Configuration Information button
void C模仿Gh0st加密上线地址Dlg::OnBnBuild(){ // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_remote_host.Replace(L" ", L""); m_remote_port.Replace(L" ", L""); CString str = m_remote_host + ":" + m_remote_port; str.MakeLower(); TCHAR * pWStrKey; // 合并后的内容 // 开辟数组 pWStrKey = new TCHAR[str.GetLength() + 1]; pWStrKey = str.GetBuffer(0); // 第一次 调用确认转换后单字节字符串的长度,用于开辟空间 int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWStrKey, wcslen(pWStrKey), NULL, 0, NULL, NULL); // 单字符 char* pCStrKey = new char[pSize + 1]; // 第二次 调用将双字节字符串转换成单字节字符串 WideCharToMultiByte(CP_OEMCP, 0, pWStrKey, wcslen(pWStrKey), pCStrKey, pSize, NULL, NULL); pCStrKey[pSize] = ‘\0‘; // 接收char* m_encode = MyEncode(pCStrKey); m_encode.Insert(0, L"AAAA"); m_encode += "AAAA"; UpdateData(FALSE);}
- Decrypt Configuration Information button
void C模仿Gh0st加密上线地址Dlg::OnBnGetLoginInfo(){ // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); // 1 获取加密编辑控件里的字符串 int pSize = m_encode.GetLength() + 1; wchar_t * pStart, *pEnd; wchar_t strKey[] = L"AAAA"; wchar_t strEncode[1024]; // 拿到最开始的字符串,如果前面是AAAA就赋值到pStart pStart = wcsstr(m_encode.GetBuffer(0), strKey); // 前移动4个字节,也就是跳过AAAA pStart += 4; // 如果AAAA匹配不到把strKey,也就是AAAA赋值给pEnd pEnd = wcsstr(pStart, strKey); // 清空原先AAAA字符串 wmemset(strEncode, 0, wcslen(pStart) + 1); // 取key值之间的内容,配置字符串 - 字符串长度,取前面的真实加密字符串 wmemcpy(strEncode, pStart, pEnd - pStart); // 单字符 char* pCStrKey = new char[wcslen(strEncode) + 1]; // 调用将双字节字符串转换成单字节字符串 WideCharToMultiByte(CP_OEMCP, 0, strEncode, wcslen(strEncode) + 1, pCStrKey, wcslen(strEncode) + 1, NULL, NULL); pCStrKey[pSize] = ‘\0‘; // 解密代码 m_decode = MyDecode(pCStrKey); UpdateData(FALSE);}
4. Actual effect
5. Reference
gh0st3.6 source Analysis (1)--Trojan horse generation
http://blog.csdn.net/hjxyshell/article/details/19094609
"Algorithm" GH0ST Configuration Encryption Algorithm (XOR, BASE64)