2015-08-25 09:19:01 +00:00
|
|
|
/* Benjamin DELPY `gentilkiwi`
|
2020-09-17 01:17:11 +00:00
|
|
|
https://blog.gentilkiwi.com
|
2015-08-25 09:19:01 +00:00
|
|
|
benjamin@gentilkiwi.com
|
|
|
|
Licence : https://creativecommons.org/licenses/by/4.0/
|
|
|
|
*/
|
2014-04-06 18:31:53 +00:00
|
|
|
#include "kull_m_output.h"
|
|
|
|
|
|
|
|
FILE * logfile = NULL;
|
2019-03-25 00:57:56 +00:00
|
|
|
#if !defined(MIMIKATZ_W2000_SUPPORT)
|
2014-05-04 23:24:54 +00:00
|
|
|
wchar_t * outputBuffer = NULL;
|
|
|
|
size_t outputBufferElements = 0, outputBufferElementsPosition = 0;
|
2017-11-06 02:37:36 +00:00
|
|
|
#endif
|
2014-04-06 18:31:53 +00:00
|
|
|
|
|
|
|
void kprintf(PCWCHAR format, ...)
|
|
|
|
{
|
2019-03-25 00:57:56 +00:00
|
|
|
#if !defined(MIMIKATZ_W2000_SUPPORT)
|
2014-05-04 23:24:54 +00:00
|
|
|
int varBuf;
|
|
|
|
size_t tempSize;
|
2017-03-19 15:03:54 +00:00
|
|
|
wchar_t * tmpBuffer;
|
2017-11-06 02:37:36 +00:00
|
|
|
#endif
|
2014-04-06 18:31:53 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2019-03-25 00:57:56 +00:00
|
|
|
#if !defined(MIMIKATZ_W2000_SUPPORT)
|
2014-05-04 23:24:54 +00:00
|
|
|
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
|
2017-03-19 15:03:54 +00:00
|
|
|
if(tmpBuffer = (wchar_t *) LocalAlloc(LPTR, tempSize * sizeof(wchar_t)))
|
|
|
|
{
|
|
|
|
RtlCopyMemory(tmpBuffer, outputBuffer, outputBufferElementsPosition * sizeof(wchar_t));
|
|
|
|
LocalFree(outputBuffer);
|
|
|
|
outputBuffer = tmpBuffer;
|
2014-05-04 23:24:54 +00:00
|
|
|
outputBufferElements = tempSize;
|
2017-03-19 15:03:54 +00:00
|
|
|
}
|
|
|
|
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());
|
2014-05-04 23:24:54 +00:00
|
|
|
}
|
|
|
|
varBuf = vswprintf_s(outputBuffer + outputBufferElementsPosition, outputBufferElements - outputBufferElementsPosition, format, args);
|
|
|
|
if(varBuf > 0)
|
|
|
|
outputBufferElementsPosition += varBuf;
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 02:37:36 +00:00
|
|
|
#endif
|
2019-03-25 00:57:56 +00:00
|
|
|
#if !defined(_POWERKATZ)
|
|
|
|
#if !defined(MIMIKATZ_W2000_SUPPORT)
|
2017-03-19 15:03:54 +00:00
|
|
|
else
|
2017-11-06 02:37:36 +00:00
|
|
|
#endif
|
2017-03-19 15:03:54 +00:00
|
|
|
{
|
|
|
|
vwprintf(format, args);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2014-05-04 23:24:54 +00:00
|
|
|
#endif
|
2014-04-06 18:31:53 +00:00
|
|
|
if(logfile)
|
2017-03-19 15:03:54 +00:00
|
|
|
{
|
2014-04-06 18:31:53 +00:00
|
|
|
vfwprintf(logfile, format, args);
|
2017-03-19 15:03:54 +00:00
|
|
|
fflush(logfile);
|
|
|
|
}
|
2014-04-06 18:31:53 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void kprintf_inputline(PCWCHAR format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
if(logfile)
|
2017-03-19 15:03:54 +00:00
|
|
|
{
|
2014-04-06 18:31:53 +00:00
|
|
|
vfwprintf(logfile, format, args);
|
2017-03-19 15:03:54 +00:00
|
|
|
fflush(logfile);
|
|
|
|
}
|
2014-04-06 18:31:53 +00:00
|
|
|
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)
|
2014-05-04 23:24:54 +00:00
|
|
|
newlog = _wfopen(file, L"a"); // XP does not like _wfopen_s
|
2014-04-06 18:31:53 +00:00
|
|
|
#pragma warning(pop)
|
|
|
|
if(newlog || !file)
|
|
|
|
{
|
|
|
|
if(logfile)
|
|
|
|
fclose(logfile);
|
|
|
|
logfile = newlog;
|
|
|
|
}
|
|
|
|
return (!file || (file && logfile));
|
2014-12-13 18:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int previousStdOut, previousStdErr;
|
|
|
|
UINT previousConsoleOutput;
|
|
|
|
void kull_m_output_init()
|
|
|
|
{
|
2019-03-25 00:57:56 +00:00
|
|
|
#if !defined(_POWERKATZ)
|
|
|
|
#if !defined(_WINDLL)
|
2014-12-13 18:52:00 +00:00
|
|
|
previousStdOut = _setmode(_fileno(stdout), _O_U8TEXT);
|
|
|
|
previousStdErr = _setmode(_fileno(stderr), _O_U8TEXT);
|
2017-07-19 23:33:50 +00:00
|
|
|
#endif
|
2014-12-13 18:52:00 +00:00
|
|
|
previousConsoleOutput = GetConsoleOutputCP();
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2017-03-19 15:03:54 +00:00
|
|
|
#endif
|
2014-12-13 18:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void kull_m_output_clean()
|
|
|
|
{
|
2019-03-25 00:57:56 +00:00
|
|
|
#if !defined(_POWERKATZ)
|
|
|
|
#if !defined(_WINDLL)
|
2014-12-13 18:52:00 +00:00
|
|
|
_setmode(_fileno(stdout), previousStdOut);
|
|
|
|
_setmode(_fileno(stderr), previousStdErr);
|
2017-07-19 23:33:50 +00:00
|
|
|
#endif
|
2014-12-13 18:52:00 +00:00
|
|
|
SetConsoleOutputCP(previousConsoleOutput);
|
2017-03-19 15:03:54 +00:00
|
|
|
#endif
|
|
|
|
}
|