6
0
mirror of https://github.com/JKornev/hidden synced 2024-06-26 00:48:05 +00:00
hidden/HiddenCLI/Ignore.cpp

204 lines
5.0 KiB
C++
Raw Normal View History

2016-12-06 23:19:49 +00:00
#include "Ignore.h"
#include <iostream>
using namespace std;
// =================
CommandIgnore::CommandIgnore() : m_command(L"/ignore")
{
}
CommandIgnore::~CommandIgnore()
{
}
bool CommandIgnore::CompareCommand(std::wstring& command)
{
return (command == m_command);
}
void CommandIgnore::LoadArgs(Arguments& args, CommandModeType mode)
2016-12-06 23:19:49 +00:00
{
wstring object, target;
if (!args.GetNext(object))
throw WException(-2, L"Error, mismatched argument #1 for command 'ignore'");
if (object == L"image")
{
m_procType = EProcTypes::TypeImage;
}
else if (object == L"pid")
{
if (!CommandModeType::Execute)
throw WException(-2, L"Error, target 'pid' isn't allowed");
2016-12-06 23:19:49 +00:00
m_procType = EProcTypes::TypeProcessId;
}
else
{
throw WException(-2, L"Error, invalid object type in command 'ignore'");
}
m_inheritType = LoadInheritOption(args, HidPsInheritTypes::WithoutInherit);
m_applyByDefault = false;
if (m_procType == EProcTypes::TypeImage && mode == CommandModeType::Execute)
2016-12-06 23:19:49 +00:00
m_applyByDefault = LoadApplyOption(args, m_applyByDefault);
if (!args.GetNext(target))
throw WException(-2, L"Error, mismatched argument #2 for command 'ignore'");
if (m_procType == EProcTypes::TypeImage)
{
m_targetImage = target;
}
else
{
m_targetProcId = _wtol(target.c_str());
2016-12-06 23:19:49 +00:00
if (!m_targetProcId)
throw WException(-2, L"Error, invalid target pid for command 'ignore'");
}
}
void CommandIgnore::PerformCommand(Connection& connection)
{
HidStatus status;
HidObjId objId = 0;
2016-12-06 23:19:49 +00:00
switch (m_procType)
{
case EProcTypes::TypeProcessId:
status = Hid_AttachExcludedState(connection.GetContext(), m_targetProcId, m_inheritType);
break;
case EProcTypes::TypeImage:
status = Hid_AddExcludedImage(connection.GetContext(), m_targetImage.c_str(), m_inheritType, m_applyByDefault, &objId);
break;
default:
throw WException(-2, L"Internal error, invalid type for command 'ignore'");
}
if (!HID_STATUS_SUCCESSFUL(status))
throw WException(HID_STATUS_CODE(status), L"Error, command 'ignore' rejected");
2016-12-26 21:33:16 +00:00
g_stderr << L"Command 'ignore' successful" << endl;
if (m_procType == EProcTypes::TypeImage)
g_stdout << L"ruleid:" << objId << endl;
2016-12-06 23:19:49 +00:00
}
void CommandIgnore::InstallCommand(RegistryKey& configKey)
{
vector<wstring> commands;
2016-12-25 20:56:18 +00:00
wstring temp, entry;
HidStatus status;
temp.insert(0, m_targetImage.size() + HID_NORMALIZATION_OVERHEAD, L'\0');
status = Hid_NormalizeFilePath(m_targetImage.c_str(), const_cast<wchar_t*>(temp.c_str()), temp.size());
if (!HID_STATUS_SUCCESSFUL(status))
throw WException(HID_STATUS_CODE(status), L"Error, can't normalize path, 'ignore' rejected");
2016-12-25 20:56:18 +00:00
entry += temp.c_str();
entry += L";";
entry += ConvertInheritTypeToUnicode(m_inheritType);
configKey.GetMultiStrValue(L"Hid_IgnoredImages", commands);
commands.push_back(entry);
configKey.SetMultiStrValue(L"Hid_IgnoredImages", commands);
2016-12-26 21:33:16 +00:00
g_stderr << L"Install 'ignore' successful" << endl;
}
void CommandIgnore::UninstallCommand(RegistryKey& configKey)
{
configKey.RemoveValue(L"Hid_IgnoredImages");
2016-12-26 21:33:16 +00:00
g_stderr << L"Uninstall 'ignore' successful" << endl;
}
2016-12-14 23:29:27 +00:00
CommandPtr CommandIgnore::CreateInstance()
{
return CommandPtr(new CommandIgnore());
}
2016-12-06 23:19:49 +00:00
// =================
CommandUnignore::CommandUnignore() : m_command(L"/unignore")
{
}
CommandUnignore::~CommandUnignore()
{
}
bool CommandUnignore::CompareCommand(std::wstring& command)
{
return (command == m_command);
}
void CommandUnignore::LoadArgs(Arguments& args, CommandModeType mode)
2016-12-06 23:19:49 +00:00
{
wstring object, target;
if (mode != CommandModeType::Execute)
throw WException(-2, L"Error, install/uninstall mode isn't supported for this command");
if (!args.GetNext(object))
throw WException(-2, L"Error, mismatched argument #1 for command 'unignore'");
if (object == L"pid")
{
m_targetType = ETargetIdType::ProcId;
if (!args.GetNext(target))
throw WException(-2, L"Error, mismatched argument #2 for command 'unignore'");
m_targetProcId = _wtol(target.c_str());
if (!m_targetProcId)
throw WException(-2, L"Error, invalid target ruleid for command 'unignore'");
}
else if (object == L"all")
{
m_targetType = ETargetIdType::All;
}
else
{
m_targetType = ETargetIdType::RuleId;
m_targetId = _wtoll(object.c_str());
if (!m_targetId)
throw WException(-2, L"Error, invalid target ruleid for command 'unignore'");
}
2016-12-06 23:19:49 +00:00
}
void CommandUnignore::PerformCommand(Connection& connection)
{
HidStatus status;
switch (m_targetType)
{
case ETargetIdType::All:
status = Hid_RemoveAllExcludedImages(connection.GetContext());
break;
case ETargetIdType::ProcId:
status = Hid_RemoveExcludedState(connection.GetContext(), m_targetProcId);
break;
case ETargetIdType::RuleId:
status = Hid_RemoveExcludedImage(connection.GetContext(), m_targetId);
break;
default:
throw WException(-2, L"Internal error, invalid type for command 'unignore'");
}
if (!HID_STATUS_SUCCESSFUL(status))
throw WException(HID_STATUS_CODE(status), L"Error, command 'unignore' rejected");
2016-12-26 21:33:16 +00:00
g_stderr << L"Command 'unignore' successful" << endl;
2016-12-06 23:19:49 +00:00
}
2016-12-14 23:29:27 +00:00
CommandPtr CommandUnignore::CreateInstance()
{
return CommandPtr(new CommandUnignore());
}