Skip to content

Commit 465a8d5

Browse files
committed
Querying candidate base address reasons
Querying vector of reason information for a candidate base address using the new core API
1 parent 4b2b3d5 commit 465a8d5

File tree

5 files changed

+85
-73
lines changed

5 files changed

+85
-73
lines changed

basedetection.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "binaryninjaapi.h"
2222

2323
using namespace BinaryNinja;
24-
using namespace std;
2524

2625

2726
BaseAddressDetection::BaseAddressDetection(Ref<BinaryView> bv)
@@ -65,13 +64,29 @@ bool BaseAddressDetection::IsAborted()
6564
}
6665

6766

68-
std::set<std::pair<size_t, uint64_t>> BaseAddressDetection::GetScores(BaseAddressDetectionConfidence* confidence)
67+
std::set<std::pair<size_t, uint64_t>> BaseAddressDetection::GetScores(BNBaseAddressDetectionConfidence* confidence,
68+
uint64_t *lastTestedBaseAddress)
6969
{
7070
std::set<std::pair<size_t, uint64_t>> result;
7171
BNBaseAddressDetectionScore scores[10];
72-
size_t numCandidates = BNGetBaseAddressDetectionScores(m_object, scores, 10,
73-
(BNBaseAddressDetectionConfidence *)confidence);
72+
size_t numCandidates = BNGetBaseAddressDetectionScores(m_object, scores, 10, confidence, lastTestedBaseAddress);
7473
for (size_t i = 0; i < numCandidates; i++)
7574
result.insert(std::make_pair(scores[i].Score, scores[i].BaseAddress));
7675
return result;
7776
}
77+
78+
79+
std::vector<BNBaseAddressDetectionReason> BaseAddressDetection::GetReasonsForBaseAddress(uint64_t baseAddress)
80+
{
81+
std::vector<BNBaseAddressDetectionReason> result;
82+
size_t count;
83+
BNBaseAddressDetectionReason *reasons = BNGetBaseAddressDetectionReasons(m_object, baseAddress, &count);
84+
if (!reasons)
85+
return result;
86+
87+
for (size_t i = 0; i < count; i++)
88+
result.push_back(reasons[i]);
89+
90+
free(reasons);
91+
return result;
92+
}

binaryninjaapi.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17391,13 +17391,6 @@ namespace BinaryNinja {
1739117391
uint32_t MaxPointersPerCluster;
1739217392
};
1739317393

17394-
enum BaseAddressDetectionConfidence
17395-
{
17396-
NoConfidence = 0,
17397-
LowConfidence = 1,
17398-
HighConfidence = 2,
17399-
};
17400-
1740117394
/*!
1740217395
\ingroup baseaddressdetection
1740317396
*/
@@ -17418,10 +17411,18 @@ namespace BinaryNinja {
1741817411

1741917412
/*! Get the top 10 candidate base addresses and thier scores
1742017413

17421-
\param confidence Confidence level that the top base address candidate is correct
17414+
\param confidence Confidence level that indicates the likelihood the top base address candidate is correct
17415+
\param lastTestedBaseAddress Last base address tested before analysis was aborted or completed
1742217416
\return Set of pairs containing candidate base addresses and their scores
1742317417
*/
17424-
std::set<std::pair<size_t, uint64_t>> GetScores(BaseAddressDetectionConfidence* confidence);
17418+
std::set<std::pair<size_t, uint64_t>> GetScores(BNBaseAddressDetectionConfidence* confidence, uint64_t *lastTestedBaseAddress);
17419+
17420+
/*! Get a vector of BNBaseAddressDetectionReasons containing information that indicates why a base address was reported as a candidate
17421+
17422+
\param baseAddress Base address to query reasons for
17423+
\return Vector of reason structures containing information about why a base address was reported as a candidate
17424+
*/
17425+
std::vector<BNBaseAddressDetectionReason> GetReasonsForBaseAddress(uint64_t baseAddress);
1742517426

1742617427
/*! Abort base address detection
1742717428
*/

binaryninjacore.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,25 +3160,25 @@ extern "C"
31603160

31613161
typedef enum BNBaseAddressDetectionPOISetting
31623162
{
3163-
POI_ANALYSIS_STRINGS_ONLY,
3164-
POI_ANALYSIS_FUNCTIONS_ONLY,
3165-
POI_ANALYSIS_ALL,
3163+
POIAnalysisStringsOnly,
3164+
POIAnalysisFunctionsOnly,
3165+
POIAnalysisAll,
31663166
} BNBaseAddressDetectionPOISetting;
31673167

31683168
typedef enum BNBaseAddressDetectionPOIType
31693169
{
3170-
POI_STRING,
3171-
POI_FUNCTION,
3172-
POI_DATA_VARIABLE,
3173-
POI_FILE_START,
3174-
POI_FILE_END,
3170+
POIString,
3171+
POIFunction,
3172+
POIDataVariable,
3173+
POIFileStart,
3174+
POIFileEnd,
31753175
} BNBaseAddressDetectionPOIType;
31763176

31773177
typedef enum BNBaseAddressDetectionConfidence
31783178
{
3179-
CONFIDENCE_UNASSIGNED,
3180-
CONFIDENCE_LOW,
3181-
CONFIDENCE_HIGH,
3179+
NoConfidence,
3180+
LowConfidence,
3181+
HighConfidence,
31823182
} BNBaseAddressDetectionConfidence;
31833183

31843184
typedef struct BNBaseAddressDetectionSettings
@@ -3197,7 +3197,7 @@ extern "C"
31973197
{
31983198
uint64_t Pointer;
31993199
uint64_t POIOffset;
3200-
BNBaseAddressDetectionPOIType BaseAddressDetectionPOIType;
3200+
BNBaseAddressDetectionPOIType POIType;
32013201
} BNBaseAddressDetectionReason;
32023202

32033203
typedef struct BNBaseAddressDetectionScore
@@ -3206,15 +3206,6 @@ extern "C"
32063206
uint64_t BaseAddress;
32073207
} BNBaseAddressDetectionScore;
32083208

3209-
typedef struct BNBaseAddressDetectionResults
3210-
{
3211-
BNBaseAddressDetectionConfidence Confidence;
3212-
BNBaseAddressDetectionScore** Scores;
3213-
BNBaseAddressDetectionReason** Reasons;
3214-
char* ErrorStr;
3215-
uint64_t LastTestedBaseAddress;
3216-
} BNBaseAddressDetectionResults;
3217-
32183209
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
32193210
BINARYNINJACOREAPI void BNFreeString(char* str);
32203211
BINARYNINJACOREAPI char** BNAllocStringList(const char** contents, size_t size);
@@ -7049,8 +7040,10 @@ extern "C"
70497040
// Base Address Detection
70507041
BINARYNINJACOREAPI BNBaseAddressDetection* BNCreateBaseAddressDetection(BNBinaryView *view);
70517042
BINARYNINJACOREAPI bool BNDetectBaseAddress(BNBaseAddressDetection* bad, BNBaseAddressDetectionSettings& settings);
7052-
BINARYNINJACOREAPI size_t BNGetBaseAddressDetectionScores(BNBaseAddressDetection* bad,
7053-
BNBaseAddressDetectionScore* scores, size_t count, BNBaseAddressDetectionConfidence* confidence);
7043+
BINARYNINJACOREAPI size_t BNGetBaseAddressDetectionScores(BNBaseAddressDetection* bad, BNBaseAddressDetectionScore* scores, size_t count,
7044+
BNBaseAddressDetectionConfidence* confidence, uint64_t* lastTestedBaseAddress);
7045+
BINARYNINJACOREAPI BNBaseAddressDetectionReason* BNGetBaseAddressDetectionReasons(BNBaseAddressDetection* bad,
7046+
uint64_t baseAddress, size_t* count);
70547047
BINARYNINJACOREAPI void BNAbortBaseAddressDetection(BNBaseAddressDetection* bad);
70557048
BINARYNINJACOREAPI bool BNIsBaseAddressDetectionAborted(BNBaseAddressDetection* bad);
70567049
BINARYNINJACOREAPI void BNFreeBaseAddressDetection(BNBaseAddressDetection* bad);

examples/triage/baseaddress.cpp

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@ using namespace std;
66
BNBaseAddressDetectionPOISetting BaseAddressDetectionPOISettingFromString(const std::string& setting)
77
{
88
if (setting == "Strings only")
9-
return POI_ANALYSIS_STRINGS_ONLY;
9+
return POIAnalysisStringsOnly;
1010
if (setting == "Functions only")
11-
return POI_ANALYSIS_FUNCTIONS_ONLY;
12-
return POI_ANALYSIS_ALL; // Default to All
11+
return POIAnalysisFunctionsOnly;
12+
return POIAnalysisAll; // Default to All
1313
}
1414

1515

1616
std::string BaseAddressDetectionPOITypeToString(BNBaseAddressDetectionPOIType type)
1717
{
1818
switch (type)
1919
{
20-
case POI_STRING:
21-
return "String";
22-
case POI_FUNCTION:
23-
return "Function";
24-
case POI_DATA_VARIABLE:
25-
return "Data variable";
26-
case POI_FILE_END:
27-
return "File end";
28-
case POI_FILE_START:
29-
return "File start";
30-
default:
31-
return "Unknown";
20+
case POIString:
21+
return "String";
22+
case POIFunction:
23+
return "Function";
24+
case POIDataVariable:
25+
return "Data variable";
26+
case POIFileEnd:
27+
return "File end";
28+
case POIFileStart:
29+
return "File start";
30+
default:
31+
return "Unknown";
3232
}
3333
}
3434

