Skip to content

Commit 4b2b3d5

Browse files
committed
Base address detection widget in Triage view
Initial implementation of base address detection UI widget in triage summary
1 parent 6ba7605 commit 4b2b3d5

File tree

7 files changed

+647
-0
lines changed

7 files changed

+647
-0
lines changed

basedetection.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2015-2024 Vector 35 Inc
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to
5+
// deal in the Software without restriction, including without limitation the
6+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
// sell copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
// IN THE SOFTWARE.
20+
21+
#include "binaryninjaapi.h"
22+
23+
using namespace BinaryNinja;
24+
using namespace std;
25+
26+
27+
BaseAddressDetection::BaseAddressDetection(Ref<BinaryView> bv)
28+
{
29+
m_object = BNCreateBaseAddressDetection(bv->GetObject());
30+
}
31+
32+
33+
BaseAddressDetection::~BaseAddressDetection()
34+
{
35+
BNFreeBaseAddressDetection(m_object);
36+
}
37+
38+
39+
bool BaseAddressDetection::DetectBaseAddress(BaseAddressDetectionSettings& settings)
40+
{
41+
BNBaseAddressDetectionSettings bnSettings = {
42+
settings.Architecture.c_str(),
43+
settings.Analysis.c_str(),
44+
settings.MinStrlen,
45+
settings.Alignment,
46+
settings.LowerBoundary,
47+
settings.UpperBoundary,
48+
settings.POIAnalysis,
49+
settings.MaxPointersPerCluster,
50+
};
51+
52+
return BNDetectBaseAddress(m_object, bnSettings);
53+
}
54+
55+
56+
void BaseAddressDetection::Abort()
57+
{
58+
return BNAbortBaseAddressDetection(m_object);
59+
}
60+
61+
62+
bool BaseAddressDetection::IsAborted()
63+
{
64+
return BNIsBaseAddressDetectionAborted(m_object);
65+
}
66+
67+
68+
std::set<std::pair<size_t, uint64_t>> BaseAddressDetection::GetScores(BaseAddressDetectionConfidence* confidence)
69+
{
70+
std::set<std::pair<size_t, uint64_t>> result;
71+
BNBaseAddressDetectionScore scores[10];
72+
size_t numCandidates = BNGetBaseAddressDetectionScores(m_object, scores, 10,
73+
(BNBaseAddressDetectionConfidence *)confidence);
74+
for (size_t i = 0; i < numCandidates; i++)
75+
result.insert(std::make_pair(scores[i].Score, scores[i].BaseAddress));
76+
return result;
77+
}

basedetection.h

Whitespace-only changes.

binaryninjaapi.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17378,6 +17378,61 @@ namespace BinaryNinja {
1737817378
const std::function<void(Symbol*, Type*)>& add);
1737917379
void Process();
1738017380
};
17381+
17382+
struct BaseAddressDetectionSettings
17383+
{
17384+
std::string Architecture;
17385+
std::string Analysis;
17386+
uint32_t MinStrlen;
17387+
uint32_t Alignment;
17388+
uint64_t LowerBoundary;
17389+
uint64_t UpperBoundary;
17390+
BNBaseAddressDetectionPOISetting POIAnalysis;
17391+
uint32_t MaxPointersPerCluster;
17392+
};
17393+
17394+
enum BaseAddressDetectionConfidence
17395+
{
17396+
NoConfidence = 0,
17397+
LowConfidence = 1,
17398+
HighConfidence = 2,
17399+
};
17400+
17401+
/*!
17402+
\ingroup baseaddressdetection
17403+
*/
17404+
class BaseAddressDetection
17405+
{
17406+
BNBaseAddressDetection* m_object;
17407+
17408+
public:
17409+
BaseAddressDetection(Ref<BinaryView> view);
17410+
~BaseAddressDetection();
17411+
17412+
/*! Analyze program, identify pointers and points-of-interest, and detect candidate base addresses
17413+
17414+
\param settings Base address detection settings
17415+
\return true on success, false otherwise
17416+
*/
17417+
bool DetectBaseAddress(BaseAddressDetectionSettings& settings);
17418+
17419+
/*! Get the top 10 candidate base addresses and thier scores
17420+
17421+
\param confidence Confidence level that the top base address candidate is correct
17422+
\return Set of pairs containing candidate base addresses and their scores
17423+
*/
17424+
std::set<std::pair<size_t, uint64_t>> GetScores(BaseAddressDetectionConfidence* confidence);
17425+
17426+
/*! Abort base address detection
17427+
*/
17428+
void Abort();
17429+
17430+
/*! Determine if base address detection is aborted
17431+
17432+
\return true if aborted by user, false otherwise
17433+
*/
17434+
bool IsAborted();
17435+
};
1738117436
} // namespace BinaryNinja
1738217437

1738317438

