首页
社区
课程
招聘
[求助]QT程序如何再加一列?
发表于: 2023-9-19 09:23 3882

[求助]QT程序如何再加一列?

2023-9-19 09:23
3882

如图所示:

只需要关注 .1337的行


越看越不顺眼,于是问题来: 能不能再加一个注释列?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#include "PatchDialog.h"
#include "ui_PatchDialog.h"
#include <QMessageBox>
#include <QIcon>
#include <QFileDialog>
#include <QTextStream>
#include "MiscUtil.h"
#include "StringUtil.h"
#include "Configuration.h"
#include <QClipboard>
  
PatchDialog::PatchDialog(QWidget* parent) :
    QDialog(parent),
    ui(new Ui::PatchDialog)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    setModal(false); //non-modal window
  
    connect(Bridge::getBridge(), SIGNAL(updatePatches()), this, SLOT(updatePatches()));
    connect(Bridge::getBridge(), SIGNAL(dbgStateChanged(DBGSTATE)), this, SLOT(dbgStateChanged(DBGSTATE)));
  
    mGroupSelector = new PatchDialogGroupSelector(parent);
    mGroupSelector->setGroup(0);
    connect(mGroupSelector, SIGNAL(rejected()), this, SLOT(showNormal()));
    connect(mGroupSelector, SIGNAL(rejected()), this, SLOT(setFocus()));
    connect(mGroupSelector, SIGNAL(groupToggle()), this, SLOT(groupToggle()));
    connect(mGroupSelector, SIGNAL(groupPrevious()), this, SLOT(groupPrevious()));
    connect(mGroupSelector, SIGNAL(groupNext()), this, SLOT(groupNext()));
  
    mIsWorking = false;
}
  
PatchDialog::~PatchDialog()
{
    delete ui;
}
  
bool PatchDialog::isPartOfPreviousGroup(const PatchInfoList & patchList, int index)
{
    if(!index)
        return true;
    uint addr = patchList.at(index).patch.addr;
    uint prevAddr = patchList.at(index - 1).patch.addr;
    for(int i = 1; i < 10; i++) //10 bytes in between groups
        if(addr - i == prevAddr)
            return true;
    return false;
}
  
bool PatchDialog::isGroupEnabled(const PatchInfoList & patchList, int group)
{
    for(int i = 0; i < patchList.size(); i++)
        if(patchList.at(i).status.group == group && !patchList.at(i).status.checked)
            return false;
    return true;
}
  
bool PatchDialog::hasPreviousGroup(const PatchInfoList & patchList, int group)
{
    for(int i = 0; i < patchList.size(); i++)
        if(patchList.at(i).status.group < group)
            return true;
    return false;
}
  
bool PatchDialog::hasNextGroup(const PatchInfoList & patchList, int group)
{
    for(int i = 0; i < patchList.size(); i++)
        if(patchList.at(i).status.group > group)
            return true;
    return false;
}
  
dsint PatchDialog::getGroupAddress(const PatchInfoList & patchList, int group)
{
    for(int i = 0; i < patchList.size(); i++)
        if(patchList.at(i).status.group == group)
            return patchList.at(i).patch.addr;
    return -1;
}
  
void PatchDialog::dbgStateChanged(DBGSTATE state)
{
    if(state == stopped)
    {
        mGroupSelector->hide();
        reject();
    }
}
  
