From 1358effe896522f881bb418f8e9b2faec6ec449e Mon Sep 17 00:00:00 2001 From: JKornev <8bit.dosninja@gmail.com> Date: Tue, 6 Dec 2016 01:37:18 +0300 Subject: [PATCH] Design for the commands --- Hidden/todo.txt | 1 + HiddenCLI/Commands.cpp | 43 +++++++++++++ HiddenCLI/Commands.h | 22 ++++++- HiddenCLI/Connection.cpp | 50 +++++++++++++++ HiddenCLI/Connection.h | 15 ++++- HiddenCLI/Helper.cpp | 18 ++++++ HiddenCLI/Helper.h | 3 +- HiddenCLI/HiddenCLI.cpp | 8 ++- HiddenCLI/HiddenCLI.vcxproj | 2 + HiddenCLI/HiddenCLI.vcxproj.filters | 22 +++++++ HiddenCLI/Hide.cpp | 99 +++++++++++++++++++++++++++++ HiddenCLI/Hide.h | 45 +++++++++++++ HiddenCLI/cli.txt | 3 + 13 files changed, 326 insertions(+), 5 deletions(-) create mode 100644 HiddenCLI/Hide.cpp create mode 100644 HiddenCLI/Hide.h diff --git a/Hidden/todo.txt b/Hidden/todo.txt index d2ffea5..9ba3cb4 100644 --- a/Hidden/todo.txt +++ b/Hidden/todo.txt @@ -44,6 +44,7 @@ + Добавить поддержку флага автоприсвоение состояния существующим процессам для Hid_AddExcludedImage\Hid_AddProtectedImage + Проверить как ведёт себя файловый фильтр с файлами открытыми по ID или по короткому пути - Реализовать HiddenCLI +- Реализовать функционал вкл\выкл драйвера через IOCTL + Портировать драйвер под архитектуру x64 + Портировать под версии Windows 8, 8.1, 10 + Залить проект на Git diff --git a/HiddenCLI/Commands.cpp b/HiddenCLI/Commands.cpp index c3ecaac..ae2e2f3 100644 --- a/HiddenCLI/Commands.cpp +++ b/HiddenCLI/Commands.cpp @@ -1,2 +1,45 @@ #include "Commands.h" +#include "Hide.h" +using namespace std; + +// ================= + +Commands::Commands(Arguments& args) +{ + wstring arg; + + if (!args.GetNext(arg)) + throw WException(-2, L"Error, no command, please use 'hiddencli help'"); + + LoadCommandsStack(); + + do + { + for (auto it = m_commandsStack.begin(); it != m_commandsStack.end(); it++) + { + if ((*it)->CompareCommand(arg)) + { + (*it)->LoadArgs(args); + break; + } + } + } + while (args.GetNext(arg)); +} + +Commands::~Commands() +{ + +} + +void Commands::LoadCommandsStack() +{ + m_commandsStack.push_back(new CommandHide()); + m_commandsStack.push_back(new CommandUnhide()); +} + +void Commands::Perform(Connection& connection) +{ + +} diff --git a/HiddenCLI/Commands.h b/HiddenCLI/Commands.h index 05bc86c..2364736 100644 --- a/HiddenCLI/Commands.h +++ b/HiddenCLI/Commands.h @@ -1,9 +1,29 @@ #pragma once #include "Helper.h" +#include "Connection.h" + +class ICommand +{ +public: + + virtual ~ICommand() {}; + + virtual bool CompareCommand(std::wstring& command) = 0; + virtual void LoadArgs(Arguments& args) = 0; + virtual void PerformCommand(Connection& connection) = 0; +}; class Commands { + std::vector m_commandsStack; + + void LoadCommandsStack(); + public: - Commands(Arguments& args){} + + Commands(Arguments& args); + ~Commands(); + + void Perform(Connection& connection); }; diff --git a/HiddenCLI/Connection.cpp b/HiddenCLI/Connection.cpp index a81ea7b..393cbec 100644 --- a/HiddenCLI/Connection.cpp +++ b/HiddenCLI/Connection.cpp @@ -1,2 +1,52 @@ #include "Connection.h" +using namespace std; + +Connection::Connection(Arguments& args) : + m_context(nullptr) +{ + wstring arg; + + if (!args.Probe(arg)) + return; + + do + { + if (arg == L"gate") + { + args.SwitchToNext(); + if (!args.GetNext(m_deviceName)) + throw WException(-2, L"Error, mismatched argument for command 'gate'"); + } + else + { + break; + } + } + while (args.Probe(arg)); +} + +Connection::~Connection() +{ + if (m_context) + Hid_Destroy(m_context); +} + +void Connection::Open() +{ + HidStatus status; + const wchar_t* deviceName = nullptr; + + if (m_deviceName.size()) + deviceName = m_deviceName.c_str(); + + //status = Hid_Initialize(&m_context, deviceName); + //if (!HID_STATUS_SUCCESSFUL(status)) + // throw WException(HID_STATUS_CODE(status), L"Error, can't connect to gate"); +} + +HidContext Connection::GetContext() +{ + return m_context; +} + diff --git a/HiddenCLI/Connection.h b/HiddenCLI/Connection.h index 49c348e..7afac2e 100644 --- a/HiddenCLI/Connection.h +++ b/HiddenCLI/Connection.h @@ -1,9 +1,22 @@ #pragma once #include "Helper.h" +#include "../HiddenLib/HiddenLib.h" class Connection { +private: + + HidContext m_context; + + std::wstring m_deviceName; + public: - Connection(Arguments& args){} + + Connection(Arguments& args); + ~Connection(); + + void Open(); + + HidContext GetContext(); }; diff --git a/HiddenCLI/Helper.cpp b/HiddenCLI/Helper.cpp index cd9aa49..e401211 100644 --- a/HiddenCLI/Helper.cpp +++ b/HiddenCLI/Helper.cpp @@ -37,6 +37,24 @@ size_t Arguments::ArgsCount() return m_arguments.size(); } +bool Arguments::Probe(std::wstring& arg) +{ + if (m_argPointer >= m_arguments.size()) + return false; + + arg = m_arguments[m_argPointer]; + return true; +} + +bool Arguments::SwitchToNext() +{ + if (m_argPointer >= m_arguments.size()) + return false; + + m_argPointer++; + return true; +} + bool Arguments::GetNext(wstring& arg) { if (m_argPointer >= m_arguments.size()) diff --git a/HiddenCLI/Helper.h b/HiddenCLI/Helper.h index 57d4331..b617f8e 100644 --- a/HiddenCLI/Helper.h +++ b/HiddenCLI/Helper.h @@ -30,7 +30,8 @@ public: size_t ArgsCount(); - bool ProbNext(std::wstring& arg); + bool Probe(std::wstring& arg); + bool SwitchToNext(); bool GetNext(std::wstring& arg); }; diff --git a/HiddenCLI/HiddenCLI.cpp b/HiddenCLI/HiddenCLI.cpp index 7684ed3..a50fcfe 100644 --- a/HiddenCLI/HiddenCLI.cpp +++ b/HiddenCLI/HiddenCLI.cpp @@ -14,6 +14,7 @@ int wmain(int argc, wchar_t* argv[]) try { Arguments arguments(argc, argv); + Connection connection(arguments); if (!arguments.ArgsCount()) throw WException( @@ -21,9 +22,12 @@ int wmain(int argc, wchar_t* argv[]) L"Welcome to HiddenCLI, please use 'hiddencli help'" ); - Connection connection(arguments); - Commands commands(arguments); + { + Commands commands(arguments); + connection.Open(); + commands.Perform(connection); + } } catch (WException& exception) { diff --git a/HiddenCLI/HiddenCLI.vcxproj b/HiddenCLI/HiddenCLI.vcxproj index d96f204..7b39ca8 100644 --- a/HiddenCLI/HiddenCLI.vcxproj +++ b/HiddenCLI/HiddenCLI.vcxproj @@ -155,11 +155,13 @@ + + diff --git a/HiddenCLI/HiddenCLI.vcxproj.filters b/HiddenCLI/HiddenCLI.vcxproj.filters index 0dd35be..a75836e 100644 --- a/HiddenCLI/HiddenCLI.vcxproj.filters +++ b/HiddenCLI/HiddenCLI.vcxproj.filters @@ -2,5 +2,27 @@ + + + + + Commands + + + + + + + + + + + Commands + + + + + {680a2e80-be0d-4ac2-8a4a-d59b67e55c61} + \ No newline at end of file diff --git a/HiddenCLI/Hide.cpp b/HiddenCLI/Hide.cpp new file mode 100644 index 0000000..36070fa --- /dev/null +++ b/HiddenCLI/Hide.cpp @@ -0,0 +1,99 @@ +#include "Hide.h" + +using namespace std; + +// ================= + +CommandHide::CommandHide() : m_command(L"hide") +{ +} + +CommandHide::~CommandHide() +{ +} + +bool CommandHide::CompareCommand(std::wstring& command) +{ + return (command == m_command); +} + +void CommandHide::LoadArgs(Arguments& args) +{ + wstring object; + + if (!args.GetNext(object)) + throw WException(-2, L"Error, mismatched argument #1 for command 'hide'"); + + if (!args.GetNext(m_path)) + throw WException(-2, L"Error, mismatched argument #2 for command 'hide'"); + + if (object == L"file") + { + m_hideType = EHideTypes::TypeFile; + } + else if (object == L"dir") + { + m_hideType = EHideTypes::TypeDir; + } + else if (object == L"regkey") + { + m_hideType = EHideTypes::TypeRegKey; + m_regRootType = GetRegType(m_path); + } + else if (object == L"regval") + { + m_hideType = EHideTypes::TypeRegVal; + m_regRootType = GetRegType(m_path); + } + else + { + throw WException(-2, L"Error, invalid argument for command 'hide'"); + } + +} + +void CommandHide::PerformCommand(Connection& connection) +{ + +} + +HidRegRootTypes CommandHide::GetRegType(wstring& path) +{ + static wchar_t regHKLM[] = L"HKLM\\"; + static wchar_t regHKCU[] = L"HKCU\\"; + static wchar_t regHKU[] = L"HKU\\"; + + if (path.compare(0, _countof(regHKLM) - 1, regHKLM) == 0) + return HidRegRootTypes::RegHKLM; + else if (path.compare(0, _countof(regHKCU) - 1, regHKCU) == 0) + return HidRegRootTypes::RegHKCU; + else if (path.compare(0, _countof(regHKU) - 1, regHKU) == 0) + return HidRegRootTypes::RegHKU; + else + throw WException(-2, L"Error, invalid registry prefix"); +} + +// ================= + +CommandUnhide::CommandUnhide() : m_command(L"unhide") +{ +} + +CommandUnhide::~CommandUnhide() +{ +} + +bool CommandUnhide::CompareCommand(std::wstring& command) +{ + return (command == m_command); +} + +void CommandUnhide::LoadArgs(Arguments& args) +{ + +} + +void CommandUnhide::PerformCommand(Connection& connection) +{ + +} diff --git a/HiddenCLI/Hide.h b/HiddenCLI/Hide.h new file mode 100644 index 0000000..48ad71f --- /dev/null +++ b/HiddenCLI/Hide.h @@ -0,0 +1,45 @@ +#pragma once + +#include "Commands.h" + +enum EHideTypes { + TypeFile, + TypeDir, + TypeRegKey, + TypeRegVal, + TypeUnknown, +}; + +class CommandHide : public ICommand +{ + const wchar_t* m_command = nullptr; + + EHideTypes m_hideType; + HidRegRootTypes m_regRootType; + std::wstring m_path; + + HidRegRootTypes GetRegType(std::wstring& path); + +public: + + CommandHide(); + virtual ~CommandHide(); + + virtual bool CompareCommand(std::wstring& command); + virtual void LoadArgs(Arguments& args); + virtual void PerformCommand(Connection& connection); +}; + +class CommandUnhide : public ICommand +{ + const wchar_t* m_command = nullptr; + +public: + + CommandUnhide(); + virtual ~CommandUnhide(); + + virtual bool CompareCommand(std::wstring& command); + virtual void LoadArgs(Arguments& args); + virtual void PerformCommand(Connection& connection); +}; diff --git a/HiddenCLI/cli.txt b/HiddenCLI/cli.txt index 0e1e6bc..54b1394 100644 --- a/HiddenCLI/cli.txt +++ b/HiddenCLI/cli.txt @@ -2,6 +2,9 @@ hiddencli [connection] connection: + + gate <%name%> + Set specific connection gate name (driver device name) commands: