fix freeze when connecting process, crash when process name cant be found, and other refactors

This commit is contained in:
Akash Mozumdar 2019-01-10 21:47:16 -05:00
parent cdfbd77d21
commit 5903bbe2e4
5 changed files with 26 additions and 26 deletions

View File

@ -1,6 +1,7 @@
include(QtUtils) include(QtUtils)
msvc_registry_search() msvc_registry_search()
find_qt5(Core Widgets) find_qt5(Core Widgets)
set(AUTOMOC OFF)
set(RESOURCE_FILES Textractor.rc Textractor.ico) set(RESOURCE_FILES Textractor.rc Textractor.ico)
add_compile_options(/GL) add_compile_options(/GL)

View File

@ -87,7 +87,7 @@ ExtenWindow::ExtenWindow(QWidget* parent) :
ui->extenList->installEventFilter(this); ui->extenList->installEventFilter(this);
for (auto extenName : QString(QAutoFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split(">")) Load(extenName); for (auto extenName : QString(QTextFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly).readAll()).split(">")) Load(extenName);
Sync(); Sync();
} }
@ -99,12 +99,12 @@ ExtenWindow::~ExtenWindow()
void ExtenWindow::Sync() void ExtenWindow::Sync()
{ {
ui->extenList->clear(); ui->extenList->clear();
QAutoFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate); QTextFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate);
std::shared_lock readLock(extenMutex); std::shared_lock readLock(extenMutex);
for (auto extenName : extenNames) for (auto extenName : extenNames)
{ {
ui->extenList->addItem(extenName); ui->extenList->addItem(extenName);
extenSaveFile->write((extenName + ">").toUtf8()); extenSaveFile.write((extenName + ">").toUtf8());
} }
} }

View File

@ -16,7 +16,7 @@ namespace
processId(processId), processId(processId),
pipe(pipe), pipe(pipe),
mappedFile(OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(processId)).c_str())), mappedFile(OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(processId)).c_str())),
view(MapViewOfFile(mappedFile, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2)), // jichi 1/16/2015: Changed to half to hook section size view(*(const TextHook(*)[MAX_HOOK])MapViewOfFile(mappedFile, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2)), // jichi 1/16/2015: Changed to half to hook section size
viewMutex(ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId)) viewMutex(ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId))
{ {
OnConnect(processId); OnConnect(processId);
@ -32,24 +32,26 @@ namespace
{ {
if (view == nullptr) return {}; if (view == nullptr) return {};
std::scoped_lock lock(viewMutex); std::scoped_lock lock(viewMutex);
auto hooks = (const TextHook*)view; for (auto hook : view)
for (int i = 0; i < MAX_HOOK; ++i) if (hook.address == addr) return hook;
if (hooks[i].address == addr) return hooks[i];
return {}; return {};
} }
template <typename T> template <typename T>
void Send(T data) void Send(T data)
{
std::thread([=]
{ {
std::enable_if_t<sizeof(data) < PIPE_BUFFER_SIZE, DWORD> DUMMY; std::enable_if_t<sizeof(data) < PIPE_BUFFER_SIZE, DWORD> DUMMY;
WriteFile(pipe, &data, sizeof(data), &DUMMY, nullptr); WriteFile(pipe, &data, sizeof(data), &DUMMY, nullptr);
}).detach();
} }
private: private:
DWORD processId; DWORD processId;
HANDLE pipe; HANDLE pipe;
AutoHandle<> mappedFile; AutoHandle<> mappedFile;
LPCVOID view; const TextHook(&view)[MAX_HOOK];
WinMutex viewMutex; WinMutex viewMutex;
}; };
@ -78,7 +80,6 @@ namespace
SetSecurityDescriptorDacl(&pipeSD, TRUE, NULL, FALSE); // Allow non-admin processes to connect to pipe created by admin host SetSecurityDescriptorDacl(&pipeSD, TRUE, NULL, FALSE); // Allow non-admin processes to connect to pipe created by admin host
SECURITY_ATTRIBUTES pipeSA = { sizeof(pipeSA), &pipeSD, FALSE }; SECURITY_ATTRIBUTES pipeSA = { sizeof(pipeSA), &pipeSD, FALSE };
struct PipeCloser { void operator()(HANDLE h) { DisconnectNamedPipe(h); CloseHandle(h); } }; struct PipeCloser { void operator()(HANDLE h) { DisconnectNamedPipe(h); CloseHandle(h); } };
AutoHandle<PipeCloser> AutoHandle<PipeCloser>
hookPipe = CreateNamedPipeW(HOOK_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 0, PIPE_BUFFER_SIZE, MAXDWORD, &pipeSA), hookPipe = CreateNamedPipeW(HOOK_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 0, PIPE_BUFFER_SIZE, MAXDWORD, &pipeSA),

