Hook CreateFile and CreateConsoleScreenBuffer

If console handles are created as GENERIC_WRITE, add GENERIC_READ in
order to be able to retrieve console information.  This fixes #93
(redirecting to CON).
This commit is contained in:
Jason Hood 2017-10-30 10:50:11 +10:00
parent 97dcb13634
commit 2f3fe57b60
3 changed files with 80 additions and 18 deletions

64
ANSI.c
View File

@ -152,10 +152,11 @@
remove wcstok, avoiding potential interference with the host;
similarly, use a private heap instead of malloc.
v1.80, 26 to 28 October, 2017:
v1.80, 26 to 30 October, 2017:
fix unloading;
revert back to (re)storing buffer cursor position;
increase cache to five handles.
increase cache to five handles;
hook CreateFile & CreateConsoleScreenBuffer to enable readable handles.
*/
#include "ansicon.h"
@ -2286,6 +2287,62 @@ WINAPI My_lwrite( HFILE hFile, LPCSTR lpBuffer, UINT uBytes )
return _lwrite( hFile, lpBuffer, uBytes );
}
//-----------------------------------------------------------------------------
// MyCreate...
// Add GENERIC_READ access to enable retrieving console info.
//-----------------------------------------------------------------------------
HANDLE
WINAPI MyCreateFileA( LPCSTR lpFileName, DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile )
{
if (dwDesiredAccess == GENERIC_WRITE)
{
if (_stricmp( lpFileName, "con" ) == 0)
lpFileName = "CONOUT$";
if (_stricmp( lpFileName, "CONOUT$" ) == 0)
dwDesiredAccess |= GENERIC_READ;
}
return CreateFileA( lpFileName, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, hTemplateFile );
}
HANDLE
WINAPI MyCreateFileW( LPCWSTR lpFileName, DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile )
{
if (dwDesiredAccess == GENERIC_WRITE)
{
if (_wcsicmp( lpFileName, L"con" ) == 0)
lpFileName = L"CONOUT$";
if (_wcsicmp( lpFileName, L"CONOUT$" ) == 0)
dwDesiredAccess |= GENERIC_READ;
}
return CreateFileW( lpFileName, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, hTemplateFile );
}
HANDLE
WINAPI MyCreateConsoleScreenBuffer( DWORD dwDesiredAccess, DWORD dwShareMode,
const SECURITY_ATTRIBUTES* lpSecurityAttributes,
DWORD dwFlags, LPVOID lpScreenBufferData )
{
dwDesiredAccess |= GENERIC_READ;
return CreateConsoleScreenBuffer( dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwFlags,
lpScreenBufferData );
}
// ========== Environment variable
void set_ansicon( PCONSOLE_SCREEN_BUFFER_INFO pcsbi )
@ -2382,6 +2439,9 @@ HookFn Hooks[] = {
{ APIConsole, "WriteConsoleW", (PROC)MyWriteConsoleW, NULL, NULL, NULL },
{ APIFile, "WriteFile", (PROC)MyWriteFile, NULL, NULL, NULL },
{ APIKernel, "_lwrite", (PROC)My_lwrite, NULL, NULL, NULL },
{ APIFile, "CreateFileA", (PROC)MyCreateFileA, NULL, NULL, NULL },
{ APIFile, "CreateFileW", (PROC)MyCreateFileW, NULL, NULL, NULL },
{ APIKernel, "CreateConsoleScreenBuffer", (PROC)MyCreateConsoleScreenBuffer, NULL, NULL, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL }
};

View File

@ -90,7 +90,7 @@
write newline with _putws, not putwchar (fixes redirecting to CON).
*/
#define PDATE L"28 October, 2017"
#define PDATE L"30 October, 2017"
#include "ansicon.h"
#include "version.h"

View File

@ -298,6 +298,8 @@ Version History
1.80 - 28 October, 2017:
- fix unloading;
- fix -e et al when redirecting to CON;
- hook CreateFile and CreateConsoleScreenBuffer to force read/write access
(fixes redirecting to CON and Microsoft's conio);
* go back to saving the buffer cursor position.
1.72 - 24 December, 2015:
@ -526,4 +528,4 @@ Distribution
=============================
Jason Hood, 28 October, 2017.
Jason Hood, 30 October, 2017.