2018-07-21 15:40:16 -07:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
2018-11-04 02:13:51 -05:00
|
|
|
#include "text.h"
|
2018-11-01 14:07:42 -04:00
|
|
|
#include "extenwindow.h"
|
2018-11-10 05:13:59 -05:00
|
|
|
#include "setdialog.h"
|
2018-07-25 21:48:18 -07:00
|
|
|
#include "misc.h"
|
2018-12-01 15:55:32 -05:00
|
|
|
#include "host/util.h"
|
2018-12-13 08:37:37 -05:00
|
|
|
#include <Psapi.h>
|
|
|
|
#include <winhttp.h>
|
2018-12-14 22:26:49 -05:00
|
|
|
#include <QFrame>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QPushButton>
|
2018-08-22 15:11:40 -04:00
|
|
|
#include <QInputDialog>
|
2018-07-23 12:25:02 -07:00
|
|
|
|
2018-07-21 15:40:16 -07:00
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
2018-07-25 10:46:59 -07:00
|
|
|
QMainWindow(parent),
|
2018-11-01 10:38:14 -04:00
|
|
|
ui(new Ui::MainWindow),
|
2018-11-10 01:17:02 -05:00
|
|
|
extenWindow(new ExtenWindow(this))
|
2018-07-21 15:40:16 -07:00
|
|
|
{
|
2018-07-25 10:46:59 -07:00
|
|
|
ui->setupUi(this);
|
2018-12-14 23:14:30 -05:00
|
|
|
for (auto[text, slot] : Array<std::tuple<QString, void(MainWindow::*)()>>{
|
2018-12-14 22:26:49 -05:00
|
|
|
{ ATTACH, &MainWindow::on_attachButton_clicked },
|
|
|
|
{ DETACH, &MainWindow::on_detachButton_clicked },
|
|
|
|
{ ADD_HOOK, &MainWindow::on_hookButton_clicked },
|
|
|
|
{ SAVE_HOOKS, &MainWindow::on_saveButton_clicked },
|
|
|
|
{ SETTINGS, &MainWindow::on_setButton_clicked },
|
|
|
|
{ EXTENSIONS, &MainWindow::on_extenButton_clicked }
|
|
|
|
})
|
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
QPushButton* button = new QPushButton(ui->processFrame);
|
2018-12-14 22:26:49 -05:00
|
|
|
connect(button, &QPushButton::clicked, this, slot);
|
|
|
|
button->setText(text);
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->processLayout->addWidget(button);
|
2018-12-14 22:26:49 -05:00
|
|
|
}
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
|
2018-08-21 22:43:30 -04:00
|
|
|
|
2018-11-10 05:13:59 -05:00
|
|
|
if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect());
|
2018-11-04 04:00:14 -05: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 05:13:59 -05:00
|
|
|
if (settings.contains(DEFAULT_CODEPAGE)) TextThread::defaultCodepage = settings.value(DEFAULT_CODEPAGE).toInt();
|
2018-10-28 02:27:24 -04:00
|
|
|
|
2018-08-21 22:43:30 -04:00
|
|
|
Host::Start(
|
2018-11-27 15:54:04 -05:00
|
|
|
[&](DWORD processId) { ProcessConnected(processId); },
|
|
|
|
[&](DWORD processId) { ProcessDisconnected(processId); },
|
|
|
|
[&](TextThread* thread) { ThreadAdded(thread); },
|
|
|
|
[&](TextThread* thread) { ThreadRemoved(thread); },
|
|
|
|
[&](TextThread* thread, std::wstring& output) { return SentenceReceived(thread, output); }
|
2018-08-21 22:43:30 -04:00
|
|
|
);
|
2018-11-04 02:13:51 -05:00
|
|
|
Host::AddConsoleOutput(ABOUT);
|
2018-12-13 08:37:37 -05:00
|
|
|
|
|
|
|
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-21 15:40:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
2018-11-10 05:13:59 -05:00
|
|
|
settings.setValue(WINDOW, geometry());
|
2018-09-21 22:25:37 -04:00
|
|
|
settings.sync();
|
2018-11-27 15:54:04 -05:00
|
|
|
delete ui;
|
2018-12-03 18:31:00 -05:00
|
|
|
ExitProcess(0);
|
2018-07-21 15:40:16 -07:00
|
|
|
}
|
2018-07-22 16:53:51 -07:00
|
|
|
|
2018-11-01 18:47:46 -04:00
|
|
|
void MainWindow::closeEvent(QCloseEvent*)
|
|
|
|
{
|
|
|
|
QCoreApplication::quit(); // Need to do this to kill any windows that might've been made by extensions
|
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
|
2018-12-01 15:55:32 -05:00
|
|
|
void MainWindow::InvokeOnMainThread(std::function<void()> f)
|
2018-07-23 22:57:54 -07:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
QMetaObject::invokeMethod(this, f);
|
2018-07-23 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
void MainWindow::ProcessConnected(DWORD processId)
|
2018-07-23 22:57:54 -07:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
if (processId == 0) return;
|
|
|
|
InvokeOnMainThread([&, processId]
|
|
|
|
{
|
2018-12-13 08:37:37 -05:00
|
|
|
QString process = S(Util::GetModuleFileName(processId).value());
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName());
|
2018-12-01 15:55:32 -05:00
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
QStringList allProcesses = QString(QAutoFile(HOOK_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split("\r", QString::SkipEmptyParts);
|
2018-12-01 15:55:32 -05:00
|
|
|
// 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-11-27 15:54:04 -05:00
|
|
|
});
|
2018-07-23 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
void MainWindow::ProcessDisconnected(DWORD processId)
|
2018-07-23 22:57:54 -07:00
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
InvokeOnMainThread([&, processId] { ui->processCombo->removeItem(ui->processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith)); });
|
2018-07-23 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
void MainWindow::ThreadAdded(TextThread* thread)
|
2018-07-23 22:57:54 -07:00
|
|
|
{
|
2018-12-13 08:37:37 -05:00
|
|
|
QString ttString = TextThreadString(thread) + S(thread->name) + " (" + GenerateCode(thread->hp, thread->tp.processId) + ")";
|
2018-12-14 23:14:30 -05:00
|
|
|
InvokeOnMainThread([&, ttString] { ui->ttCombo->addItem(ttString); });
|
2018-07-24 10:39:02 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
void MainWindow::ThreadRemoved(TextThread* thread)
|
2018-07-24 10:39:02 -07:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
QString ttString = TextThreadString(thread);
|
|
|
|
InvokeOnMainThread([&, ttString]
|
2018-07-25 10:46:59 -07:00
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
int threadIndex = ui->ttCombo->findText(ttString, Qt::MatchStartsWith);
|
|
|
|
if (threadIndex == ui->ttCombo->currentIndex())
|
2018-11-27 15:54:04 -05:00
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->ttCombo->setCurrentIndex(0);
|
2018-11-27 15:54:04 -05:00
|
|
|
on_ttCombo_activated(0);
|
|
|
|
}
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->ttCombo->removeItem(threadIndex);
|
2018-11-27 15:54:04 -05:00
|
|
|
});
|
2018-07-23 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence)
|
2018-10-08 00:26:43 -04:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
if (DispatchSentenceToExtensions(sentence, GetMiscInfo(thread)))
|
2018-10-08 00:26:43 -04:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
sentence += L"\r\n";
|
|
|
|
QString ttString = TextThreadString(thread);
|
|
|
|
InvokeOnMainThread([&, ttString, sentence]
|
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
if (ui->ttCombo->currentText().startsWith(ttString))
|
2018-11-27 15:54:04 -05:00
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->textOutput->moveCursor(QTextCursor::End);
|
|
|
|
ui->textOutput->insertPlainText(S(sentence));
|
|
|
|
ui->textOutput->moveCursor(QTextCursor::End);
|
2018-11-27 15:54:04 -05:00
|
|
|
}
|
|
|
|
});
|
2018-10-08 00:26:43 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-22 15:11:40 -04:00
|
|
|
QString MainWindow::TextThreadString(TextThread* thread)
|
|
|
|
{
|
2018-09-20 22:32:47 -04:00
|
|
|
ThreadParam tp = thread->tp;
|
2018-09-21 22:25:37 -04:00
|
|
|
return QString("%1:%2:%3:%4:%5: ").arg(
|
2018-09-22 17:13:53 -04:00
|
|
|
QString::number(thread->handle, 16),
|
2018-11-04 20:48:46 -05:00
|
|
|
QString::number(tp.processId, 16),
|
|
|
|
QString::number(tp.addr, 16),
|
|
|
|
QString::number(tp.ctx, 16),
|
|
|
|
QString::number(tp.ctx2, 16)
|
2018-08-22 15:11:40 -04:00
|
|
|
).toUpper();
|
|
|
|
}
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
ThreadParam MainWindow::ParseTextThreadString(QString ttString)
|
2018-08-22 15:11:40 -04:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
QStringList threadParam = ttString.split(":");
|
2018-09-22 17:13:06 -04:00
|
|
|
return { threadParam[1].toUInt(nullptr, 16), threadParam[2].toULongLong(nullptr, 16), threadParam[3].toULongLong(nullptr, 16), threadParam[4].toULongLong(nullptr, 16) };
|
2018-08-22 15:11:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD MainWindow::GetSelectedProcessId()
|
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
return ui->processCombo->currentText().split(":")[0].toULong(nullptr, 16);
|
2018-08-22 15:11:40 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 14:07:42 -04:00
|
|
|
std::unordered_map<std::string, int64_t> MainWindow::GetMiscInfo(TextThread* thread)
|
2018-08-04 03:16:14 -04:00
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
{ "current select", ui->ttCombo->currentText().startsWith(TextThreadString(thread)) },
|
2018-09-22 17:13:06 -04:00
|
|
|
{ "text number", thread->handle },
|
2018-11-04 20:48:46 -05:00
|
|
|
{ "process id", thread->tp.processId },
|
|
|
|
{ "hook address", thread->tp.addr },
|
2018-09-22 17:13:06 -04:00
|
|
|
{ "text handle", thread->handle },
|
2018-09-20 22:32:47 -04:00
|
|
|
{ "text name", (int64_t)thread->name.c_str() }
|
2018-08-04 03:16:14 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:25:02 -07:00
|
|
|
void MainWindow::on_attachButton_clicked()
|
2018-07-22 16:53:51 -07:00
|
|
|
{
|
2018-12-13 08:37:37 -05: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-09 22:37:48 -04:00
|
|
|
QStringList processList(allProcesses.uniqueKeys());
|
2018-08-20 14:30:50 -04:00
|
|
|
processList.sort(Qt::CaseInsensitive);
|
2018-07-25 21:48:18 -07:00
|
|
|
bool ok;
|
2018-11-19 08:17:00 -05:00
|
|
|
QString process = QInputDialog::getItem(this, SELECT_PROCESS, ATTACH_INFO, processList, 0, true, &ok, Qt::WindowCloseButtonHint);
|
2018-07-30 20:25:08 -07:00
|
|
|
if (!ok) return;
|
2018-11-04 02:13:51 -05:00
|
|
|
if (process.toInt(nullptr, 0)) Host::InjectProcess(process.toInt(nullptr, 0));
|
|
|
|
else for (auto processId : allProcesses.values(process)) Host::InjectProcess(processId);
|
2018-07-23 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_detachButton_clicked()
|
|
|
|
{
|
2018-08-21 22:43:30 -04:00
|
|
|
Host::DetachProcess(GetSelectedProcessId());
|
2018-07-23 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
2018-07-25 21:48:18 -07:00
|
|
|
void MainWindow::on_hookButton_clicked()
|
|
|
|
{
|
|
|
|
bool ok;
|
2018-11-04 04:31:49 -05:00
|
|
|
QString hookCode = QInputDialog::getText(this, ADD_HOOK, CODE_INFODUMP, QLineEdit::Normal, "", &ok, Qt::WindowCloseButtonHint);
|
2018-07-30 20:25:08 -07:00
|
|
|
if (!ok) return;
|
2018-08-24 14:24:46 -04:00
|
|
|
if (auto hp = ParseCode(hookCode)) Host::InsertHook(GetSelectedProcessId(), hp.value());
|
2018-11-04 02:13:51 -05:00
|
|
|
else Host::AddConsoleOutput(INVALID_CODE);
|
2018-07-25 21:48:18 -07:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:42:21 -07:00
|
|
|
void MainWindow::on_saveButton_clicked()
|
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
if (auto processName = Util::GetModuleFileName(GetSelectedProcessId()))
|
2018-11-27 16:57:47 -05:00
|
|
|
{
|
2018-12-14 23:14:30 -05: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-27 16:57:47 -05:00
|
|
|
}
|
2018-07-26 22:42:21 -07:00
|
|
|
}
|
|
|
|
|
2018-11-10 05:13:59 -05:00
|
|
|
void MainWindow::on_setButton_clicked()
|
|
|
|
{
|
|
|
|
SetDialog(this).exec();
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:02:52 -04:00
|
|
|
void MainWindow::on_extenButton_clicked()
|
2018-07-23 22:57:54 -07:00
|
|
|
{
|
2018-11-01 17:02:52 -04:00
|
|
|
extenWindow->activateWindow();
|
|
|
|
extenWindow->showNormal();
|
2018-07-22 16:53:51 -07:00
|
|
|
}
|
2018-07-26 22:42:21 -07:00
|
|
|
|
2018-11-01 17:02:52 -04:00
|
|
|
void MainWindow::on_ttCombo_activated(int index)
|
2018-07-26 22:42:21 -07:00
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->textOutput->setPlainText(S(Host::GetThread(ParseTextThreadString(ui->ttCombo->itemText(index)))->GetStorage()));
|
|
|
|
ui->textOutput->moveCursor(QTextCursor::End);
|
2018-07-26 22:42:21 -07:00
|
|
|
}
|