Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for redirecting to file #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions SpecuCheck.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2020
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SpecuCheck", "SpecuCheck.vcxproj", "{8F20C38F-7966-4695-B747-C2F346EB8026}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8F20C38F-7966-4695-B747-C2F346EB8026}.Release|x64.ActiveCfg = Release|x64
{8F20C38F-7966-4695-B747-C2F346EB8026}.Release|x64.Build.0 = Release|x64
{8F20C38F-7966-4695-B747-C2F346EB8026}.Release|x86.ActiveCfg = Release|Win32
{8F20C38F-7966-4695-B747-C2F346EB8026}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C65086CA-A198-49AD-A3DD-E8951CBA9DBF}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion SpecuCheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion>
<RootNamespace>SpecuCheck</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<ProjectName>SpecuCheck</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down
4 changes: 4 additions & 0 deletions SpecuCheck.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
145 changes: 113 additions & 32 deletions specucheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ GetCyanString (
return g_SupportsAnsi ? "\x1b[1;36m" : "";
}

BOOL
IsConsoleRedirected(void);

INT
SpcMain (
VOID
Expand All @@ -229,23 +232,38 @@ SpcMain (
SPC_ERROR_CODES errorCode;
WCHAR stateBuffer[2048];
INT charsWritten;

//
DWORD dwBytesWritten;
BOOL boolRedirected;

// are we redirected?
boolRedirected = FALSE;
if (IsConsoleRedirected()) {
boolRedirected = TRUE;
}

//
// Open the output handle -- also not much we can do if this fails
//
hStdOut = CreateFile(L"CONOUT$",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hStdOut == INVALID_HANDLE_VALUE)
{
hStdOut = INVALID_HANDLE_VALUE;
errorCode = SpcFailedToOpenStandardOut;
goto Exit;
}

if (boolRedirected) {
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
}
else {
hStdOut = CreateFile(L"CONOUT$",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
}

if (hStdOut == INVALID_HANDLE_VALUE)
{
hStdOut = INVALID_HANDLE_VALUE;
errorCode = SpcFailedToOpenStandardOut;
goto Exit;
}

//
// Enable ANSI on Windows 10 if supported
Expand All @@ -258,7 +276,12 @@ SpcMain (
//
// We now have display capabilities -- say hello!
//
WriteConsole(hStdOut, WelcomeString, ARRAYSIZE(WelcomeString) - 1, NULL, NULL);
if (boolRedirected) {
WriteFile(hStdOut, WelcomeString, lstrlen(WelcomeString) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut, WelcomeString, ARRAYSIZE(WelcomeString) - 1, NULL, NULL);
}

//
// Get the KVA Shadow Information
Expand All @@ -272,11 +295,16 @@ SpcMain (
//
// Print out an error if this failed
//
WriteConsole(hStdOut,
UnpatchedString,
ARRAYSIZE(UnpatchedString) - 1,
NULL,
NULL);
if (boolRedirected) {
WriteFile(hStdOut, UnpatchedString, lstrlen(UnpatchedString) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut,
UnpatchedString,
ARRAYSIZE(UnpatchedString) - 1,
NULL,
NULL);
}
errorCode = SpcFailedToQueryKvaShadowing;
goto Exit;
}
Expand Down Expand Up @@ -320,7 +348,13 @@ SpcMain (
kvaInfo.KvaShadowFlags.KvaShadowInvpcid ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);

if (boolRedirected) {
WriteFile(hStdOut, stateBuffer, lstrlen(stateBuffer) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
}

//
// Print status of L1TF Features
Expand All @@ -347,7 +381,12 @@ SpcMain (
(kvaInfo.KvaShadowFlags.InvalidPteBit)) ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
if (boolRedirected) {
WriteFile(hStdOut, stateBuffer, lstrlen(stateBuffer) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
}

//
// Get the Speculation Control Information
Expand All @@ -361,13 +400,19 @@ SpcMain (
//
// Print out an error if this failed
//
WriteConsole(hStdOut,
UnpatchedString,
ARRAYSIZE(UnpatchedString) - 1,
NULL,
NULL);
errorCode = SpcFailedToQuerySpeculationControl;
goto Exit;
if (boolRedirected) {
WriteFile(hStdOut, UnpatchedString, lstrlen(UnpatchedString) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut,
UnpatchedString,
ARRAYSIZE(UnpatchedString) - 1,
NULL,
NULL);
}
errorCode = SpcFailedToQuerySpeculationControl;
goto Exit;

}
else if (!NT_SUCCESS(status))
{
Expand Down Expand Up @@ -417,7 +462,13 @@ SpcMain (
specInfo.SpeculationControlFlags.SpecCmdEnumerated ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);

if (boolRedirected) {
WriteFile(hStdOut, stateBuffer, lstrlen(stateBuffer) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
}

//
// Print status of Speculation Control SSBD Features
Expand Down Expand Up @@ -445,7 +496,12 @@ SpcMain (
specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisabledKernel ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
if (boolRedirected) {
WriteFile(hStdOut, stateBuffer, lstrlen(stateBuffer) * sizeof(WCHAR), &dwBytesWritten, NULL);
}
else {
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
}

//
// This is our happy path
Expand All @@ -466,3 +522,28 @@ SpcMain (
//
return errorCode;
}

BOOL IsConsoleRedirected() {
INT* stdouthndl = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdouthndl != INVALID_HANDLE_VALUE) {
UINT filetype = GetFileType(stdouthndl);
if (!((filetype == FILE_TYPE_UNKNOWN) && (GetLastError() != ERROR_SUCCESS))) {
DWORD mode;
filetype &= ~(FILE_TYPE_REMOTE);
if (filetype == FILE_TYPE_CHAR) {
BOOL retval = GetConsoleMode(stdouthndl, &mode);
if ((retval == FALSE) && (GetLastError() == ERROR_INVALID_HANDLE)) {
return TRUE;
}
else {
return FALSE;
}
}
else {
return TRUE;
}
}
}

return FALSE;
}