View File

@ -34,7 +34,7 @@ MainWindow::MainWindow(QWidget *parent) :
} }
ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding)); ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
connect(ui->ttCombo, (void(QComboBox::*)(int))&QComboBox::activated, this, &MainWindow::ViewThread); connect(ui->ttCombo, qOverload<int>(&QComboBox::activated), this, &MainWindow::ViewThread);
QSettings settings(CONFIG_FILE, QSettings::IniFormat); QSettings settings(CONFIG_FILE, QSettings::IniFormat);
if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect()); if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect());
@ -88,10 +88,10 @@ void MainWindow::ProcessConnected(DWORD processId)
if (processId == 0) return; if (processId == 0) return;
QMetaObject::invokeMethod(this, [this, processId] QMetaObject::invokeMethod(this, [this, processId]
{ {
QString process = S(Util::GetModuleFilename(processId).value()); QString process = S(Util::GetModuleFilename(processId).value_or(L"???"));
ui->processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName()); 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); QStringList allProcesses = QString(QTextFile(HOOK_SAVE_FILE, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts);
// Can't use QFileInfo::absoluteFilePath since hook save file has '\\' as path separator // 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); }); auto hookList = std::find_if(allProcesses.rbegin(), allProcesses.rend(), [&](QString hookList) { return hookList.contains(process); });
if (hookList != allProcesses.rend()) if (hookList != allProcesses.rend())
@ -136,7 +136,7 @@ bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence)
{ {
if (DispatchSentenceToExtensions(sentence, GetMiscInfo(thread))) if (DispatchSentenceToExtensions(sentence, GetMiscInfo(thread)))
{ {
sentence += L"\r\n"; sentence += L"\n";
QString ttString = TextThreadString(thread); QString ttString = TextThreadString(thread);
QMetaObject::invokeMethod(this, [this, ttString, sentence] QMetaObject::invokeMethod(this, [this, ttString, sentence]
{ {
@ -227,11 +227,13 @@ void MainWindow::SaveHooks()
for (int i = 0; i < ui->ttCombo->count(); ++i) for (int i = 0; i < ui->ttCombo->count(); ++i)
{ {
ThreadParam tp = ParseTextThreadString(ui->ttCombo->itemText(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); if (tp.processId == GetSelectedProcessId())
{
HookParam hp = Host::GetHookParam(tp);
if (!(hp.type & HOOK_ENGINE)) hookCodes[tp.addr] = GenerateCode(hp, tp.processId);
} }
QString hookList = S(processName.value()); }
for (auto hookCode : hookCodes) hookList += " , " + hookCode; QTextFile(HOOK_SAVE_FILE, QIODevice::Append).write((S(processName.value()) + " , " + QStringList(hookCodes.values()).join(" , ") + "\n").toUtf8());
QAutoFile(HOOK_SAVE_FILE, QIODevice::Append)->write((hookList + "\r\n").toUtf8());
} }
} }

View File

@ -3,14 +3,10 @@
#include "qtcommon.h" #include "qtcommon.h"
#include "types.h" #include "types.h"
class QAutoFile struct QTextFile : QFile
{ {
public: using QFile::QFile;
QAutoFile(const QString& name, QIODevice::OpenMode mode) : f(name) { f.open(mode); } QTextFile(const QString& name, QIODevice::OpenMode mode) : QFile(name) { open(mode | QIODevice::Text); }
QFile* operator->() { return &f; }
private:
QFile f;
}; };
inline std::wstring S(const QString& S) { return { S.toStdWString() }; } inline std::wstring S(const QString& S) { return { S.toStdWString() }; }