binaryninjacore.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ extern "C"
279279
typedef struct BNExternalLibrary BNExternalLibrary;
280280
typedef struct BNExternalLocation BNExternalLocation;
281281
typedef struct BNProjectFolder BNProjectFolder;
282+
typedef struct BNBaseAddressDetection BNBaseAddressDetection;
282283

283284
//! Console log levels
284285
typedef enum BNLogLevel
@@ -3157,6 +3158,63 @@ extern "C"
31573158
ConflictSyncStatus
31583159
} BNSyncStatus;
31593160

3161+
typedef enum BNBaseAddressDetectionPOISetting
3162+
{
3163+
POI_ANALYSIS_STRINGS_ONLY,
3164+
POI_ANALYSIS_FUNCTIONS_ONLY,
3165+
POI_ANALYSIS_ALL,
3166+
} BNBaseAddressDetectionPOISetting;
3167+
3168+
typedef enum BNBaseAddressDetectionPOIType
3169+
{
3170+
POI_STRING,
3171+
POI_FUNCTION,
3172+
POI_DATA_VARIABLE,
3173+
POI_FILE_START,
3174+
POI_FILE_END,
3175+
} BNBaseAddressDetectionPOIType;
3176+
3177+
typedef enum BNBaseAddressDetectionConfidence
3178+
{
3179+
CONFIDENCE_UNASSIGNED,
3180+
CONFIDENCE_LOW,
3181+
CONFIDENCE_HIGH,
3182+
} BNBaseAddressDetectionConfidence;
3183+
3184+
typedef struct BNBaseAddressDetectionSettings
3185+
{
3186+
const char* Architecture;
3187+
const char* Analysis;
3188+
uint32_t MinStrlen;
3189+
uint32_t Alignment;
3190+
uint64_t LowerBoundary;
3191+
uint64_t UpperBoundary;
3192+
BNBaseAddressDetectionPOISetting POIAnalysis;
3193+
uint32_t MaxPointersPerCluster;
3194+
} BNBaseAddressDetectionSettings;
3195+
3196+
typedef struct BNBaseAddressDetectionReason
3197+
{
3198+
uint64_t Pointer;
3199+
uint64_t POIOffset;
3200+
BNBaseAddressDetectionPOIType BaseAddressDetectionPOIType;
3201+
} BNBaseAddressDetectionReason;
3202+
3203+
typedef struct BNBaseAddressDetectionScore
3204+
{
3205+
size_t Score;
3206+
uint64_t BaseAddress;
3207+
} BNBaseAddressDetectionScore;
3208+
3209+
typedef struct BNBaseAddressDetectionResults
3210+
{
3211+
BNBaseAddressDetectionConfidence Confidence;
3212+
BNBaseAddressDetectionScore** Scores;
3213+
BNBaseAddressDetectionReason** Reasons;
3214+
char* ErrorStr;
3215+
uint64_t LastTestedBaseAddress;
3216+
} BNBaseAddressDetectionResults;
3217+
31603218
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
31613219
BINARYNINJACOREAPI void BNFreeString(char* str);
31623220
BINARYNINJACOREAPI char** BNAllocStringList(const char** contents, size_t size);
@@ -6988,6 +7046,14 @@ extern "C"
69887046
BINARYNINJACOREAPI bool BNBinaryViewPullTypeArchiveTypes(BNBinaryView* view, const char* archiveId, const char* const* archiveTypeIds, size_t archiveTypeIdCount, char*** updatedArchiveTypeIds, char*** updatedAnalysisTypeIds, size_t* updatedTypeCount);
69897047
BINARYNINJACOREAPI bool BNBinaryViewPushTypeArchiveTypes(BNBinaryView* view, const char* archiveId, const char* const* typeIds, size_t typeIdCount, char*** updatedAnalysisTypeIds, char*** updatedArchiveTypeIds, size_t* updatedTypeCount);
69907048

7049+
// Base Address Detection
7050+
BINARYNINJACOREAPI BNBaseAddressDetection* BNCreateBaseAddressDetection(BNBinaryView *view);
7051+
BINARYNINJACOREAPI bool BNDetectBaseAddress(BNBaseAddressDetection* bad, BNBaseAddressDetectionSettings& settings);
7052+
BINARYNINJACOREAPI size_t BNGetBaseAddressDetectionScores(BNBaseAddressDetection* bad,
7053+
BNBaseAddressDetectionScore* scores, size_t count, BNBaseAddressDetectionConfidence* confidence);
7054+
BINARYNINJACOREAPI void BNAbortBaseAddressDetection(BNBaseAddressDetection* bad);
7055+
BINARYNINJACOREAPI bool BNIsBaseAddressDetectionAborted(BNBaseAddressDetection* bad);
7056+
BINARYNINJACOREAPI void BNFreeBaseAddressDetection(BNBaseAddressDetection* bad);
69917057
#ifdef __cplusplus
69927058
}
69937059
#endif

0 commit comments

Comments
 (0)