首页
社区
课程
招聘
[原创]Minlink Engine V1.0
发表于: 2012-9-4 12:10 12906

[原创]Minlink Engine V1.0

2012-9-4 12:10
12906
RT.顺便妹子.
简介一下几个函数.没使用反汇编引擎,因此遇到特定的指令序列的时候会出错.测试了10个程序左右.我运气好,只有一个出错了.
计算对其值.
unsigned long alignment(unsigned long size,unsigned long align)
{
	if(size%align!=0)
		return  (size/align+1)*align;
	return size;
}

变形 push xxxxxxxx
void _link_push_imm(void * dst,size_t _new_push_rva,void * src,size_t _code_push_rva,size_t _image_base_offset)
{
	/*
	    jmp     xxxxxxxx
        sub     esp, 4
        mov     dword ptr [esp], -1
        jmp     FFFFFFFF
	*/
	unsigned char _rand_reg[] = {0x50, 0x51, 0x52, 0x53, 0x53, 0x54, 0x55, 0x56, 0x57};
	unsigned char push[] = {0x50, 
		                    0xC7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00, 
                            0xE9, 0x00, 0x00, 0x00, 0x00};
	unsigned char * pdst = (unsigned char *) dst;
	unsigned char * psrc = (unsigned char *) src;
	unsigned long _next_opcode_va = ((unsigned long)psrc + 5 + _code_push_rva + _image_base_offset);
	if(*psrc == 0x68)
	{
		/*mov     dword ptr [esp], xxxxxxxx*/
		memmove(push + 4,psrc + 1,sizeof(unsigned long));
		int index = clock() % 9;
		memset(push,_rand_reg[index],sizeof(unsigned char));
		/*jmp     xxxxxxxx*/
		memset(psrc,0xE9,sizeof(unsigned char));
	    *(unsigned long *) (psrc + 1)  = (unsigned long) (pdst + _new_push_rva + _image_base_offset - _next_opcode_va);
		/*jmp     xxxxxxxx*/
		*(unsigned long *)(push + 9) = (_next_opcode_va - ((unsigned long)pdst + 8 + _new_push_rva  + _image_base_offset + 5));
		memmove(pdst,push,0xD);
	}
}


变形call [xxxxxxxx] /*FF15 XXXXXXXX*/
void _link_call_mem(void * dst,size_t _new_push_rva,void * src,size_t _code_push_rva,size_t _image_base_offset)
{
	unsigned long _next_address = 0;
	unsigned char _rand_reg[] = {0x50, 0x51, 0x52, 0x53, 0x53, 0x54, 0x55, 0x56, 0x57};
	unsigned char call[] = {0x50, 
		                    0xC7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00, 
		                    0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, 
		                    0xE9, 0x00, 0x00, 0x00, 0x00};
	unsigned char * pdst = (unsigned char *) dst;
	unsigned char * psrc = (unsigned char *) src;
	unsigned long _next_opcode_va = ((unsigned long)psrc + 5 + _code_push_rva + _image_base_offset);

	if(*(unsigned short *)psrc == 0x15ff)
	{
		Sleep(1);
		int index = rand() % 9;
		memset(call,_rand_reg[index],sizeof(unsigned char));
		memmove(call + 10,psrc + 2,sizeof(unsigned long));

		memset(psrc,0xE9,sizeof(unsigned char));
		*(unsigned long *) (psrc + 1)  = (unsigned long) (pdst + _new_push_rva + _image_base_offset - _next_opcode_va);
		memset(psrc + 5,0x90,sizeof(unsigned char));
		/*mov     dword ptr [esp], -1*/
		_next_address = (unsigned long) ((unsigned long)pdst + 14 + _new_push_rva  + _image_base_offset);
		memmove((void *)(call + 4),(void *)&_next_address,sizeof(unsigned long));
		_next_address = (unsigned long) (_next_opcode_va - ((unsigned long)pdst + 14 + _new_push_rva  + _image_base_offset + 5));
		memmove((void *)(call + 15),(void *)&_next_address,sizeof(unsigned long));
		memmove(pdst,call,19);
	}
}