void PatchDialog::updatePatches()
{
    if(this->isVisible())
        mGroupSelector->reject();
    mIsWorking = true;
    //clear GUI
    ui->listModules->clear();
    ui->listPatches->clear();
    mPatches.clear();
  
    //get patches from DBG
    size_t cbsize;
    if(!DbgFunctions()->PatchEnum || !DbgFunctions()->PatchEnum(0, &cbsize))
        return;
    int numPatches = (int)cbsize / sizeof(DBGPATCHINFO);
    if(!numPatches)
        return;
    DBGPATCHINFO* patches = new DBGPATCHINFO[numPatches];
    memset(patches, 0, numPatches * sizeof(DBGPATCHINFO));
    if(!DbgFunctions()->PatchEnum(patches, 0))
    {
        delete [] patches;
        mIsWorking = false;
        return;
    }
  
    //fill the patch list
    STATUSINFO defaultStatus;
    defaultStatus.group = 0;
    defaultStatus.checked = true;
    for(int i = 0; i < numPatches; i++)
    {
        if(!patches[i].addr)
            continue;
        if(!*patches[i].mod)
            continue;
        QString mod = patches[i].mod;
        PatchMap::iterator found = mPatches.find(mod);
        if(found != mPatches.end()) //found
            mPatches[mod].append(PatchPair(patches[i], defaultStatus));
        else //not found
        {
            PatchInfoList patchList;
            patchList.append(PatchPair(patches[i], defaultStatus));
            mPatches.insert(mod, patchList);
        }
    }
    delete [] patches;
  
    //sort the patches by address
    for(PatchMap::iterator i = mPatches.begin(); i != mPatches.end(); ++i)
    {
        qSort(i.value().begin(), i.value().end(), PatchInfoLess);
        PatchInfoList & curPatchList = i.value();
        //group the patched bytes
        for(int j = 0, group = 0; j < curPatchList.size(); j++)
        {
            if(!isPartOfPreviousGroup(curPatchList, j))
                group++;
            curPatchList[j].status.group = group;
            unsigned char byte;
            if(DbgMemRead(curPatchList[j].patch.addr, &byte, sizeof(byte)))
                curPatchList[j].status.checked = byte == curPatchList[j].patch.newbyte;
        }
        ui->listModules->addItem(i.key());
    }
  
    if(mPatches.size())
        ui->listModules->item(0)->setSelected(true); //select first module
  
    mIsWorking = false;
}
  