3535

36-
std::string BaseAddressDetectionConfidenceToString(BinaryNinja::BaseAddressDetectionConfidence level)
36+
std::string BaseAddressDetectionConfidenceToString(BNBaseAddressDetectionConfidence level)
3737
{
3838
switch (level)
3939
{
40-
case BinaryNinja::NoConfidence:
41-
return "Unassigned";
42-
case BinaryNinja::HighConfidence:
43-
return "High";
44-
case BinaryNinja::LowConfidence:
45-
return "Low";
46-
default:
47-
return "Unknown";
40+
case NoConfidence:
41+
return "Unassigned";
42+
case HighConfidence:
43+
return "High";
44+
case LowConfidence:
45+
return "Low";
46+
default:
47+
return "Unknown";
4848
}
4949
}
5050

@@ -124,21 +124,24 @@ void BaseAddressDetectionThread::run()
124124
if (!m_baseDetection->DetectBaseAddress(settings))
125125
emit ResultReady(results);
126126

127-
auto scores = m_baseDetection->GetScores(&results.Confidence);
127+
auto scores = m_baseDetection->GetScores(&results.Confidence, &results.LastTestedBaseAddress);
128128
results.Scores = scores;
129+
for (const auto& score : scores)
130+
{
131+
auto reasons = m_baseDetection->GetReasonsForBaseAddress(score.second);
132+
results.Reasons[score.second] = reasons;
133+
}
134+
129135
emit ResultReady(results);
130136
}
131137

132-
133138
void BaseAddressDetectionWidget::HandleResults(const BaseAddressDetectionQtResults& results)
134139
{
135140
if (!results.Status.empty())
136141
m_status->setText(QString::fromStdString(results.Status));
137142

138-
/* TODO
139143
if (results.Status.empty() && m_worker->IsAborted())
140-
m_status->setText("Aborted by user (Last Base: 0x" + QString::number(results.Results.LastTestedBaseAddress, 16) + ")");
141-
*/
144+
m_status->setText("Aborted by user (Last Base: 0x" + QString::number(results.LastTestedBaseAddress, 16) + ")");
142145

143146
if (results.Scores.empty())
144147
{
@@ -159,27 +162,25 @@ void BaseAddressDetectionWidget::HandleResults(const BaseAddressDetectionQtResul
159162
}
160163

161164
m_resultsTableWidget->clearContents();
162-
/* TODO
163165
size_t numRows = 0;
164-
for (auto rit = results.Results.Scores.rbegin(); rit != results.Results.Scores.rend(); rit++)
165-
numRows += results.Results.Reasons.at(rit->second).size();
166+
for (auto rit = results.Scores.rbegin(); rit != results.Scores.rend(); rit++)
167+
numRows += results.Reasons.at(rit->second).size();
166168

167169
m_resultsTableWidget->setRowCount(numRows);
168170
size_t row = 0;
169-
for (auto rit = results.Results.Scores.rbegin(); rit != results.Results.Scores.rend(); rit++)
171+
for (auto rit = results.Scores.rbegin(); rit != results.Scores.rend(); rit++)
170172
{
171173
auto [score, baseaddr] = *rit;
172-
for (const auto& reason : results.Results.Reasons.at(baseaddr))
174+
for (const auto& reason : results.Reasons.at(baseaddr))
173175
{
174176
m_resultsTableWidget->setItem(row, 0, new QTableWidgetItem("0x" + QString::number(baseaddr, 16)));
175177
m_resultsTableWidget->setItem(row, 1, new QTableWidgetItem("0x" + QString::number(reason.Pointer, 16)));
176178
m_resultsTableWidget->setItem(row, 2, new QTableWidgetItem("0x" + QString::number(reason.POIOffset, 16)));
177179
m_resultsTableWidget->setItem(row, 3, new QTableWidgetItem(
178-
QString::fromStdString(BaseAddressDetectionPOITypeToString(reason.BaseAddressDetectionPOIType))));
180+
QString::fromStdString(BaseAddressDetectionPOITypeToString(reason.POIType))));
179181
row++;
180182
}
181183
}
182-
*/
183184

184185
m_detectBaseAddressButton->setEnabled(true);
185186
m_abortButton->setHidden(true);

examples/triage/baseaddress.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ struct BaseAddressDetectionQtResults
2828
{
2929
std::string Status;
3030
std::set<std::pair<size_t, uint64_t>> Scores;
31-
BinaryNinja::BaseAddressDetectionConfidence Confidence;
31+
BNBaseAddressDetectionConfidence Confidence;
32+
std::map<uint64_t, std::vector<BNBaseAddressDetectionReason>> Reasons;
33+
uint64_t LastTestedBaseAddress;
3234
};
3335

3436
class BaseAddressDetectionThread : public QThread

0 commit comments

Comments
 (0)