变形 call [xxxxxxxx]./*call......jmp*/
void _link_call_rva(void * dst,size_t _new_push_rva,void * src,size_t _code_push_rva,size_t _image_base_offset)
{
	/*
	    jmp     _new_adderss
        push    exx
	    mov     dword ptr [esp], -1
        jmp     dword ptr[xxxxxxxx]
_next:
        jmp     _ret_address
	*/
	unsigned char _rand_reg[] = {0x50, 0x51, 0x52, 0x53, 0x53, 0x54, 0x55, 0x56, 0x57};
	unsigned char call[] = {0x50, 
		                    0xC7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00,
		                    0xE9, 0x00, 0x00, 0x00, 0x00, 
                            0xE9, 0x00, 0x00, 0x00, 0x00};
	unsigned char * pdst = (unsigned char *) dst;
	unsigned char * psrc = (unsigned char *) src;
	unsigned long _next_opcode_va = ((unsigned long)psrc + 5 + _code_push_rva + _image_base_offset);
	if(*psrc == 0xE8 && *(unsigned short *)(psrc + 3) == 0)
	{
		/*jmp     _new_adderss*/
		unsigned long _jmp_va = ((unsigned long)psrc + (*(unsigned long*)(psrc + 1)) + 6 + _code_push_rva  + _image_base_offset); 
		memset(psrc,0xE9,sizeof(unsigned char));
		*(unsigned long *) (psrc + 1)  = (unsigned long) (pdst + _new_push_rva + _image_base_offset - _next_opcode_va);
		/*push    exx*/
		Sleep(1);
		int index = clock() % 9;
		memset(call,_rand_reg[index],sizeof(unsigned char));
		/*mov     dword ptr [esp], -1*/
		unsigned long _ret_address = (unsigned long) ((unsigned long)pdst + 13 + _new_push_rva  + _image_base_offset);
		memmove((void *)(call + 4),(void *)&_ret_address,sizeof(unsigned long));
		/*jmp     dword ptr[xxxxxxxx]*/
		_jmp_va -= ((unsigned long)pdst + 9 + _new_push_rva  + _image_base_offset + 5);
		memmove((void *)(call + 9),(void *)&_jmp_va,sizeof(unsigned long));
		/*jmp     _ret_address*/
		*(unsigned long *)(call + 14) = (_next_opcode_va - ((unsigned long)pdst + 13 + _new_push_rva  + _image_base_offset + 5));
		memmove(pdst,call,18);
	}
}

主函数
unsigned long _push(unsigned char * imagebase)
{
	PIMAGE_DOS_HEADER         _dos_header = NULL;
	PIMAGE_NT_HEADERS32       _nt32_header = NULL; 
	PIMAGE_FILE_HEADER        _file_header = NULL;
	PIMAGE_OPTIONAL_HEADER32  _optional32_header = NULL;
	PIMAGE_SECTION_HEADER     _section_header = NULL;
	unsigned long _push_length = 0;
	if(*(unsigned short *) imagebase == IMAGE_DOS_SIGNATURE)
	{
		_dos_header = (PIMAGE_DOS_HEADER) imagebase;
		unsigned char * _uchar_base = imagebase;
		for(char index = 0; index < 0x200; index++)
		{
			if(*(unsigned long *)_uchar_base == IMAGE_NT_SIGNATURE)
			{
				_dos_header->e_lfanew = _uchar_base - imagebase;
				_nt32_header = (PIMAGE_NT_HEADERS32) _uchar_base;
				_file_header = (PIMAGE_FILE_HEADER) &_nt32_header->FileHeader;
				_optional32_header = (PIMAGE_OPTIONAL_HEADER32) &_nt32_header->OptionalHeader;
                _section_header = (PIMAGE_SECTION_HEADER)((unsigned long)_nt32_header + 
					sizeof(IMAGE_OPTIONAL_HEADER32) + sizeof(IMAGE_FILE_HEADER) + sizeof(unsigned long));
				break;
			}
			_uchar_base++;
		}
		if(NULL != _nt32_header)
		{
			unsigned long _min_image_base = _optional32_header->ImageBase;
			unsigned long _max_image_base = _optional32_header->ImageBase + _optional32_header->SizeOfImage;
			unsigned long _section_number = _file_header->NumberOfSections;
			_section_header[_section_number - 1].Characteristics = 0xE0000080;
            unsigned char * _new_push_code = _section_header[_section_number - 1].PointerToRawData + imagebase;
			unsigned long   _new_push_offset = _section_header[_section_number - 1].VirtualAddress - _section_header[_section_number - 1].PointerToRawData;
			for(int i = 0; i < _section_number - 1; i++)
			{
			    unsigned long _section_raw = _section_header[i].PointerToRawData;
				unsigned long _section_raw_sze = _section_header[i].SizeOfRawData;
				unsigned long _section_offset = _section_header[i].VirtualAddress - _section_raw;
				unsigned char * _section_code = (unsigned char *) (imagebase + _section_raw);
                if(IMAGE_SCN_MEM_EXECUTE&_section_header[i].Characteristics)
			    {
				    for(int _index_opcode = 0; _index_opcode < _section_raw_sze; _index_opcode++)
					{
						unsigned long _is_data_address = *(unsigned long *)(_section_code + 1);
						if(*(unsigned char *)_section_code == 0xE8
							&& *(unsigned short *)(_section_code + 3) == 0 && *(unsigned short *)(_section_code + 1) != 0)
						{
							_link_call_rva(_new_push_code,_new_push_offset,_section_code,_section_offset,(_min_image_base - (unsigned long)imagebase));
							_new_push_code += 18;
							_push_length += 18;
							_section_code += 5;
						}
						else if(*(unsigned short *)_section_code == 0x15ff && (*(unsigned long *)(_section_code + 2)) > _min_image_base 
							&& (*(unsigned long *)(_section_code + 2)) < _max_image_base)
						{
							_link_call_mem(_new_push_code,_new_push_offset,_section_code,_section_offset,(_min_image_base - (unsigned long)imagebase));
							_new_push_code += 19;
							_push_length += 19;
							_section_code += 6;
						}
						else if(*(unsigned char *)_section_code == 0x68 && _is_data_address > _min_image_base 
							&& _is_data_address < _max_image_base)
						{
							_link_push_imm(_new_push_code,_new_push_offset,_section_code,_section_offset,(_min_image_base - (unsigned long)imagebase));
							_new_push_code += 0xD;
							_push_length += 0xD;
							_section_code += 5;
						}
						else
							++_section_code;
					}
				}
			}
			if(_push_length)
			{
                _section_header[_section_number - 1].SizeOfRawData = alignment(_push_length,_optional32_header->FileAlignment);
		        _section_header[_section_number - 1].Misc.VirtualSize = alignment(_push_length,_optional32_header->SectionAlignment);
		        unsigned long _new_image_offset = _optional32_header->SizeOfImage -_section_header[_section_number - 1].VirtualAddress;
			    _optional32_header->SizeOfImage -= _new_image_offset;
			    _optional32_header->SizeOfImage += alignment(_push_length,_optional32_header->SectionAlignment);
			}
		}
	}
	return _push_length;
}

