From 97fe9800a6283429690ce6ed8fc3d04513884dab Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Mon, 23 Jul 2018 22:57:54 -0700 Subject: [PATCH] implement basic gui --- GUI/mainwindow.cpp | 82 +++++++++++++++++++++++++++++++++++-------- GUI/mainwindow.h | 10 +++++- GUI/mainwindow.ui | 86 +++++++++++++++++++--------------------------- texthook/host.cc | 5 ++- 4 files changed, 117 insertions(+), 66 deletions(-) diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index ee5157c..09369d7 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -1,6 +1,8 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "QTextBrowser" #include "QMessageBox" +#include "QComboBox" #include "QLineEdit" #include "QTableWidget" #include "QInputDialog" @@ -9,46 +11,98 @@ #include #include "../texthook/host.h" -QTableWidget* processList; +QMainWindow* mainWindow; +QComboBox* processCombo; +QComboBox* ttCombo; +QTextBrowser* textOutput; QString GetModuleName(DWORD processId, HMODULE module = NULL) { HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId); wchar_t buffer[MAX_PATH]; GetModuleFileNameExW(handle, module, buffer, MAX_PATH); + CloseHandle(handle); return QString::fromWCharArray(wcsrchr(buffer, L'\\') + 1); } -void OnProcessAttach(DWORD processId) +QString ProcessString(DWORD processId) { - processList->setItem(processList->rowCount(), 0, new QTableWidgetItem(QString::number(processId))); + return QString("%1: %2").arg(QString::number(processId), GetModuleName(processId)); +} + +QString TextThreadString(TextThread* thread) +{ + ThreadParameter tp = thread->GetThreadParameter(); + return QString("%1:%2:%3:%4:%5:%6").arg( + QString::number(thread->Number()), + QString::number(tp.pid), + QString::number(tp.hook, 16), + QString::number(tp.retn, 16), + QString::number(tp.spl, 16), + QString::fromWCharArray(Host::GetHookName(tp.pid, tp.hook).c_str()) + ); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - Host::Start(); ui->setupUi(this); + mainWindow = this; + processCombo = mainWindow->findChild("processCombo"); + ttCombo = mainWindow->findChild("ttCombo"); + textOutput = this->findChild("textOutput"); - processList = this->findChild("processList"); - Host::RegisterProcessAttachCallback([](DWORD processId) - { - processList->insertRow(processList->rowCount()); - processList->setItem(processList->rowCount() - 1, 0, new QTableWidgetItem(QString::number(processId))); - processList->setItem(processList->rowCount() - 1, 1, new QTableWidgetItem(GetModuleName(processId))); - }); + Host::Start(); + Host::RegisterProcessAttachCallback(AddProcess); + Host::RegisterProcessDetachCallback(RemoveProcess); + Host::RegisterThreadCreateCallback(AddThread); + Host::RegisterThreadRemoveCallback(RemoveThread); Host::Open(); } MainWindow::~MainWindow() { + Host::Close(); delete ui; } +void AddProcess(DWORD processId) +{ + processCombo->addItem(ProcessString(processId)); +} + +void RemoveProcess(DWORD processId) +{ + processCombo->removeItem(processCombo->findText(ProcessString(processId))); +} + +void AddThread(TextThread* thread) +{ + ttCombo->addItem(TextThreadString(thread)); + thread->RegisterOutputCallBack([](auto thread, auto data) + { + if (ttCombo->currentText() == TextThreadString(thread)) textOutput->append(QString::fromWCharArray(data.c_str())); + return data + L"\r\n"; + }); +} + +void RemoveThread(TextThread* thread) +{ + ttCombo->removeItem(ttCombo->findText(TextThreadString(thread))); +} + void MainWindow::on_attachButton_clicked() { - //processList->insertRow(processList->rowCount()); - //processList->setItem(processList->rowCount() - 1, 0, new QTableWidgetItem(QString::number(6000))); - Host::InjectProcess(QInputDialog::getInt(this, "Process ID?", "")); + Host::InjectProcess(QInputDialog::getInt(this, "Process ID?", "You can find this under Task Manager -> Details")); +} + +void MainWindow::on_detachButton_clicked() +{ + Host::DetachProcess(processCombo->currentText().split(":")[0].toInt()); +} + +void MainWindow::on_ttCombo_activated(int index) +{ + textOutput->setText(QString::fromWCharArray(Host::GetThread(index)->GetStore().c_str())); } diff --git a/GUI/mainwindow.h b/GUI/mainwindow.h index d1084ea..2d4781c 100644 --- a/GUI/mainwindow.h +++ b/GUI/mainwindow.h @@ -2,6 +2,8 @@ #define MAINWINDOW_H #include +#include +#include "../texthook/textthread.h" namespace Ui { @@ -17,11 +19,17 @@ public: ~MainWindow(); private slots: - void on_attachButton_clicked(); + void on_detachButton_clicked(); + void on_ttCombo_activated(int index); private: Ui::MainWindow *ui; }; +void AddProcess(DWORD processId); +void RemoveProcess(DWORD processId); +void AddThread(TextThread* thread); +void RemoveThread(TextThread* thread); + #endif // MAINWINDOW_H diff --git a/GUI/mainwindow.ui b/GUI/mainwindow.ui index 62fc05d..b8e4cad 100644 --- a/GUI/mainwindow.ui +++ b/GUI/mainwindow.ui @@ -15,20 +15,10 @@ - - - - - 5 - 0 - - - - - + 2 0 @@ -40,6 +30,9 @@ QFrame::Raised + + + @@ -48,51 +41,44 @@ - - - - 0 - 0 - - + + + + - Currently attached to: - - - Qt::AlignCenter + Detach from game + + + + + + + + 6 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + - - - 2 + + + + + + + 5 + 0 + - - 50 - - - true - - - false - - - - ID - - - AlignCenter - - - - - Name - - - AlignCenter - - diff --git a/texthook/host.cc b/texthook/host.cc index bdf62cd..78c4fb9 100644 --- a/texthook/host.cc +++ b/texthook/host.cc @@ -74,13 +74,15 @@ namespace Host onCreate = onRemove = nullptr; nextThreadNumber = 0; // Console text thread - (textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++))->Status() |= USING_UNICODE; return true; } } DLLEXPORT void Open() { + TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++); + console->Status() |= USING_UNICODE; + if (onCreate) onCreate(console); CreateNewPipe(); } @@ -179,6 +181,7 @@ namespace Host DLLEXPORT std::wstring GetHookName(DWORD pid, DWORD addr) { + if (pid == 0) return L"Console"; HOST_LOCK; std::string buffer = ""; ProcessRecord pr = processRecordsByIds[pid];