6
0
mirror of https://github.com/JKornev/hidden synced 2024-06-29 18:32:00 +00:00
hidden/HiddenCLI/Hide.cpp

264 lines
6.8 KiB
C++
Raw Normal View History

2016-12-05 22:37:18 +00:00
#include "Hide.h"
#include <iostream>
2016-12-25 20:56:18 +00:00
#include <algorithm>
2016-12-05 22:37:18 +00:00
using namespace std;
// =================
CommandHide::CommandHide() : m_command(L"/hide")
2016-12-05 22:37:18 +00:00
{
}
CommandHide::~CommandHide()
{
}
bool CommandHide::CompareCommand(std::wstring& command)
{
return (command == m_command);
}
2016-12-25 20:56:18 +00:00
HidRegRootTypes CommandHide::GetTypeAndNormalizeRegPath(std::wstring& regPath)
{
HidRegRootTypes type = GetRegType(regPath);
size_t pos = regPath.find(L"\\");
if (pos == wstring::npos)
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, invalid registry path");
2016-12-25 20:56:18 +00:00
regPath = std::move(wstring(regPath.c_str() + pos + 1));
return type;
}
void CommandHide::LoadArgs(Arguments& args, CommandModeType mode)
2016-12-05 22:37:18 +00:00
{
wstring object;
if (!args.GetNext(object))
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, mismatched argument #1 for command 'hide'");
2016-12-05 22:37:18 +00:00
if (!args.GetNext(m_path))
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, mismatched argument #2 for command 'hide'");
2016-12-05 22:37:18 +00:00
if (object == L"file")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeFile;
2016-12-05 22:37:18 +00:00
}
else if (object == L"dir")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeDir;
2016-12-05 22:37:18 +00:00
}
else if (object == L"regkey")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeRegKey;
2016-12-25 20:56:18 +00:00
m_regRootType = GetTypeAndNormalizeRegPath(m_path);
2016-12-05 22:37:18 +00:00
}
else if (object == L"regval")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeRegVal;
2016-12-25 20:56:18 +00:00
m_regRootType = GetTypeAndNormalizeRegPath(m_path);
2016-12-05 22:37:18 +00:00
}
else
{
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, invalid argument for command 'hide'");
2016-12-05 22:37:18 +00:00
}
}
void CommandHide::PerformCommand(Connection& connection)
{
HidStatus status;
HidObjId objId;
2016-12-05 22:37:18 +00:00
switch (m_hideType)
{
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeFile:
status = Hid_AddHiddenFile(connection.GetContext(), m_path.c_str(), &objId);
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeDir:
status = Hid_AddHiddenDir(connection.GetContext(), m_path.c_str(), &objId);
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeRegKey:
status = Hid_AddHiddenRegKey(connection.GetContext(), m_regRootType, m_path.c_str(), &objId);
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeRegVal:
status = Hid_AddHiddenRegValue(connection.GetContext(), m_regRootType, m_path.c_str(), &objId);
break;
default:
2016-12-26 21:52:27 +00:00
throw WException(ERROR_UNKNOWN_COMPONENT, L"Internal error, invalid type for command 'hide'");
}
if (!HID_STATUS_SUCCESSFUL(status))
throw WException(HID_STATUS_CODE(status), L"Error, command 'hide' rejected");
2016-12-26 21:33:16 +00:00
g_stderr << L"Command 'hide' successful" << endl;
g_stdout << L"ruleid:" << objId << endl;
2016-12-05 22:37:18 +00:00
}
void CommandHide::InstallCommand(RegistryKey& configKey)
{
vector<wstring> commands;
const wchar_t* valueName;
2016-12-25 20:56:18 +00:00
HidStatus status;
wstring entry;
2016-12-25 20:56:18 +00:00
entry.insert(0, m_path.size() + HID_NORMALIZATION_OVERHEAD, L'\0');
switch (m_hideType)
{
case EObjTypes::TypeFile:
valueName = L"Hid_HideFsFiles";
2016-12-25 20:56:18 +00:00
status = Hid_NormalizeFilePath(m_path.c_str(), const_cast<wchar_t*>(entry.c_str()), entry.size());
break;
case EObjTypes::TypeDir:
valueName = L"Hid_HideFsDirs";
2016-12-25 20:56:18 +00:00
status = Hid_NormalizeFilePath(m_path.c_str(), const_cast<wchar_t*>(entry.c_str()), entry.size());
break;
case EObjTypes::TypeRegKey:
valueName = L"Hid_HideRegKeys";
2016-12-25 20:56:18 +00:00
status = Hid_NormalizeRegistryPath(m_regRootType, m_path.c_str(), const_cast<wchar_t*>(entry.c_str()), entry.size());
break;
case EObjTypes::TypeRegVal:
valueName = L"Hid_HideRegValues";
2016-12-25 20:56:18 +00:00
status = Hid_NormalizeRegistryPath(m_regRootType, m_path.c_str(), const_cast<wchar_t*>(entry.c_str()), entry.size());
break;
default:
2016-12-26 21:52:27 +00:00
throw WException(ERROR_UNKNOWN_COMPONENT, L"Internal error, invalid type for command 'hide'");
}
configKey.GetMultiStrValue(valueName, commands);
commands.push_back(entry);
configKey.SetMultiStrValue(valueName, commands);
2016-12-26 21:33:16 +00:00
g_stderr << L"Install 'hide' successful" << endl;
}
void CommandHide::UninstallCommand(RegistryKey& configKey)
{
2016-12-26 21:33:16 +00:00
int errors = 0;
2016-12-26 21:33:16 +00:00
try { configKey.RemoveValue(L"Hid_HideFsFiles"); } catch (...) { errors++; }
try { configKey.RemoveValue(L"Hid_HideFsDirs"); } catch (...) { errors++; }
try { configKey.RemoveValue(L"Hid_HideRegKeys"); } catch (...) { errors++; }
try { configKey.RemoveValue(L"Hid_HideRegValues"); } catch (...) { errors++; }
if (errors < 4)
g_stderr << L"Uninstall 'hide' successful" << endl;
}
2016-12-14 23:29:27 +00:00
CommandPtr CommandHide::CreateInstance()
{
return CommandPtr(new CommandHide());
}
2016-12-05 22:37:18 +00:00
// =================
CommandUnhide::CommandUnhide() : m_command(L"/unhide")
2016-12-05 22:37:18 +00:00
{
m_targetId = 0;
2016-12-05 22:37:18 +00:00
}
CommandUnhide::~CommandUnhide()
{
}
bool CommandUnhide::CompareCommand(std::wstring& command)
{
return (command == m_command);
}
void CommandUnhide::LoadArgs(Arguments& args, CommandModeType mode)
2016-12-05 22:37:18 +00:00
{
wstring object, target;
if (!args.GetNext(object))
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, mismatched argument #1 for command 'unhide'");
if (!args.GetNext(target))
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, mismatched argument #2 for command 'unhide'");
if (object == L"file")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeFile;
}
else if (object == L"dir")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeDir;
}
else if (object == L"regkey")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeRegKey;
}
else if (object == L"regval")
{
2016-12-06 23:19:49 +00:00
m_hideType = EObjTypes::TypeRegVal;
}
else
{
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, invalid argument for command 'unhide'");
}
2016-12-05 22:37:18 +00:00
m_targetAll = (target == L"all");
if (!m_targetAll)
{
m_targetId = _wtoll(target.c_str());
if (!m_targetId)
2016-12-26 21:52:27 +00:00
throw WException(ERROR_INVALID_PARAMETER, L"Error, invalid target objid for command 'unhide'");
}
2016-12-05 22:37:18 +00:00
}
void CommandUnhide::PerformCommand(Connection& connection)
{
HidStatus status;
if (m_targetAll)
{
switch (m_hideType)
{
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeFile:
status = Hid_RemoveAllHiddenFiles(connection.GetContext());
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeDir:
status = Hid_RemoveAllHiddenDirs(connection.GetContext());
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeRegKey:
status = Hid_RemoveAllHiddenRegKeys(connection.GetContext());
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeRegVal:
status = Hid_RemoveAllHiddenRegValues(connection.GetContext());
break;
default:
2016-12-26 21:52:27 +00:00
throw WException(ERROR_UNKNOWN_COMPONENT, L"Internal error #1, invalid type for command 'unhide'");
}
}
else
{
switch (m_hideType)
{
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeFile:
status = Hid_RemoveHiddenFile(connection.GetContext(), m_targetId);
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeDir:
status = Hid_RemoveHiddenDir(connection.GetContext(), m_targetId);
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeRegKey:
status = Hid_RemoveHiddenRegKey(connection.GetContext(), m_targetId);
break;
2016-12-06 23:19:49 +00:00
case EObjTypes::TypeRegVal:
status = Hid_RemoveHiddenRegValue(connection.GetContext(), m_targetId);
break;
default:
2016-12-26 21:52:27 +00:00
throw WException(ERROR_UNKNOWN_COMPONENT, L"Internal error #2, invalid type for command 'unhide'");
}
}
if (!HID_STATUS_SUCCESSFUL(status))
2016-12-26 21:33:16 +00:00
throw WException(HID_STATUS_CODE(status), L"Error, command 'unhide' rejected");
2016-12-05 22:37:18 +00:00
2016-12-26 21:33:16 +00:00
g_stderr << L"Command 'unhide' successful" << endl;
2016-12-05 22:37:18 +00:00
}
2016-12-14 23:29:27 +00:00
CommandPtr CommandUnhide::CreateInstance()
{
return CommandPtr(new CommandUnhide());
}