Textractor_test/GUI/mainwindow.cpp

243 lines
7.6 KiB
C++
Raw Normal View History

2018-07-22 06:40:16 +08:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
2018-11-04 15:13:51 +08:00
#include "text.h"
#include "extenwindow.h"
2018-11-10 18:13:59 +08:00
#include "setdialog.h"
2018-07-26 12:48:18 +08:00
#include "misc.h"
2018-08-23 03:11:40 +08:00
#include <QInputDialog>
2018-07-24 03:25:02 +08:00
2018-07-22 06:40:16 +08:00
MainWindow::MainWindow(QWidget *parent) :
2018-07-26 01:46:59 +08:00
QMainWindow(parent),
2018-11-01 22:38:14 +08:00
ui(new Ui::MainWindow),
2018-11-10 14:17:02 +08:00
extenWindow(new ExtenWindow(this))
2018-07-22 06:40:16 +08:00
{
2018-07-26 01:46:59 +08:00
ui->setupUi(this);
2018-07-24 03:25:02 +08:00
2018-08-22 10:43:30 +08:00
processCombo = findChild<QComboBox*>("processCombo");
ttCombo = findChild<QComboBox*>("ttCombo");
textOutput = findChild<QPlainTextEdit*>("textOutput");
2018-11-10 18:13:59 +08:00
if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect());
2018-11-04 17:00:14 +08:00
if (settings.contains(FLUSH_DELAY)) TextThread::flushDelay = settings.value(FLUSH_DELAY).toInt();
if (settings.contains(MAX_BUFFER_SIZE)) TextThread::maxBufferSize = settings.value(MAX_BUFFER_SIZE).toInt();
2018-11-10 18:13:59 +08:00
if (settings.contains(DEFAULT_CODEPAGE)) TextThread::defaultCodepage = settings.value(DEFAULT_CODEPAGE).toInt();
qRegisterMetaType<std::shared_ptr<TextThread>>();
2018-08-22 10:43:30 +08:00
connect(this, &MainWindow::SigAddProcess, this, &MainWindow::AddProcess);
connect(this, &MainWindow::SigRemoveProcess, this, &MainWindow::RemoveProcess);
connect(this, &MainWindow::SigAddThread, this, &MainWindow::AddThread);
connect(this, &MainWindow::SigRemoveThread, this, &MainWindow::RemoveThread);
connect(this, &MainWindow::SigThreadOutput, this, &MainWindow::ThreadOutput);
2018-10-18 05:38:05 +08:00
2018-08-22 10:43:30 +08:00
Host::Start(
[&](DWORD processId) { emit SigAddProcess(processId); },
[&](DWORD processId) { emit SigRemoveProcess(processId); },
[&](std::shared_ptr<TextThread> thread) { emit SigAddThread(thread); },
[&](std::shared_ptr<TextThread> thread) { emit SigRemoveThread(thread); },
2018-10-08 12:26:43 +08:00
[&](TextThread* thread, std::wstring& output) { return ProcessThreadOutput(thread, output); }
2018-08-22 10:43:30 +08:00
);
2018-11-04 15:13:51 +08:00
Host::AddConsoleOutput(ABOUT);
2018-07-22 06:40:16 +08:00
}
MainWindow::~MainWindow()
{
2018-11-10 18:13:59 +08:00
settings.setValue(WINDOW, geometry());
2018-09-22 10:25:37 +08:00
settings.sync();
2018-07-26 01:46:59 +08:00
delete ui;
Host::Close();
2018-07-22 06:40:16 +08:00
}
2018-07-23 07:53:51 +08:00
void MainWindow::closeEvent(QCloseEvent*)
{
QCoreApplication::quit(); // Need to do this to kill any windows that might've been made by extensions
}
2018-10-08 12:26:43 +08:00
void MainWindow::AddProcess(unsigned processId)
2018-07-24 13:57:54 +08:00
{
2018-11-05 09:48:46 +08:00
if (processId == 0) return;
processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + GetModuleName(processId));
2018-11-04 17:00:14 +08:00
QFile file(HOOK_SAVE_FILE);
file.open(QIODevice::ReadOnly);
2018-07-27 13:42:21 +08:00
QString processName = GetFullModuleName(processId);
2018-10-10 20:16:14 +08:00
QStringList allProcesses = QString(file.readAll()).split("\r", QString::SkipEmptyParts);
for (auto hooks = allProcesses.rbegin(); hooks != allProcesses.rend(); ++hooks)
if (hooks->contains(processName))
2018-07-27 13:42:21 +08:00
{
2018-10-10 20:16:14 +08:00
for (auto hook : hooks->split(" , "))
if (auto hp = ParseCode(hook)) Host::InsertHook(processId, hp.value());
2018-07-27 13:42:21 +08:00
return;
}
2018-07-24 13:57:54 +08:00
}
2018-10-08 12:26:43 +08:00
void MainWindow::RemoveProcess(unsigned processId)
2018-07-24 13:57:54 +08:00
{
processCombo->removeItem(processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith));
2018-07-24 13:57:54 +08:00
}
void MainWindow::AddThread(std::shared_ptr<TextThread> thread)
2018-07-24 13:57:54 +08:00
{
2018-07-26 01:46:59 +08:00
ttCombo->addItem(
TextThreadString(thread.get()) +
2018-09-21 10:32:47 +08:00
QString::fromStdWString(thread->name) +
2018-07-26 12:48:18 +08:00
" (" +
2018-11-05 09:48:46 +08:00
GenerateCode(thread->hp, thread->tp.processId) +
2018-07-26 12:48:18 +08:00
")"
2018-07-26 01:46:59 +08:00
);
2018-07-24 13:57:54 +08:00
}
void MainWindow::RemoveThread(std::shared_ptr<TextThread> thread)
2018-07-24 13:57:54 +08:00
{
int threadIndex = ttCombo->findText(TextThreadString(thread.get()), Qt::MatchStartsWith);
2018-07-26 01:46:59 +08:00
if (threadIndex == ttCombo->currentIndex())
{
ttCombo->setCurrentIndex(0);
on_ttCombo_activated(0);
}
2018-08-02 14:17:20 +08:00
ttCombo->removeItem(threadIndex);
}
void MainWindow::ThreadOutput(QString threadString, QString output)
{
if (ttCombo->currentText().startsWith(threadString))
2018-07-26 01:46:59 +08:00
{
textOutput->moveCursor(QTextCursor::End);
textOutput->insertPlainText(output);
textOutput->moveCursor(QTextCursor::End);
}
2018-07-24 13:57:54 +08:00
}
2018-10-08 12:26:43 +08:00
bool MainWindow::ProcessThreadOutput(TextThread* thread, std::wstring& output)
{
if (DispatchSentenceToExtensions(output, GetMiscInfo(thread)))
2018-10-08 12:26:43 +08:00
{
output += L"\r\n";
emit SigThreadOutput(TextThreadString(thread), QString::fromStdWString(output));
2018-10-08 12:26:43 +08:00
return true;
}
return false;
}
2018-08-23 03:11:40 +08:00
QString MainWindow::TextThreadString(TextThread* thread)
{
2018-09-21 10:32:47 +08:00
ThreadParam tp = thread->tp;
2018-09-22 10:25:37 +08:00
return QString("%1:%2:%3:%4:%5: ").arg(
2018-09-23 05:13:53 +08:00
QString::number(thread->handle, 16),
2018-11-05 09:48:46 +08:00
QString::number(tp.processId, 16),
QString::number(tp.addr, 16),
QString::number(tp.ctx, 16),
QString::number(tp.ctx2, 16)
2018-08-23 03:11:40 +08:00
).toUpper();
}
2018-08-23 23:53:23 +08:00
ThreadParam MainWindow::ParseTextThreadString(QString textThreadString)
2018-08-23 03:11:40 +08:00
{
QStringList threadParam = textThreadString.split(":");
return { threadParam[1].toUInt(nullptr, 16), threadParam[2].toULongLong(nullptr, 16), threadParam[3].toULongLong(nullptr, 16), threadParam[4].toULongLong(nullptr, 16) };
2018-08-23 03:11:40 +08:00
}
DWORD MainWindow::GetSelectedProcessId()
{
return processCombo->currentText().split(":")[0].toULong(nullptr, 16);
2018-08-23 03:11:40 +08:00
}
std::unordered_map<std::string, int64_t> MainWindow::GetMiscInfo(TextThread* thread)
{
return
{
{ "current select", ttCombo->currentText().startsWith(TextThreadString(thread)) },
{ "text number", thread->handle },
2018-11-05 09:48:46 +08:00
{ "process id", thread->tp.processId },
{ "hook address", thread->tp.addr },
{ "text handle", thread->handle },
2018-09-21 10:32:47 +08:00
{ "text name", (int64_t)thread->name.c_str() }
};
}
2018-07-26 12:48:18 +08:00
QVector<HookParam> MainWindow::GetAllHooks(DWORD processId)
{
QSet<uint64_t> addresses;
2018-07-26 12:48:18 +08:00
QVector<HookParam> hooks;
for (int i = 0; i < ttCombo->count(); ++i)
2018-08-22 10:43:30 +08:00
{
2018-08-23 23:53:23 +08:00
ThreadParam tp = ParseTextThreadString(ttCombo->itemText(i));
2018-11-05 09:48:46 +08:00
if (tp.processId == processId && !addresses.contains(tp.addr))
2018-07-26 12:48:18 +08:00
{
2018-11-05 09:48:46 +08:00
addresses.insert(tp.addr);
2018-08-22 10:43:30 +08:00
hooks.push_back(Host::GetHookParam(tp));
2018-07-26 12:48:18 +08:00
}
2018-08-22 10:43:30 +08:00
}
2018-07-26 12:48:18 +08:00
return hooks;
}
2018-07-24 03:25:02 +08:00
void MainWindow::on_attachButton_clicked()
2018-07-23 07:53:51 +08:00
{
2018-11-04 15:13:51 +08:00
auto allProcesses = GetAllProcesses();
2018-09-10 10:37:48 +08:00
QStringList processList(allProcesses.uniqueKeys());
2018-08-21 02:30:50 +08:00
processList.sort(Qt::CaseInsensitive);
2018-07-26 12:48:18 +08:00
bool ok;
2018-11-19 21:17:00 +08:00
QString process = QInputDialog::getItem(this, SELECT_PROCESS, ATTACH_INFO, processList, 0, true, &ok, Qt::WindowCloseButtonHint);
2018-07-31 11:25:08 +08:00
if (!ok) return;
2018-11-04 15:13:51 +08:00
if (process.toInt(nullptr, 0)) Host::InjectProcess(process.toInt(nullptr, 0));
else for (auto processId : allProcesses.values(process)) Host::InjectProcess(processId);
2018-07-24 13:57:54 +08:00
}
void MainWindow::on_detachButton_clicked()
{
2018-08-22 10:43:30 +08:00
Host::DetachProcess(GetSelectedProcessId());
2018-07-24 13:57:54 +08:00
}
2018-07-26 12:48:18 +08:00
void MainWindow::on_hookButton_clicked()
{
bool ok;
2018-11-04 17:31:49 +08:00
QString hookCode = QInputDialog::getText(this, ADD_HOOK, CODE_INFODUMP, QLineEdit::Normal, "", &ok, Qt::WindowCloseButtonHint);
2018-07-31 11:25:08 +08:00
if (!ok) return;
2018-08-25 02:24:46 +08:00
if (auto hp = ParseCode(hookCode)) Host::InsertHook(GetSelectedProcessId(), hp.value());
2018-11-04 15:13:51 +08:00
else Host::AddConsoleOutput(INVALID_CODE);
2018-07-26 12:48:18 +08:00
}
void MainWindow::on_unhookButton_clicked()
{
2018-11-04 15:13:51 +08:00
auto hooks = GetAllHooks(GetSelectedProcessId());
if (hooks.empty()) return Host::AddConsoleOutput(NO_HOOKS);
2018-07-26 12:48:18 +08:00
QStringList hookList;
2018-11-05 09:48:46 +08:00
for (auto hp : hooks)
2018-10-10 20:16:14 +08:00
hookList.push_back(
2018-11-05 09:48:46 +08:00
QString::fromStdWString(Host::GetHookName(GetSelectedProcessId(), hp.insertion_address)) +
2018-10-10 20:16:14 +08:00
": " +
2018-11-05 09:48:46 +08:00
GenerateCode(hp, GetSelectedProcessId())
2018-10-10 20:16:14 +08:00
);
2018-07-26 12:48:18 +08:00
bool ok;
2018-11-04 17:31:49 +08:00
QString hook = QInputDialog::getItem(this, UNHOOK, REMOVE_HOOK, hookList, 0, false, &ok, Qt::WindowCloseButtonHint);
if (ok) Host::RemoveHook(GetSelectedProcessId(), hooks.at(hookList.indexOf(hook)).insertion_address);
2018-07-26 12:48:18 +08:00
}
2018-07-27 13:42:21 +08:00
void MainWindow::on_saveButton_clicked()
{
2018-11-04 15:13:51 +08:00
auto hooks = GetAllHooks(GetSelectedProcessId());
2018-08-22 10:43:30 +08:00
QString hookList = GetFullModuleName(GetSelectedProcessId());
2018-11-05 09:48:46 +08:00
for (auto hp : hooks)
if (!(hp.type & HOOK_ENGINE))
hookList += " , " + GenerateCode(hp, GetSelectedProcessId());
2018-11-04 17:00:14 +08:00
QFile file(HOOK_SAVE_FILE);
file.open(QIODevice::Append);
2018-07-27 13:42:21 +08:00
file.write((hookList + "\r\n").toUtf8());
}
2018-11-10 18:13:59 +08:00
void MainWindow::on_setButton_clicked()
{
SetDialog(this).exec();
}
void MainWindow::on_extenButton_clicked()
2018-07-24 13:57:54 +08:00
{
extenWindow->activateWindow();
extenWindow->showNormal();
2018-07-23 07:53:51 +08:00
}
2018-07-27 13:42:21 +08:00
void MainWindow::on_ttCombo_activated(int index)
2018-07-27 13:42:21 +08:00
{
textOutput->setPlainText(QString::fromStdWString(Host::GetThread(ParseTextThreadString(ttCombo->itemText(index)))->GetStorage()));
textOutput->moveCursor(QTextCursor::End);
2018-07-27 13:42:21 +08:00
}