forked from gentilkiwi/mimikatz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkull_m_file.c
205 lines (187 loc) · 6.3 KB
/
kull_m_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Benjamin DELPY `gentilkiwi`
https://blog.gentilkiwi.com
Licence : https://creativecommons.org/licenses/by/4.0/
*/
#include "kull_m_file.h"
BOOL isBase64InterceptOutput = FALSE, isBase64InterceptInput = FALSE;
BOOL kull_m_file_getCurrentDirectory(wchar_t ** ppDirName)
{
BOOL reussite = FALSE;
DWORD tailleRequise = GetCurrentDirectory(0, NULL);
if(*ppDirName = (wchar_t *) LocalAlloc(LPTR, tailleRequise * sizeof(wchar_t)))
if(!(reussite = (tailleRequise > 0 && (GetCurrentDirectory(tailleRequise, *ppDirName) == tailleRequise - 1))))
LocalFree(*ppDirName);
return reussite;
}
BOOL kull_m_file_getAbsolutePathOf(PCWCHAR thisData, wchar_t ** reponse)
{
BOOL reussite = FALSE;
wchar_t *monRep;
*reponse = (wchar_t *) LocalAlloc(LPTR, MAX_PATH * sizeof(wchar_t));
if(PathIsRelative(thisData))
{
if(kull_m_file_getCurrentDirectory(&monRep))
{
reussite = (PathCombine(*reponse , monRep, thisData) != NULL);
LocalFree(monRep);
}
}
else reussite = PathCanonicalize(*reponse, thisData);
if(!reussite)
LocalFree(*reponse);
return reussite;
}
BOOL kull_m_file_isFileExist(PCWCHAR fileName)
{
BOOL reussite = FALSE;
HANDLE hFile = NULL;
reussite = ((hFile = CreateFile(fileName, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) && hFile != INVALID_HANDLE_VALUE);
if(reussite)
CloseHandle(hFile);
return reussite;
}
BOOL kull_m_file_writeData(PCWCHAR fileName, LPCVOID data, DWORD lenght)
{
BOOL reussite = FALSE;
DWORD dwBytesWritten = 0, i;
HANDLE hFile = NULL;
LPWSTR base64;
if(isBase64InterceptOutput)
{
if(CryptBinaryToString((const BYTE *) data, lenght, CRYPT_STRING_BASE64, NULL, &dwBytesWritten))
{
if(base64 = (LPWSTR) LocalAlloc(LPTR, dwBytesWritten * sizeof(wchar_t)))
{
if(reussite = CryptBinaryToString((const BYTE *) data, lenght, CRYPT_STRING_BASE64, base64, &dwBytesWritten))
{
kprintf(L"\n====================\nBase64 of file : %s\n====================\n", fileName);
for(i = 0; i < dwBytesWritten; i++)
kprintf(L"%c", base64[i]);
kprintf(L"====================\n");
}
LocalFree(base64);
}
}
}
else if((hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) && hFile != INVALID_HANDLE_VALUE)
{
if(WriteFile(hFile, data, lenght, &dwBytesWritten, NULL) && (lenght == dwBytesWritten))
reussite = FlushFileBuffers(hFile);
CloseHandle(hFile);
}
return reussite;
}
BOOL kull_m_file_readData(PCWCHAR fileName, PBYTE * data, PDWORD lenght) // for ""little"" files !
{
return kull_m_file_readGeneric(fileName, data, lenght, 0);
}
BOOL kull_m_file_readGeneric(PCWCHAR fileName, PBYTE * data, PDWORD lenght, DWORD flags)
{
BOOL reussite = FALSE;
DWORD dwBytesReaded;
LARGE_INTEGER filesize;
HANDLE hFile = NULL;
if(isBase64InterceptInput)
{
if(!(reussite = kull_m_string_quick_base64_to_Binary(fileName, data, lenght)))
PRINT_ERROR_AUTO(L"kull_m_string_quick_base64_to_Binary");
}
else if((hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, flags, NULL)) && hFile != INVALID_HANDLE_VALUE)
{
if(GetFileSizeEx(hFile, &filesize) && !filesize.HighPart)
{
*lenght = filesize.LowPart;
if(*data = (PBYTE) LocalAlloc(LPTR, *lenght))
{
if(!(reussite = ReadFile(hFile, *data, *lenght, &dwBytesReaded, NULL) && (*lenght == dwBytesReaded)))
LocalFree(*data);
}
}
CloseHandle(hFile);
}
return reussite;
}
const wchar_t kull_m_file_forbiddenChars[] = {L'\\', L'/', L':', L'*', L'?', L'\"', L'<', L'>', L'|'};
void kull_m_file_cleanFilename(PWCHAR fileName)
{
DWORD i, j;
for(i = 0; fileName[i]; i++)
for(j = 0; j < ARRAYSIZE(kull_m_file_forbiddenChars); j++)
if(fileName[i] == kull_m_file_forbiddenChars[j])
fileName[i] = L'~';
}
PWCHAR kull_m_file_fullPath(PCWCHAR fileName)
{
PWCHAR buffer = NULL;
DWORD bufferLen;
if(fileName)
if(bufferLen = ExpandEnvironmentStrings(fileName, NULL, 0))
if(buffer = (PWCHAR) LocalAlloc(LPTR, bufferLen * sizeof(wchar_t)))
if(bufferLen != ExpandEnvironmentStrings(fileName, buffer, bufferLen))
buffer = (PWCHAR) LocalFree(buffer);
return buffer;
}
BOOL kull_m_file_Find(PCWCHAR directory, PCWCHAR filter, BOOL isRecursive /*TODO*/, DWORD level, BOOL isPrintInfos, BOOL isWithDir, PKULL_M_FILE_FIND_CALLBACK callback, PVOID pvArg)
{
BOOL status = FALSE;
DWORD dwAttrib;
HANDLE hFind;
WIN32_FIND_DATA fData;
PWCHAR fullpath;
dwAttrib = GetFileAttributes(directory);
if((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
{
if(isPrintInfos && !level)
{
kprintf(L"%*s" L"Directory \'%s\'", level << 1, L"", directory);
if(filter)
kprintf(L" (%s)", filter);
kprintf(L"\n");
}
if(fullpath = (wchar_t *) LocalAlloc(LPTR, MAX_PATH * sizeof(wchar_t)))
{
if(wcscpy_s(fullpath, MAX_PATH, directory) == 0)
{
if(wcscat_s(fullpath, MAX_PATH, L"\\") == 0)
{
if(wcscat_s(fullpath, MAX_PATH, filter ? filter : L"*") == 0)
{
hFind = FindFirstFile(fullpath, &fData);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
if(_wcsicmp(fData.cFileName, L".") && _wcsicmp(fData.cFileName, L".."))
{
if(wcscpy_s(fullpath, MAX_PATH, directory) == 0)
{
if(wcscat_s(fullpath, MAX_PATH, L"\\") == 0)
{
dwAttrib = (DWORD) wcslen(fullpath);
if(wcscat_s(fullpath, MAX_PATH, fData.cFileName) == 0)
{
if(isPrintInfos)
kprintf(L"%*s" L"%3u %c|'%s\'\n", level << 1, L"", level, (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? L'D' : L'F' , fData.cFileName);
if(isWithDir || !(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if(callback)
status = callback(level, fullpath, fullpath + dwAttrib, pvArg);
}
else if(isRecursive && fData.cFileName)
status = kull_m_file_Find(fullpath, filter, TRUE, level + 1, isPrintInfos, isWithDir, callback, pvArg);
}
}
}
}
} while(!status && FindNextFile(hFind, &fData));
FindClose(hFind);
}
}
}
}
LocalFree(fullpath);
}
}
return status;
}