文件映射,直接写文件.
void * createmap(char * path)
{
	HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile==INVALID_HANDLE_VALUE)
	{
		MessageBox(GetActiveWindow(),TEXT("不能打开文件"),TEXT("提示"),MB_ICONSTOP);
		return NULL;
	}
	HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 0, NULL);
	if(NULL ==hMap)
	{
		MessageBox(GetActiveWindow(),TEXT("创建文件映射失败"),TEXT("提示"),MB_ICONSTOP);
		return NULL;
	}
	return MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
}

用法
_push((unsigned char *)createmap("test.exe"));

[课程]Android-CTF解题方法汇总!

收藏
免费 7
支持
分享
最新回复 (11)
雪    币: 1844
活跃值: (35)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
2
嗯,还真的要和反汇编引擎配合一下的,做出来的效果可以复杂很多
2012-9-4 12:55
0
雪    币: 287
活跃值: (578)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
3
无法学习,只能膜拜
2012-9-4 13:22
0
雪    币: 27
活跃值: (90)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
4
2012-9-4 16:25
0
雪    币: 19
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
过来围观lz,看来lz最近在研究代码变形~
2012-9-4 17:26
0
雪    币: 1905
活跃值: (1537)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
6
VM解析贴一出,各种代码变形随之而出。
2012-9-5 12:37
0
雪    币: 4902
活跃值: (110)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
速度赶来学习了
2012-9-5 12:54
0
雪    币: 142
活跃值: (310)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
8
楼主竟然是个妹纸,高手啊,求联系方式。
2012-9-6 09:19
0
雪    币: 120
活跃值: (160)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
楼主还木有男盆友。。你得抓紧了。。。我看过PP的,一个字:“灰常漂亮”
2012-9-6 09:44
0
雪    币: 287
活跃值: (578)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
10
我这有LZMM的QQ,我便宜卖你,要不要啊
2012-9-6 10:38
0
雪    币: 1689
活跃值: (379)
能力值: ( LV15,RANK:440 )
在线值:
发帖
回帖
粉丝
11
她终于答应作我女朋友了.幸运。
2012-9-7 08:52
0
雪    币: 8865
活跃值: (2379)
能力值: ( LV12,RANK:760 )
在线值:
发帖
回帖
粉丝
12
都是这些坏淫~
2012-9-8 19:46
0
游客
登录 | 注册 方可回帖
返回
//