forked from gentilkiwi/mimikatz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kull_m_output.c
114 lines (106 loc) · 2.74 KB
/
kull_m_output.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
/* Benjamin DELPY `gentilkiwi`
http://blog.gentilkiwi.com
Licence : https://creativecommons.org/licenses/by/4.0/
*/
#include "kull_m_output.h"
FILE * logfile = NULL;
wchar_t * outputBuffer = NULL;
size_t outputBufferElements = 0, outputBufferElementsPosition = 0;
void kprintf(PCWCHAR format, ...)
{
int varBuf;
size_t tempSize;
wchar_t * tmpBuffer;
va_list args;
va_start(args, format);
if(outputBuffer)
{
varBuf = _vscwprintf(format, args);
if(varBuf > 0)
{
if((size_t) varBuf > (outputBufferElements - outputBufferElementsPosition - 1)) // NULL character
{
tempSize = (outputBufferElements + varBuf + 1) * 2; // * 2, just to be cool
if(tmpBuffer = (wchar_t *) LocalAlloc(LPTR, tempSize * sizeof(wchar_t)))
{
RtlCopyMemory(tmpBuffer, outputBuffer, outputBufferElementsPosition * sizeof(wchar_t));
LocalFree(outputBuffer);
outputBuffer = tmpBuffer;
outputBufferElements = tempSize;
}
else wprintf(L"Erreur LocalAlloc: %u\n", GetLastError());
//if(outputBuffer = (wchar_t *) LocalReAlloc(outputBuffer, tempSize * sizeof(wchar_t), LPTR))
// outputBufferElements = tempSize;
//else wprintf(L"Erreur ReAlloc: %u\n", GetLastError());
}
varBuf = vswprintf_s(outputBuffer + outputBufferElementsPosition, outputBufferElements - outputBufferElementsPosition, format, args);
if(varBuf > 0)
outputBufferElementsPosition += varBuf;
}
}
#ifndef _POWERKATZ
else
{
vwprintf(format, args);
fflush(stdout);
}
#endif
if(logfile)
{
vfwprintf(logfile, format, args);
fflush(logfile);
}
va_end(args);
}
void kprintf_inputline(PCWCHAR format, ...)
{
va_list args;
va_start(args, format);
if(logfile)
{
vfwprintf(logfile, format, args);
fflush(logfile);
}
va_end(args);
}
BOOL kull_m_output_file(PCWCHAR file)
{
BOOL status = FALSE;
FILE * newlog = NULL;
if(file)
#pragma warning(push)
#pragma warning(disable:4996)
newlog = _wfopen(file, L"a"); // XP does not like _wfopen_s
#pragma warning(pop)
if(newlog || !file)
{
if(logfile)
fclose(logfile);
logfile = newlog;
}
return (!file || (file && logfile));
}
int previousStdOut, previousStdErr;
UINT previousConsoleOutput;
void kull_m_output_init()
{
#ifndef _POWERKATZ
#ifndef _WINDLL
previousStdOut = _setmode(_fileno(stdout), _O_U8TEXT);
previousStdErr = _setmode(_fileno(stderr), _O_U8TEXT);
#endif
previousConsoleOutput = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
#endif
}
void kull_m_output_clean()
{
#ifndef _POWERKATZ
#ifndef _WINDLL
_setmode(_fileno(stdout), previousStdOut);
_setmode(_fileno(stderr), previousStdErr);
#endif
SetConsoleOutputCP(previousConsoleOutput);
#endif
}