Textractor_test/GUI/mainwindow.cpp

275 lines
9.7 KiB
C++
Raw Normal View History

2018-07-22 06:40:16 +08:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
2018-12-19 01:14:54 +08:00
#include "defs.h"
2018-11-04 15:13:51 +08:00
#include "text.h"
#include "extenwindow.h"
2018-07-26 12:48:18 +08:00
#include "misc.h"
#include "host/util.h"
#include <Psapi.h>
#include <winhttp.h>
#include <QFormLayout>
2018-12-15 11:26:49 +08:00
#include <QPushButton>
#include <QSpinBox>
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);
for (auto[text, slot] : Array<std::tuple<QString, void(MainWindow::*)()>>{
2018-12-19 01:14:54 +08:00
{ ATTACH, &MainWindow::AttachProcess },
{ DETACH, &MainWindow::DetachProcess },
{ ADD_HOOK, &MainWindow::AddHook },
{ SAVE_HOOKS, &MainWindow::SaveHooks },
{ SETTINGS, &MainWindow::Settings },
{ EXTENSIONS, &MainWindow::Extensions }
2018-12-15 11:26:49 +08:00
})
{
QPushButton* button = new QPushButton(ui->processFrame);
2018-12-15 11:26:49 +08:00
connect(button, &QPushButton::clicked, this, slot);
button->setText(text);
ui->processLayout->addWidget(button);
2018-12-15 11:26:49 +08:00
}
ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
2018-08-22 10:43:30 +08:00
2018-12-19 01:14:54 +08:00
connect(ui->ttCombo, (void(QComboBox::*)(int))&QComboBox::activated, this, &MainWindow::ViewThread);
QSettings settings(CONFIG_FILE, QSettings::IniFormat);
2019-01-01 04:06:47 +08:00
setGeometry(settings.value(WINDOW, geometry()).toRect());
TextThread::flushDelay = settings.value(FLUSH_DELAY, TextThread::flushDelay).toInt();
TextThread::maxBufferSize = settings.value(MAX_BUFFER_SIZE, TextThread::maxBufferSize).toInt();
TextThread::defaultCodepage = settings.value(DEFAULT_CODEPAGE, TextThread::defaultCodepage).toInt();
2018-08-22 10:43:30 +08:00
Host::Start(
2018-12-19 01:55:11 +08:00
[this](DWORD processId) { ProcessConnected(processId); },
[this](DWORD processId) { ProcessDisconnected(processId); },
[this](TextThread* thread) { ThreadAdded(thread); },
[this](TextThread* thread) { ThreadRemoved(thread); },
[this](TextThread* thread, std::wstring& output) { return SentenceReceived(thread, output); }
2018-08-22 10:43:30 +08:00
);
2018-11-04 15:13:51 +08:00
Host::AddConsoleOutput(ABOUT);
std::thread([]
{
// Queries GitHub releases API https://developer.github.com/v3/repos/releases/ and checks the last release tag to check if it's the same
struct InternetHandleCloser { void operator()(void* h) { WinHttpCloseHandle(h); } };
if (AutoHandle<InternetHandleCloser> internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0))
if (AutoHandle<InternetHandleCloser> connection = WinHttpConnect(internet, L"api.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
if (AutoHandle<InternetHandleCloser> request = WinHttpOpenRequest(connection, L"GET", L"/repos/Artikash/Textractor/releases", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
{
DWORD bytesRead;
char buffer[1000] = {};
WinHttpReceiveResponse(request, NULL);
WinHttpReadData(request, buffer, 1000, &bytesRead);
if (abs(strstr(buffer, "/tag/") - strstr(buffer, CURRENT_VERSION)) > 10) Host::AddConsoleOutput(UPDATE_AVAILABLE);
}
}).detach();
2018-07-22 06:40:16 +08:00
}
MainWindow::~MainWindow()
{
2018-12-19 01:14:54 +08:00
QSettings settings(CONFIG_FILE, QSettings::IniFormat);
2018-11-10 18:13:59 +08:00
settings.setValue(WINDOW, geometry());
2018-09-22 10:25:37 +08:00
settings.sync();
2018-12-28 07:52:59 +08:00
SetErrorMode(SEM_NOGPFAULTERRORBOX);
2018-12-04 07:31:00 +08:00
ExitProcess(0);
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
}
void MainWindow::ProcessConnected(DWORD processId)
2018-07-24 13:57:54 +08:00
{
if (processId == 0) return;
2018-12-19 01:55:11 +08:00
QMetaObject::invokeMethod(this, [this, processId]
{
QString process = S(Util::GetModuleFilename(processId).value());
ui->processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName());
QStringList allProcesses = QString(QAutoFile(HOOK_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split("\r", QString::SkipEmptyParts);
// Can't use QFileInfo::absoluteFilePath since hook save file has '\\' as path separator
auto hookList = std::find_if(allProcesses.rbegin(), allProcesses.rend(), [&](QString hookList) { return hookList.contains(process); });
if (hookList != allProcesses.rend())
for (auto hookCode : hookList->split(" , "))
if (auto hp = ParseCode(hookCode)) Host::InsertHook(processId, hp.value());
});
2018-07-24 13:57:54 +08:00
}
void MainWindow::ProcessDisconnected(DWORD processId)
2018-07-24 13:57:54 +08:00
{
2018-12-19 01:55:11 +08:00
QMetaObject::invokeMethod(this, [this, processId] { ui->processCombo->removeItem(ui->processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith)); });
2018-07-24 13:57:54 +08:00
}
void MainWindow::ThreadAdded(TextThread* thread)
2018-07-24 13:57:54 +08:00
{
QString ttString = TextThreadString(thread) + S(thread->name) + " (" + GenerateCode(thread->hp, thread->tp.processId) + ")";
2018-12-19 01:55:11 +08:00
QMetaObject::invokeMethod(this, [this, ttString] { ui->ttCombo->addItem(ttString); });
}
void MainWindow::ThreadRemoved(TextThread* thread)
{
QString ttString = TextThreadString(thread);
2018-12-19 01:55:11 +08:00
QMetaObject::invokeMethod(this, [this, ttString]
2018-07-26 01:46:59 +08:00
{
int threadIndex = ui->ttCombo->findText(ttString, Qt::MatchStartsWith);
if (threadIndex == ui->ttCombo->currentIndex())
{
ui->ttCombo->setCurrentIndex(0);
2018-12-19 01:14:54 +08:00
ViewThread(0);
}
ui->ttCombo->removeItem(threadIndex);
});
2018-07-24 13:57:54 +08:00
}
bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence)
2018-10-08 12:26:43 +08:00
{
if (DispatchSentenceToExtensions(sentence, GetMiscInfo(thread)))
2018-10-08 12:26:43 +08:00
{
sentence += L"\r\n";
QString ttString = TextThreadString(thread);
2018-12-19 01:55:11 +08:00
QMetaObject::invokeMethod(this, [this, ttString, sentence]
{
if (ui->ttCombo->currentText().startsWith(ttString))
{
ui->textOutput->moveCursor(QTextCursor::End);
ui->textOutput->insertPlainText(S(sentence));
ui->textOutput->moveCursor(QTextCursor::End);
}
});
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();
}
ThreadParam MainWindow::ParseTextThreadString(QString ttString)
2018-08-23 03:11:40 +08:00
{
QStringList threadParam = ttString.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 ui->processCombo->currentText().split(":")[0].toULong(nullptr, 16);
2018-08-23 03:11:40 +08:00
}
2019-01-02 06:50:22 +08:00
std::unordered_map<const char*, int64_t> MainWindow::GetMiscInfo(TextThread* thread)
{
2018-12-29 01:14:56 +08:00
return
{
{ "current select", ui->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-12-19 01:14:54 +08:00
void MainWindow::AttachProcess()
2018-07-23 07:53:51 +08:00
{
QMultiHash<QString, DWORD> allProcesses;
DWORD allProcessIds[5000] = {}, spaceUsed = 0;
EnumProcesses(allProcessIds, sizeof(allProcessIds), &spaceUsed);
for (int i = 0; i < spaceUsed / sizeof(DWORD); ++i)
if (auto processName = Util::GetModuleFilename(allProcessIds[i])) allProcesses.insert(QFileInfo(S(processName.value())).fileName(), allProcessIds[i]);
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
}
2018-12-19 01:14:54 +08:00
void MainWindow::DetachProcess()
2018-07-24 13:57:54 +08:00
{
2018-08-22 10:43:30 +08:00
Host::DetachProcess(GetSelectedProcessId());
2018-07-24 13:57:54 +08:00
}
2018-12-19 01:14:54 +08:00
void MainWindow::AddHook()
2018-07-26 12:48:18 +08:00
{
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
}
2018-12-19 01:14:54 +08:00
void MainWindow::SaveHooks()
2018-07-27 13:42:21 +08:00
{
if (auto processName = Util::GetModuleFilename(GetSelectedProcessId()))
2018-11-28 05:57:47 +08:00
{
QHash<uint64_t, QString> hookCodes;
for (int i = 0; i < ui->ttCombo->count(); ++i)
{
ThreadParam tp = ParseTextThreadString(ui->ttCombo->itemText(i));
if (tp.processId == GetSelectedProcessId() && !(Host::GetHookParam(tp).type & HOOK_ENGINE)) hookCodes[tp.addr] = GenerateCode(Host::GetHookParam(tp), tp.processId);
}
QString hookList = S(processName.value());
for (auto hookCode : hookCodes) hookList += " , " + hookCode;
QAutoFile(HOOK_SAVE_FILE, QIODevice::Append)->write((hookList + "\r\n").toUtf8());
2018-11-28 05:57:47 +08:00
}
2018-07-27 13:42:21 +08:00
}
2018-12-19 01:14:54 +08:00
void MainWindow::Settings()
2018-11-10 18:13:59 +08:00
{
struct : QDialog
{
using QDialog::QDialog;
void launch()
{
auto settings = new QSettings(CONFIG_FILE, QSettings::IniFormat, this);
auto layout = new QFormLayout(this);
auto save = new QPushButton(this);
save->setText(SAVE_SETTINGS);
layout->addWidget(save);
for (auto[value, label] : Array<std::tuple<int&, const char*>>{
{ TextThread::defaultCodepage, DEFAULT_CODEPAGE },
{ TextThread::maxBufferSize, MAX_BUFFER_SIZE },
{ TextThread::flushDelay, FLUSH_DELAY },
})
{
auto spinBox = new QSpinBox(this);
spinBox->setMaximum(INT_MAX);
spinBox->setValue(value);
layout->insertRow(0, label, spinBox);
2019-01-01 04:06:47 +08:00
connect(save, &QPushButton::clicked, [=, &value] { settings->setValue(label, value = spinBox->value()); });
}
connect(save, &QPushButton::clicked, this, &QDialog::accept);
setWindowTitle(SETTINGS);
exec();
}
} settingsDialog(this, Qt::WindowCloseButtonHint);
settingsDialog.launch();
2018-11-10 18:13:59 +08:00
}
2018-12-19 01:14:54 +08:00
void MainWindow::Extensions()
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
2018-12-19 01:14:54 +08:00
void MainWindow::ViewThread(int index)
2018-07-27 13:42:21 +08:00
{
2018-12-19 01:14:54 +08:00
ui->textOutput->setPlainText(S(Host::GetThread(ParseTextThreadString(ui->ttCombo->itemText(index)))->storage->c_str()));
ui->textOutput->moveCursor(QTextCursor::End);
2018-07-27 13:42:21 +08:00
}