SV.PCC.MODIFIED_BEFORE_CREATE

不安全的临时文件名修改

如果以不安全的方式创建或使用临时文件,应用程序和系统数据可能受到攻击。危险数据可能注入应用程序中,或存储在临时文件中的数据可能被访问、修改或破坏。如果在调用 CreateFile 前更改了临时文件名缓冲区,SV.PCC.MODIFIED_BEFORE_CREATE 检查器将标记此情况。

漏洞与风险

临时文件漏洞是一个十分常见的安全问题。Mitre Corp. 安全警报数据库针对此类型的安全问题列出了 200 多个报告。临时文件漏洞可用于升级权限或使用重要信息。

缓解与预防

要避免该漏洞:

  • 使用 GetTempPath 和 GetTempFileName 确保随机创建名称
  • 检查临时文件的最低权限设置

修正代码示例

复制
     //  获取临时路径 env 字符串(不保证它是有效路径)。
    dwRetVal = GetTempPath(MAX_PATH,          // length of the buffer
                           lpTempPathBuffer); // buffer for path 
    if (dwRetVal > MAX_PATH || (dwRetVal == 0))
    {
        PrintError(TEXT("GetTempPath failed"));
        if (!CloseHandle(hFile))
        {
            PrintError(TEXT("CloseHandle(hFile) failed"));
           return (7);
       }
       return (2);
   }
   //  Generates a temporary file name. 
   uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files
                             TEXT("DEMO"),     // temp file name prefix 
                             0,                // create unique name 
                             szTempFileName);  // buffer for name 
   if (uRetVal == 0)
   {
       PrintError(TEXT("GetTempFileName failed"));
       if (!CloseHandle(hFile))
       {
           PrintError(TEXT("CloseHandle(hFile) failed"));
           return (7);
       }
       return (3);
   }
   //  Creates the new file to write to for the upper-case version.
   hTempFile = CreateFile((LPTSTR) szTempFileName, // file name 
                          GENERIC_WRITE,        // open for write 
                          0,                    // do not share 
                          NULL,                 // default security 
                          CREATE_ALWAYS,        // overwrite existing
                          FILE_ATTRIBUTE_TEMPORARY, // temporary storage 
                          NULL);                // no template 
   if (hTempFile == INVALID_HANDLE_VALUE) 
   { 
       PrintError(TEXT("Second CreateFile failed"));
       if (!CloseHandle(hFile))
       {
           PrintError(TEXT("CloseHandle(hFile) failed"));
           return (7);
       }
       return (4);
   } 

此代码示例很好地遵循了临时文件的创建规则。通过使用安全的临时文件名,可大幅降低应用程序或系统数据受到攻击的风险。

安全培训

应用程序安全培训材料由 Secure Code Warrior 提供。