Skip to content

Commit

Permalink
Optimize memory read write and section code.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackINT3 committed Aug 25, 2020
1 parent cf3ae05 commit 3da8e17
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 108 deletions.
20 changes: 14 additions & 6 deletions src/OpenArk/kernel/memory/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void KernelMemory::ModuleInit(Ui::Kernel *mainui, Kernel *kernel)

KernelMemoryRW::KernelMemoryRW()
{
maxsize_ = -1;
QUiLoader loader;
QFile file(":/UI/ui/memory-rw.ui");
file.open(QFile::ReadOnly);
Expand Down Expand Up @@ -92,7 +93,7 @@ KernelMemoryRW::KernelMemoryRW()

std::string buf;
if (!ArkDrvApi::Memory::MemoryRead(pid, addr, size, buf)) {
LabelError(statusLabel, tr("Read Memory error, addr:%1 size:%2").arg(QString::number(addr, 16).toUpper()).arg(size));
LabelError(statusLabel, tr("Read Memory error, addr:0x%1 size:0x%2").arg(QString::number(addr, 16).toUpper()).arg(QString::number(size, 16).toUpper()));
return;
}

Expand Down Expand Up @@ -144,14 +145,21 @@ void KernelMemoryRW::ViewMemory(ULONG pid, ULONG64 addr, ULONG size)
DEFINE_WIDGET(QLineEdit*, pidEdit);
pidEdit->setText(QString::number(pid));

if (ArkDrvApi::Memory::MemoryRead(pid, addr, size, buf)) {

auto minsize = MIN(maxsize_, size);
if (ArkDrvApi::Memory::MemoryRead(pid, addr, minsize, buf)) {
mem = (char*)buf.c_str();
memsize = buf.size();
readok = true;
}

auto hexdump = HexDumpMemory(addr, mem, size);
auto disasm = DisasmMemory(addr, mem, size);
auto hexdump = HexDumpMemory(addr, mem, minsize);
if (maxsize_ != -1 && size > maxsize_) {
auto delta = size - maxsize_;
auto hexdump2 = HexDumpMemory(addr+size, nullptr, size-maxsize_);
hexdump.append(hexdump2);
}
auto disasm = DisasmMemory(addr, mem, minsize);

DEFINE_WIDGET(QTextEdit*, hexEdit);
DEFINE_WIDGET(QTextEdit*, disasmEdit);
Expand All @@ -170,8 +178,8 @@ void KernelMemoryRW::ViewMemory(ULONG pid, ULONG64 addr, ULONG size)
}
}
regionLabel->setText(path);
readok ? LabelSuccess(statusLabel, tr("Read Memory successfully, addr:%1 size:%2").arg(QString::number(addr, 16).toUpper()).arg(size)) :
LabelError(statusLabel, tr("Read Memory error, addr:%1 size:%2").arg(QString::number(addr, 16).toUpper()).arg(size));
readok ? LabelSuccess(statusLabel, tr("Read Memory successfully, addr:0x%1 size:0x%2").arg(QString::number(addr, 16).toUpper()).arg(QString::number(size, 16).toUpper())) :
LabelError(statusLabel, tr("Read Memory error, addr:0x%1 size:0x%2").arg(QString::number(addr, 16).toUpper()).arg(QString::number(size, 16).toUpper()));
}

void KernelMemoryRW::ViewMemory(ULONG pid, std::string data)
Expand Down
6 changes: 4 additions & 2 deletions src/OpenArk/kernel/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@ class KernelMemoryRW : public QWidget {
QWidget *memui_;
std::function<void(QList<QVariant>)> free_callback_;
QList<QVariant> free_vars_;
ULONG maxsize_;

public:
void RegFreeCallback(std::function<void(QList<QVariant>)> callback, QList<QVariant> vars) {
free_callback_ = callback;
free_vars_ = vars;
};
void SetMaxSize(ULONG maxsize) { maxsize_ = maxsize; };
void ViewMemory(ULONG pid, ULONG64 addr, ULONG size);
void ViewMemory(ULONG pid, std::string data);
void WriteMemory(std::string data);
void OpenNewWindow(QWidget *parent, ULONG64 addr, ULONG size)
{
auto memwidget = this->GetWidget();
memwidget->findChild<QLineEdit*>("readAddrEdit")->setText(QString::number(addr,16).toUpper());
memwidget->findChild<QLineEdit*>("readSizeEdit")->setText(DWordToHexQ(size));
memwidget->findChild<QLineEdit*>("readAddrEdit")->setText(QString("0x%1").arg(QString::number(addr,16).toUpper()));
memwidget->findChild<QLineEdit*>("readSizeEdit")->setText(QString("0x%1").arg(QString::number(size, 16).toUpper()));
memwidget->setParent(parent);
memwidget->setWindowTitle(tr("Memory Read-Write"));
memwidget->setWindowFlags(Qt::Window);
Expand Down
1 change: 1 addition & 0 deletions src/OpenArk/kernel/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void KernelObject::InitObjectSectionsView()
UnmapViewOfFile(addr);
CloseHandle(hd);
}, vars);
memrw->SetMaxSize(map_size);
map_size = MIN(map_size, PAGE_SIZE);
memrw->ViewMemory(GetCurrentProcessId(), map_addr, map_size);
memrw->OpenNewWindow(qobject_cast<QWidget*>(this->parent()), map_addr, map_size);
Expand Down
Loading

0 comments on commit 3da8e17

Please sign in to comment.