void PatchDialog::groupToggle()
{
    int group = mGroupSelector->group();
    if(mIsWorking || !ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    PatchInfoList & curPatchList = found.value();
    bool enabled = !isGroupEnabled(curPatchList, group);
    Qt::CheckState checkState = enabled ? Qt::Checked : Qt::Unchecked;
    mIsWorking = true;
    for(int i = 0; i < curPatchList.size(); i++)
    {
        if(curPatchList.at(i).status.group != group)
            continue;
        ui->listPatches->item(i)->setCheckState(checkState);
        curPatchList[i].status.checked = enabled;
        //change the byte to reflect the change for the user (cypherpunk reported this)
        unsigned char writebyte = curPatchList[i].status.checked ? curPatchList[i].patch.newbyte : curPatchList[i].patch.oldbyte;
        DbgMemWrite(curPatchList[i].patch.addr, &writebyte, sizeof(writebyte));
    }
    mIsWorking = false;
    dsint groupStart = getGroupAddress(curPatchList, group);
    GuiUpdateAllViews();
    if(!groupStart)
        return;
    QString color = enabled ? "#00DD00" "red";
    QString addrText = ToPtrString(groupStart);
    QString title = "<font color='" + color + "'><b>" + QString().sprintf("%d:", group) + addrText + "</b></font>";
    mGroupSelector->setGroupTitle(title);
}
  
void PatchDialog::groupPrevious()
{
    int group = mGroupSelector->group();
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    PatchInfoList & curPatchList = found.value();
    if(!hasPreviousGroup(curPatchList, group))
        return;
    group--;
    QString color = isGroupEnabled(curPatchList, group) ? "#00DD00" "red";
    QString addrText = ToPtrString(getGroupAddress(curPatchList, group));
    QString title = "<font color='" + color + "'><b>" + QString().sprintf("%d:", group) + addrText + "</b></font>";
    mGroupSelector->setGroupTitle(title);
    mGroupSelector->setGroup(group);
    mGroupSelector->setPreviousEnabled(hasPreviousGroup(curPatchList, group));
    mGroupSelector->setNextEnabled(hasNextGroup(curPatchList, group));
    mGroupSelector->showNormal();
    DbgCmdExecDirect(QString("disasm " + addrText));
    DbgCmdExecDirect(QString("dump " + addrText));
}
  
void PatchDialog::groupNext()
{
    int group = mGroupSelector->group();
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    PatchInfoList & curPatchList = found.value();
    if(!hasNextGroup(curPatchList, group))
        return;
    group++;
    QString color = isGroupEnabled(curPatchList, group) ? "#00DD00" "red";
    QString addrText = ToPtrString(getGroupAddress(curPatchList, group));
    QString title = "<font color='" + color + "'><b>" + QString().sprintf("%d:", group) + addrText + "</b></font>";
    mGroupSelector->setGroupTitle(title);
    mGroupSelector->setGroup(group);
    mGroupSelector->setPreviousEnabled(hasPreviousGroup(curPatchList, group));
    mGroupSelector->setNextEnabled(hasNextGroup(curPatchList, group));
    mGroupSelector->showNormal();
    DbgCmdExecDirect(QString("disasm " + addrText));
    DbgCmdExecDirect(QString("dump " + addrText));
}
  
void PatchDialog::on_listModules_itemSelectionChanged()
{
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod(ui->listModules->selectedItems().at(0)->text());
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    mIsWorking = true;
    PatchInfoList & patchList = found.value();
    ui->listPatches->clear();
    for(int i = 0; i < patchList.size(); i++)
    {
        const DBGPATCHINFO curPatch = patchList.at(i).patch;
        QString addrText = ToPtrString(curPatch.addr);
        QListWidgetItem* item = new QListWidgetItem(QString().sprintf("%d", patchList.at(i).status.group).rightJustified(4, ' 'true) + "|" + addrText + QString().sprintf(":%.2X->%.2X", curPatch.oldbyte, curPatch.newbyte), ui->listPatches);
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        Qt::CheckState state = patchList.at(i).status.checked ? Qt::Checked : Qt::Unchecked;
        item->setCheckState(state);
        if(DbgFunctions()->ModRelocationAtAddr(patchList.at(i).patch.addr, nullptr))
        {
            item->setTextColor(ConfigColor("PatchRelocatedByteHighlightColor"));
            item->setToolTip(tr("Byte is located in relocation region"));
        }
    }
    mIsWorking = false;
}
  
void PatchDialog::on_listPatches_itemChanged(QListWidgetItem* item) //checkbox changed
{
    if(mIsWorking || !ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    bool checked = item->checkState() == Qt::Checked;
    PatchInfoList & curPatchList = found.value();
    PatchPair & patch = curPatchList[ui->listPatches->row(item)];
    if(patch.status.checked == checked) //check state did not change
        return;
    patch.status.checked = checked;
    //change the byte to reflect the change for the user (cypherpunk reported this)
    unsigned char writebyte = patch.status.checked ? patch.patch.newbyte : patch.patch.oldbyte;
    DbgMemWrite(patch.patch.addr, &writebyte, sizeof(writebyte));
    //check state changed
    if((QApplication::keyboardModifiers() & Qt::ControlModifier) != Qt::ControlModifier)
    {
        mIsWorking = true;
        //check/uncheck the complete group
        for(int i = 0; i < curPatchList.size(); i++)
            if(curPatchList.at(i).status.group == patch.status.group)
            {
                //change the patch state
                curPatchList[i].status.checked = checked;
                ui->listPatches->item(i)->setCheckState(item->checkState());
                //change the byte to reflect the change for the user (cypherpunk reported this)
                unsigned char writebyte = curPatchList[i].status.checked ? curPatchList[i].patch.newbyte : curPatchList[i].patch.oldbyte;
                DbgMemWrite(curPatchList[i].patch.addr, &writebyte, sizeof(writebyte));
            }
        mIsWorking = false;
    }
    int group = mGroupSelector->group();
    QString color = isGroupEnabled(curPatchList, group) ? "#00DD00" "red";
    QString addrText = ToPtrString(getGroupAddress(curPatchList, group));
    QString title = "<font color='" + color + "'><b>" + QString().sprintf("%d:", group) + addrText + "</b></font>";
    mGroupSelector->setGroupTitle(title);
    mGroupSelector->setPreviousEnabled(hasPreviousGroup(curPatchList, group));
    mGroupSelector->setNextEnabled(hasNextGroup(curPatchList, group));
    GuiUpdateAllViews();
}
  
void PatchDialog::on_btnSelectAll_clicked()
{
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    mIsWorking = true;
    PatchInfoList & curPatchList = found.value();
    for(int i = 0; i < curPatchList.size(); i++)
    {
        ui->listPatches->item(i)->setCheckState(Qt::Checked);
        curPatchList[i].status.checked = true;
        //change the byte to reflect the change for the user (cypherpunk reported this)
        DbgMemWrite(curPatchList[i].patch.addr, &curPatchList[i].patch.newbyte, sizeof(unsigned char));
    }
    GuiUpdateAllViews();
    mIsWorking = false;
}
  
void PatchDialog::on_btnDeselectAll_clicked()
{
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    mIsWorking = true;
    PatchInfoList & curPatchList = found.value();
    for(int i = 0; i < curPatchList.size(); i++)
    {
        ui->listPatches->item(i)->setCheckState(Qt::Unchecked);
        curPatchList[i].status.checked = false;
        //change the byte to reflect the change for the user (cypherpunk reported this)
        DbgMemWrite(curPatchList[i].patch.addr, &curPatchList[i].patch.oldbyte, sizeof(unsigned char));
    }
    GuiUpdateAllViews();
    mIsWorking = false;
}
  
void PatchDialog::on_btnRestoreSelected_clicked()
{
    if(!ui->listModules->selectedItems().size())
        return;
    int selModIdx = ui->listModules->row(ui->listModules->selectedItems().at(0));
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    mIsWorking = true;
    PatchInfoList & curPatchList = found.value();
    int removed = 0;
    int total = curPatchList.size();
    for(int i = 0; i < total; i++)
    {
        if(curPatchList.at(i).status.checked)
        {
            DbgFunctions()->PatchRestore(curPatchList.at(i).patch.addr);
            removed++;
        }
    }
    mIsWorking = false;
    updatePatches();
    if(removed != total)
        ui->listModules->setCurrentRow(selModIdx);
    GuiUpdateAllViews();
}
  
void PatchDialog::on_listPatches_itemSelectionChanged()
{
    if(!ui->listModules->selectedItems().size() || !ui->listPatches->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    PatchInfoList & curPatchList = found.value();
    PatchPair & patch = curPatchList[ui->listPatches->row(ui->listPatches->selectedItems().at(0))]; //selected item
    dsint groupStart = getGroupAddress(curPatchList, patch.status.group);
    if(!groupStart)
        return;
    QString addrText = ToPtrString(groupStart);
    DbgCmdExecDirect(QString("disasm " + addrText));
    DbgCmdExecDirect(QString("dump " + addrText));
}
  
void PatchDialog::on_btnPickGroups_clicked()
{
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    PatchInfoList & curPatchList = found.value();
    if(!curPatchList.size())
        return;
    this->showMinimized();
  
    int group = mGroupSelector->group();
    QString color = isGroupEnabled(curPatchList, group) ? "#00DD00" "red";
    QString addrText = ToPtrString(getGroupAddress(curPatchList, group));
    QString title = "<font color='" + color + "'><b>" + QString().sprintf("%d:", group) + addrText + "</b></font>";
    mGroupSelector->setGroupTitle(title);
    mGroupSelector->setPreviousEnabled(hasPreviousGroup(curPatchList, group));
    mGroupSelector->setNextEnabled(hasNextGroup(curPatchList, group));
    mGroupSelector->show();
    DbgCmdExecDirect(QString("disasm " + addrText));
    DbgCmdExecDirect(QString("dump " + addrText));
}
  
void PatchDialog::on_btnPatchFile_clicked()
{
    //get current module
    if(!ui->listModules->selectedItems().size())
        return;
    QString mod = ui->listModules->selectedItems().at(0)->text();
    PatchMap::iterator found = mPatches.find(mod);
    if(found == mPatches.end()) //not found
        return;
    PatchInfoList & curPatchList = found.value();
  
    //get patches to save
    QList<DBGPATCHINFO> patchList;
    for(int i = 0; i < curPatchList.size(); i++)
        if(curPatchList.at(i).status.checked)
            patchList.push_back(curPatchList.at(i).patch);
    if(!curPatchList.size() || !patchList.size())
    {
        SimpleInfoBox(this, tr("Information"), tr("Nothing to patch!"));
        return;
    }
  
    if(containsRelocatedBytes(curPatchList) && !showRelocatedBytesWarning())
        return;
  
    char szModName[MAX_PATH] = "";
    if(!DbgFunctions()->ModPathFromAddr(DbgFunctions()->ModBaseFromName(mod.toUtf8().constData()), szModName, MAX_PATH))
    {
        SimpleErrorBox(this, tr("Error!"), tr("Failed to get module filename..."));
        return;
    }
  
    //open the save file dialog
    int len = (int)strlen(szModName);
    while(szModName[len] != '\\')
        len--;
  
    QApplication::clipboard()->setText(szModName); //这样就把内容自动复制到剪切板了
    //QApplication::clipboard()->text();  //这是从剪切板获取内容
  
    //char szDirName[MAX_PATH] = "";
    //strcpy_s(szDirName, szModName);
    //szDirName[len] = '\0';
  
    QFileInfo fileInfo = QFileInfo(szModName);
  
    QString FileName = fileInfo.fileName();
    QString FilePath = fileInfo.absolutePath();
    FileName = "Patch_"+FileName;
    FilePath = FilePath+'/'+FileName;
  
    QString filename = QFileDialog::getSaveFileName(this, tr("Save file"), FilePath, tr("All files (*.*)"));
    if(!filename.length())
        return;
    filename = QDir::toNativeSeparators(filename); //转换为本机路径格式(带反斜杠)
  
    //call patchSave function
    DBGPATCHINFO* dbgPatchList = new DBGPATCHINFO[patchList.size()];
    for(int i = 0; i < patchList.size(); i++)
        dbgPatchList[i] = patchList.at(i);
    char error[MAX_ERROR_SIZE] = "";
    int patched = DbgFunctions()->PatchFile(dbgPatchList, patchList.size(), filename.toUtf8().constData(), error);
    delete [] dbgPatchList;
    if(patched == -1)
    {
        SimpleErrorBox(this, tr("Error!"), tr("Failed to save patched file (%1)").arg(error));
        return;
    }
    SimpleInfoBox(this, tr("Information"), tr("%1/%2 patch(es) applied!").arg(patched).arg(patchList.size()));
}
  
void PatchDialog::on_btnImport_clicked()
{
    QStringList filenamelist = QFileDialog::getOpenFileNames(this, tr("Open patch"), "", tr("Patch files (*.1337)"));
    int patched = 0;
    bool bBadOriginal = false;
    bool bAlreadyDone = false;
  
    typedef struct _IMPORTSTATUS
    {
        bool badoriginal;
        bool alreadypatched;
    } IMPORTSTATUS;
    QList<QPair<DBGPATCHINFO, IMPORTSTATUS>> patchList;
    DBGPATCHINFO curPatch;
  
    for(const auto & filename1 : filenamelist)
    {
        if(!filename1.length())
            continue;
        QString filename = QDir::toNativeSeparators(filename1); //convert to native path format (with backlashes)
        QFile file(filename);
        file.open(QFile::ReadOnly | QFile::Text);
        QTextStream in(&file);
        QString patch = in.readAll();
        file.close();
        patch = patch.replace("\r\n""\n");
        QStringList lines = patch.split("\n", QString::SkipEmptyParts);
        if(!lines.size())
        {
            SimpleErrorBox(this, tr("Error!"), tr("The patch file is empty..."));
            continue;
        }
  
        dsint modbase = 0;
        for(int i = 0; i < lines.size(); i++)
        {
            ULONGLONG rva;
            unsigned int oldbyte;
            unsigned int newbyte;
            QString curLine = lines.at(i);
            if(curLine.startsWith(">")) //module
            {
                strcpy_s(curPatch.mod, curLine.toUtf8().constData() + 1);
                modbase = DbgFunctions()->ModBaseFromName(curPatch.mod);
                continue;
            }
            if(!modbase)
                continue;
            curLine = curLine.replace(" """);
            if(sscanf_s(curLine.toUtf8().constData(), "%llX:%X->%X", &rva, &oldbyte, &newbyte) != 3)
            {
                //File format is error. Don't continue processing this file
                SimpleErrorBox(this, tr("Error!"), tr("Patch file format is incorrect..."));
                break;
            }
            oldbyte &= 0xFF;
            newbyte &= 0xFF;
            curPatch.addr = rva + modbase;
            if(!DbgMemIsValidReadPtr(curPatch.addr))
                continue;
            unsigned char checkbyte = 0;
            DbgMemRead(curPatch.addr, &checkbyte, sizeof(checkbyte));
            IMPORTSTATUS status;
            status.alreadypatched = (checkbyte == newbyte);
            status.badoriginal = (checkbyte != oldbyte);
            if(status.alreadypatched)
                bAlreadyDone = true;
            else if(status.badoriginal)
                bBadOriginal = true;
            curPatch.oldbyte = oldbyte;
            curPatch.newbyte = newbyte;
            patchList.push_back(QPair<DBGPATCHINFO, IMPORTSTATUS>(curPatch, status));
        }
    }
  
    //Check if any patch exists
    if(!patchList.size())
    {
        SimpleInfoBox(this, tr("Information"), tr("No patches to apply in the current process."));
        return;
    }
  
    //Warn if some are already patched
    bool bUndoPatched = false;
    if(bAlreadyDone)
    {
        QMessageBox msg(QMessageBox::Question, tr("Question"), tr("Some patches are already applied.\n\nDo you want to remove these patches?"), QMessageBox::Yes | QMessageBox::No);
        msg.setWindowIcon(DIcon("question.png"));
        msg.setParent(this, Qt::Dialog);
        msg.setWindowFlags(msg.windowFlags() & (~Qt::WindowContextHelpButtonHint));
        if(msg.exec() == QMessageBox::Yes)
            bUndoPatched = true;
    }
  
    bool bPatchBadOriginals = false;
    if(bBadOriginal)
    {
        QMessageBox msg(QMessageBox::Question, tr("Question"), tr("Some bytes do not match the original in the patch file.\n\nDo you want to apply these patches anyway?"), QMessageBox::Yes | QMessageBox::No);
        msg.setWindowIcon(DIcon("question.png"));
        msg.setParent(this, Qt::Dialog);
        msg.setWindowFlags(msg.windowFlags() & (~Qt::WindowContextHelpButtonHint));
        if(msg.exec() == QMessageBox::Yes)
            bPatchBadOriginals = true;
    }
  
    //Apply all of the patches
    for(int i = 0; i < patchList.size(); i++)
    {
        if(!bPatchBadOriginals && patchList.at(i).second.badoriginal)
            continue;
        curPatch = patchList.at(i).first;
        if(bUndoPatched && patchList.at(i).second.alreadypatched)
        {
            if(DbgFunctions()->MemPatch(curPatch.addr, &curPatch.oldbyte, 1))
                patched++;
        }
        else
        {
            if(DbgFunctions()->MemPatch(curPatch.addr, &curPatch.newbyte, 1))
                patched++;
        }
    }
  
    updatePatches();
    GuiUpdateAllViews();
  
    SimpleInfoBox(this, tr("Information"), tr("%1/%2 patch(es) applied!").arg(patched).arg(patchList.size()));
}
  
void PatchDialog::on_btnExport_clicked()
{
    if(!mPatches.size())
        return;
  
    if(containsRelocatedBytes() && !showRelocatedBytesWarning())
        return;
  
    QString filename = QFileDialog::getSaveFileName(this, tr("Save patch"), "", tr("Patch files (*.1337)"));
    if(!filename.length())
        return;
    filename = QDir::toNativeSeparators(filename); //convert to native path format (with backlashes)
    if(filename.endsWith(QString(".1337")))
        saveAs1337(filename);
    // TODO: C program source
}
  
void PatchDialog::saveAs1337(const QString & filename)
{
    QStringList lines;
  
    int patches = 0;
    for(PatchMap::iterator i = mPatches.begin(); i != mPatches.end(); ++i)
    {
        const PatchInfoList & curPatchList = i.value();
        bool bModPlaced = false;
        dsint modbase = DbgFunctions()->ModBaseFromName(i.key().toUtf8().constData());
        if(!modbase)
            continue;
        for(int j = 0; j < curPatchList.size(); j++)
        {
            if(!curPatchList.at(j).status.checked) //skip unchecked patches
                continue;
            if(!bModPlaced)
            {
                lines.push_back(">" + i.key());
                bModPlaced = true;
            }
            QString addrText = ToPtrString(curPatchList.at(j).patch.addr - modbase);
            lines.push_back(addrText + QString().sprintf(":%.2X->%.2X", curPatchList.at(j).patch.oldbyte, curPatchList.at(j).patch.newbyte));
            patches++;
        }
    }
  
    if(!lines.size())
    {
        SimpleInfoBox(this, tr("Information"), tr("No patches to export."));
        return;
    }
  
    QFile file(filename);
    file.open(QFile::WriteOnly | QFile::Text);
    QString text = lines.join("\n");
    QByteArray textUtf8 = text.toUtf8();
    file.write(textUtf8.constData(), textUtf8.length());
    file.close();
  
    SimpleInfoBox(this, tr("Information"), tr("%1 patch(es) exported!").arg(patches));
}
  
bool PatchDialog::containsRelocatedBytes()
{
    for(PatchMap::iterator i = mPatches.begin(); i != mPatches.end(); ++i)
    {
        const PatchInfoList & curPatchList = i.value();
        if(containsRelocatedBytes(curPatchList))
            return true;
    }
    return false;
}
  
bool PatchDialog::containsRelocatedBytes(const PatchInfoList & patchList)
{
    for(int i = 0; i < patchList.size(); i++)
    {
        if(patchList.at(i).status.checked && DbgFunctions()->ModRelocationAtAddr(patchList.at(i).patch.addr, nullptr))
            return true;
    }
  
    return false;
}
  
bool PatchDialog::showRelocatedBytesWarning()
{
    auto result = QMessageBox::question(this, tr("Patches overlap with relocation regions"), tr("Your patches overlap with relocation regions. This can cause your code to become corrupted when you load the patched executable. Do you want to continue?"));
    return result == QMessageBox::Yes;
}
  
void PatchDialog::on_listModules_itemDoubleClicked(QListWidgetItem *item)
{
     QString str = ui->listModules->currentItem()->text();   //这个编译成功啦~~
     Bridge::CopyToClipboard("Patch_" + str); //得到的列表项内容
}



[注意]看雪招聘,专注安全领域的专业人才平台!

收藏
免费
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册