-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathidascript.cpp
More file actions
201 lines (175 loc) · 5.36 KB
/
Copy pathidascript.cpp
File metadata and controls
201 lines (175 loc) · 5.36 KB
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
/*-------------------------------------------------------------------------
IDAScript - 1.0 (c) Hex-Rays
A frontend utility for the -S and -t switches.
This utility allows you to run IDC scripts (or other registered scripts) from
the command line.
-------------------------------------------------------------------------*/
#include <stdio.h>
#include <windows.h>
#include <string>
#include <conio.h>
//-------------------------------------------------------------------------
#define IDAG_EXE "ida.exe"
#define IDAG_EXE64 "ida64.exe"
#define IDAT_EXE "idat.exe"
#define IDAT_EXE64 "idat64.exe"
#define IDA_OUT "idaout.txt"
//-------------------------------------------------------------------------
bool launch_program(
const char* prog,
const char* args)
{
PROCESS_INFORMATION pi;
STARTUPINFO si = { sizeof(si) };
char* cargs = strdup(args);
bool ok = CreateProcess(prog, cargs, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) == TRUE;
if (ok)
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
free(cargs);
return ok;
}
//-------------------------------------------------------------------------
void show_usage()
{
static const char help[] =
"IDAScript 1.0 (c) Hex-Rays - A tool to run IDA Pro scripts from the command line\n"
"\n"
"It can be used in two modes:\n"
"\n"
"a) With a database:\n"
"\n"
"\tidascript database.idb script.(idc|py|...) [arg1 [arg2 [arg3 [...]]]]\n"
"\n"
"b) With a temporary database:\n"
"\n"
"\tidascript script.(idc|py|...) [arg1 [arg2 [arg3 [...]]]]\n"
"\n";
printf("%s", help);
}
//-------------------------------------------------------------------------
// Opens a file and retrieves the last write time.
// This function can be used to test if a file exists by passing 'ft = NULL'
bool get_file_modified_date(const char* file, FILETIME* ft = NULL)
{
HANDLE h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
{
memset(ft, 0, sizeof(FILETIME));
return false;
}
if (ft != NULL)
GetFileTime(h, NULL, NULL, ft);
CloseHandle(h);
return true;
}
//-------------------------------------------------------------------------
// atexit handler that will pause and wait for a keypress
static void pause()
{
printf("Press any key to continue...");
_getch();
}
//-------------------------------------------------------------------------
// Checks if the program was executed from an existing console or a new console
// From: http://www.codeguru.com/cpp/misc/misc/consoleapps/article.php/c15893
void install_pause_at_exit()
{
CONSOLE_SCREEN_BUFFER_INFO csbi = { 0 };
HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hStdOutput, &csbi))
return;
// Check whether the cursor position is (0,0)
if (csbi.dwCursorPosition.X == 0 && csbi.dwCursorPosition.Y == 0)
{
// By using atexit, the pause() function will be called
// automatically when the program exits.
atexit(pause);
}
}
//-------------------------------------------------------------------------
int main(int argc, char* argv[])
{
install_pause_at_exit();
if (argc < 2)
{
show_usage();
return -1;
}
int argi = 1;
FILETIME out_ft_before, out_ft_after;
// Take first argument
const char* p;
const char* script_file;
const char* db_file;
// If file has no extension then we assume it is a script file
p = strrchr(argv[argi], '.');
if (p == NULL)
{
printf("Error: Scripts must have an extension in order to determine extlang!\n");
return -1;
}
// idb file?
++p;
if (strncmp(p, "idc", 3) != 0 && strnicmp(p, "id", 2) == 0)
{
if (argc < 3)
{
show_usage();
return -1;
}
db_file = argv[argi++];
script_file = argv[argi];
}
else
{
db_file = "-t";
script_file = argv[argi];
}
// Check if script file exists
if (!get_file_modified_date(script_file))
{
printf("Error: script file '%s' not found!\n", script_file);
return -2;
}
// Tell IDA to skip UI related updates
putenv("TVHEADLESS=1");
// Get output file time before executing the script
get_file_modified_date(IDA_OUT, &out_ft_before);
std::string cmdln = IDAG_EXE " -A -S\"";
for (int i = argi; i < argc; i++)
{
if (strchr(argv[i], ' ') == NULL)
{
cmdln += argv[i];
}
else
{
cmdln += "\\\"";
cmdln += argv[i];
cmdln += "\\\"";
}
if (i + 1 < argc)
cmdln.push_back(' ');
}
cmdln += "\" ";
cmdln += db_file;
if (!launch_program(NULL, cmdln.c_str()))
{
printf("Failed to run IDA with: %s\n", cmdln.c_str());
return -2;
}
// Log file exists and has been modified?
if (get_file_modified_date(IDA_OUT, &out_ft_after) && memcmp(&out_ft_after, &out_ft_before, sizeof(FILETIME)) != 0)
{
FILE* fp = fopen(IDA_OUT, "r");
char line[4096];
while (fgets(line, sizeof(line), fp) != NULL)
printf("%s", line);
fclose(fp);
}
return 0;
}