自编程序伪造文件创建时间、修改时间、访问时间
作者:Tuuzed(土仔)
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明。
http://www.cppblog.com/tuuzed/archive/2008/02/16/42816.html
一个文件从建立开始,就有了创建时间、修改时间和访问时间。可是,有时为了不让别人一眼看出文件已经给修改了,我们需要修改文件的创建时间和修改时间,网上的工具很多,评价比较好的是一款叫touchpro的软件,据说是已经内嵌在Explorer。商业软件嘛,要么破解,要么购买。不如自己来做一个吧。
查查VS2005自带的MSDN Library,发现了两个关键的API函数(在MSDN“查找”栏里输入关键词“file time”,一切都明白了):GetFileTime、SetFileTime,还有需要了解两个文件时间结构:FILETIME、SYSTEMTIME。
GetFileTime:获取文件的创建时间、修改时间和访问时间。
SetFileTime:设置文件的创建时间、修改时间和访问时间。
FILETIME:MSDN上说就是记录从1601年1月1日(UTC)到目前所间隔的时间,是一个64位的值。
SYSTEMTIME:也是记录时间,这个时间格式接近我们日常的时间格式,有年月日等。
获取文件时间:
计算机是利用FILETIME结构来记录文件的创建时间,如果要变为容易识别的时间格式SYSTEMTIME,则需要用到FileTimeToSystemTime函数。这个时候得到的SYSTEMTIME还不是我们当前计算机的时间,而是UTC时间。当地的时间,需要用SystemTimeToTzSpecificLocalTime函数得到。
修改文件时间:
已经知道如何获取文件的时间了,那么接下来修改就好办了。自己构造一个SYSTEMTIME结构的时间,然后用TzSpecificLocalTimeToSystemTime转成UTC格式的SYSTEMTIME,最后用SystemTimeToFileTime转成FILETIME格式。
自己的程序SFT.EXE(鼠标右键另存):
ShowDateTime:显示文件的创建、修改、访问时间。
SetDateTime:设置文件的创建、修改、访问时间。
ParseTime:将时间格式化显示。
FileTimeToLocalSystemTime、LocalSystemTimeToFileTime:将一些API函数直接串接在一起。
1
int ShowDateTime(HANDLE hFile)
2

{
3
if (hFile==NULL) return 0;
4
5
FILETIME ftCreTime, ftAccTime, ftWriTime;
6
//get file time
7
8
if(GetFileTime(hFile, &ftCreTime,&ftAccTime,&ftWriTime))
9
{
10
SYSTEMTIME stLOCCreTime, stLOCAccTime, stLOCWriTime;
11
12
FileTimeToLocalSystemTime(&ftCreTime, &stLOCCreTime);
13
FileTimeToLocalSystemTime(&ftAccTime, &stLOCAccTime);
14
FileTimeToLocalSystemTime(&ftWriTime, &stLOCWriTime);
15
16
//Build a string to show time and date.
17
char CreTime[MAX_DATETIME];
18
char AccTime[MAX_DATETIME];
19
char WriTime[MAX_DATETIME];
20
ParseTime(&stLOCCreTime, CreTime);
21
ParseTime(&stLOCAccTime, AccTime);
22
ParseTime(&stLOCWriTime, WriTime);
23
24
//print result
25
cout << "====================================="<<endl;
26
cout << "CreationTime: " << CreTime << endl;
27
cout << "AccessTime: " << AccTime << endl;
28
cout << "WriteTime: " << WriTime << endl;
29
return 1;
30
}
31
else
32
{
33
return 0;
34
}
35
36
}
37
38
int SetDateTime(HANDLE hFile, char *pDate, char *pTime)
39

