summaryrefslogtreecommitdiff
path: root/examples/enterprise_test/src/enterprise_test.cpp
blob: 4297fc9f489df341ef980a3c863413d4039a0293 (plain)
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
// This is just bin-info but with an enterprise license checkout

#include <sys/stat.h>

#include <iostream>
#include <cstdlib>

#include "binaryninjacore.h"
#include "binaryninjaapi.h"
#include "enterprise.h"

using namespace BinaryNinja;
using namespace std;

bool is_file(char* fname)
{
	struct stat buf;
	if (stat(fname, &buf) == 0 && (buf.st_mode & S_IFREG) == S_IFREG)
		return true;

	return false;
}

int main(int argc, char* argv[])
{
	if (argc != 2)
	{
		cerr << "USAGE: " << argv[0] << " <file_name>" << endl;
		exit(-1);
	}

	char* fname = argv[1];
	if (!is_file(fname))
	{
		cerr << "Error: " << fname << " is not a regular file" << endl;
		exit(-1);
	}

	Enterprise::LicenseCheckout _{};

	/* In order to initiate the bundled plugins properly, the location
	 * of where bundled plugins directory is must be set. */
	SetBundledPluginDirectory(GetBundledPluginDirectory());
	InitPlugins();

	Ref<BinaryView> bv = BinaryNinja::Load(fname);
	if (!bv || bv->GetTypeName() == "Raw")
	{
		fprintf(stderr, "Input file does not appear to be an executable\n");
		return -1;
	}

	cout << "Target:   " << fname << endl << endl;
	cout << "TYPE:     " << bv->GetTypeName() << endl;
	cout << "START:    0x" << hex << bv->GetStart() << endl;
	cout << "ENTRY:    0x" << hex << bv->GetEntryPoint() << endl;
	cout << "PLATFORM: " << bv->GetDefaultPlatform()->GetName() << endl;
	cout << endl;

	cout << "---------- 10 Functions ----------" << endl;
	int x = 0;
	for (auto func : bv->GetAnalysisFunctionList())
	{
		cout << hex << func->GetStart() << " " << func->GetSymbol()->GetFullName() << endl;
		if (++x >= 10)
			break;
	}
	cout << endl;

	cout << "---------- 10 Strings ----------" << endl;
	x = 0;
	for (auto str_ref : bv->GetStrings())
	{
		char* str = (char*)malloc(str_ref.length + 1);
		bv->Read(str, str_ref.start, str_ref.length);
		str[str_ref.length] = 0;

		cout << hex << str_ref.start << " (" << dec << str_ref.length << ") " << str << endl;
		free(str);

		if (++x >= 10)
			break;
	}

	// Close the file so that the resources can be freed
	bv->GetFile()->Close();

	// Shutting down is required to allow for clean exit of the core
	BNShutdown();

	return 0;
}