Framework工程师必备操作:C++ open()和read()函数使用

对于Framework工程师来说,必要C或者C++编程能力是必须的,像对设备节点的操作是最基本的操作,那么我们便会用到open和read函数。open()函数用于打开文件,而read()函数用于从打开的文件中读取数据。

1. open() 函数

open()函数是C/C++标准库中的一个POSIX标准函数,用于打开一个文件并返回一个文件描述符(File Descriptor),以供后续的读写操作。其函数声明如下:

#include <fcntl.h>
int open(const char* path, int flags, mode_t mode);
  • path:要打开或创建的目标文件名。
  • flags:对文件进行多种操作的模式,如O_RDONLY(只读打开)、O_WRONLY(只写打开)、O_RDWR(读写打开)、O_CREAT(若文件不存在,创建文件)等。
  • mode:新文件的访问权限,通常使用权限掩码如S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH。
    如果打开文件成功,open()返回一个文件描述符;如果失败,返回-1,并设置errno以指示错误原因‌12。

2. read() 函数

read()函数用于从打开的文件描述符中读取数据。其函数声明如下:

#include <unistd.h>
ssize_t read(int fd, void* buf, size_t count);
  • fd:文件描述符,由open()函数返回。
  • buf:指向用于存储读取数据的缓冲区的指针。
  • count:要读取的字节数。
    如果读取成功,read()返回实际读取的字节数;如果遇到文件结束或发生错误,返回-1,并设置errno以指示错误原因‌23。
    示例代码
    以下是一个简单的示例,展示如何使用open()和read()函数读取文件:
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    const char* filePath = "input.txt";
    int fd = open(filePath, O_RDONLY); // 以只读模式打开文件
    if (fd < 0) {
        perror("Failed to open file");
        return -1;
    }
    char buffer[1024]; // 定义一个缓冲区用于存储读取的数据
    ssize_t bytesRead = read(fd, buffer, sizeof(buffer)); // 从文件中读取数据到缓冲区
    if (bytesRead < 0) { // 如果读取失败或遇到文件结束符EOF
        perror("Failed to read file");
        close(fd); // 关闭文件描述符
        return -1;
    }
    printf("Read %ld bytes from file: %s\n", bytesRead, buffer); // 打印读取的内容和字节数
    close(fd); // 关闭文件描述符
    return 0;
}
注入DLL通常需要在目标进程中创建一个远程线程,然后在远程线程中调用LoadLibrary函数加载DLL,最后调用DLL中的导出函数。以下是一个简单的C++注入DLL的示例代码: ``` #include <Windows.h> #include <TlHelp32.h> #include <iostream> int main() { // 获取目标进程的句柄 DWORD processId = 1234; // 目标进程ID HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId); if (processHandle == NULL) { std::cout << "Failed to open process" << std::endl; return 1; } // 获取LoadLibrary函数的地址 LPVOID loadLibraryAddress = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (loadLibraryAddress == NULL) { std::cout << "Failed to get LoadLibrary address" << std::endl; return 1; } // 分配内存 LPVOID remoteMemory = VirtualAllocEx(processHandle, NULL, MAX_PATH, MEM_COMMIT, PAGE_READWRITE); if (remoteMemory == NULL) { std::cout << "Failed to allocate remote memory" << std::endl; return 1; } // 写入DLL路径到目标进程中 const char* dllPath = "C:\\path\\to\\your\\dll.dll"; if (!WriteProcessMemory(processHandle, remoteMemory, dllPath, strlen(dllPath) + 1, NULL)) { std::cout << "Failed to write DLL path to remote memory" << std::endl; return 1; } // 创建远程线程并调用LoadLibrary函数 HANDLE remoteThread = CreateRemoteThread(processHandle, NULL, 0, (LPTHREAD_START_ROUTINE)loadLibraryAddress, remoteMemory, 0, NULL); if (remoteThread == NULL) { std::cout << "Failed to create remote thread" << std::endl; return 1; } // 等待远程线程结束 WaitForSingleObject(remoteThread, INFINITE); // 获取导出函数地址并调用 const char* functionName = "YourExportFunction"; FARPROC functionAddress = GetProcAddress(GetModuleHandle("yourdll.dll"), functionName); if (functionAddress == NULL) { std::cout << "Failed to get function address" << std::endl; return 1; } // 调用导出函数 typedef int (*YourExportFunction)(); YourExportFunction yourFunction = (YourExportFunction)functionAddress; int result = yourFunction(); std::cout << "Result: " << result << std::endl; // 关闭句柄 CloseHandle(remoteThread); VirtualFreeEx(processHandle, remoteMemory, 0, MEM_RELEASE); CloseHandle(processHandle); return 0; } ``` 在上面的示例中,我们首先使用OpenProcess函数打开目标进程句柄,然后使用GetProcAddress函数获取LoadLibrary函数的地址。接下来,我们使用VirtualAllocEx函数在目标进程中分配内存,并使用WriteProcessMemory函数将DLL路径写入目标进程中。然后,我们使用CreateRemoteThread函数创建远程线程,并在远程线程中调用LoadLibrary函数来加载DLL。最后,我们使用GetProcAddress函数获取导出函数地址,并调用导出函数。 需要注意的是,在实际的注入过程中,你还需要处理一些异常情况,例如目标进程拥有较高的权限,或者DLL文件不存在等等。此外,在使用CreateRemoteThread函数创建远程线程时,你还需要指定一个适当的起始地址,并在远程线程中调用ExitThread函数结束远程线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值