{
40
int result=0;
41
if (hFile==NULL || (pDate==NULL && pTime==NULL)) return result;
42
43
FILETIME ftCreTime, ftAccTime, ftWriTime;
44
result=GetFileTime(hFile, &ftCreTime,&ftAccTime,&ftWriTime);
45
if (result==0)
46
return result;
47
48
SYSTEMTIME stLOCCreTime, stLOCAccTime, stLOCWriTime;
49
FileTimeToLocalSystemTime(&ftCreTime, &stLOCCreTime);
50
FileTimeToLocalSystemTime(&ftAccTime, &stLOCAccTime);
51
FileTimeToLocalSystemTime(&ftWriTime, &stLOCWriTime);
52
53
int wYear=2008, wMonth=2, wDay=11, wHour=0, wMinute=0, wSecond=0;
54
if (pDate!=NULL)
55
{
56
sscanf(pDate, "%d-%d-%d", &wYear, &wMonth, &wDay);
57
#ifdef _DEBUG
58
cout<<"Year:"<<wYear<<"Month:"<<wMonth<<"Day:"<<wDay<<endl;
59
#endif
60
stLOCCreTime.wYear=wYear;
61
stLOCCreTime.wMonth=wMonth;
62
stLOCCreTime.wDay=wDay;
63
stLOCAccTime.wYear=wYear;
64
stLOCAccTime.wMonth=wMonth;
65
stLOCAccTime.wDay=wDay;
66
stLOCWriTime.wYear=wYear;
67
stLOCWriTime.wMonth=wMonth;
68
stLOCWriTime.wDay=wDay;
69
}
70
71
if (pTime!=NULL)
72
{
73
sscanf(pTime, "%d:%d:%d", &wHour, &wMinute, &wSecond);
74
#ifdef _DEBUG
75
cout<<"Hour:"<<wHour<<"Minute:"<<wMinute<<"Second:"<<wSecond<<endl;
76
#endif
77
stLOCCreTime.wHour=wHour;
78
stLOCCreTime.wMinute=wMinute;
79
stLOCCreTime.wSecond=wSecond;
80
stLOCAccTime.wHour=wHour;
81
stLOCAccTime.wMinute=wMinute;
82
stLOCAccTime.wSecond=wSecond;
83
stLOCWriTime.wHour=wHour;
84
stLOCWriTime.wMinute=wMinute;
85
stLOCWriTime.wSecond=wSecond;
86
}
87
88
result=LocalSystemTimeToFileTime(&stLOCCreTime, &ftCreTime);
89
if (result==0) return result;
90
LocalSystemTimeToFileTime(&stLOCAccTime, &ftAccTime);
91
if (result==0) return result;
92
LocalSystemTimeToFileTime(&stLOCWriTime, &ftWriTime);
93
if (result==0) return result;
94
95
result=SetFileTime(hFile, &ftCreTime, &ftAccTime, &ftWriTime);
96
if (result==0) return result;
97
return result;
98
}
99
100
void ParseTime(SYSTEMTIME *in_st, char *out_string)
101

{
102
char *DayOfWeek[]=
{"Sun", "Mon", "Tue","Wed","Thu","Fri","Sat"};
103
wsprintf(out_string, "%d-%d-%d %s %d:%d:%d", in_st->wYear, in_st->wMonth, in_st->wDay, \
104
DayOfWeek[in_st->wDayOfWeek], \
105
in_st->wHour, in_st->wMinute, in_st->wSecond);
106
}
107
108
int FileTimeToLocalSystemTime(const FILETIME *pFileTime , SYSTEMTIME *pLocSystemTime)
109

{
110
int result=0;
111
SYSTEMTIME tmpSystemTime;
112
result=FileTimeToSystemTime(pFileTime, &tmpSystemTime);
113
if (result!=0)
{
114
result=SystemTimeToTzSpecificLocalTime(NULL,&tmpSystemTime, pLocSystemTime);
115
return result;
116
}
117
else
118
return result;
119
}
120
121
int LocalSystemTimeToFileTime(const SYSTEMTIME *pLocSystemTime, FILETIME *pFileTime)
122

{
123
int result=0;
124
FILETIME tmpFileTime;
125
result=SystemTimeToFileTime(pLocSystemTime, &tmpFileTime);
126
if (result!=0)
{
127
result=LocalFileTimeToFileTime(&tmpFileTime, pFileTime);
128
return result;
129
}
130
else
131
return result;
132
}
133
UTC:协调世界时(Universal Time Coordinated),过去曾用格林威治平均时(GMT)来表示。UTC is the high-precision atomic clock version of GMT (Greenwich Mean Time).