HyperDbg Debugger
Loading...
Searching...
No Matches
script.cpp File Reference

.script command More...

#include "pch.h"

Functions

VOID CommandScriptHelp ()
 help of the .script command
VOID CommandScriptRunCommand (std::string Input, vector< string > PathAndArgs)
 Run the command.
VOID HyperDbgScriptReadFileAndExecuteCommand (std::vector< std::string > &PathAndArgs)
 Read file and run the script.
INT HyperDbgScriptReadFileAndExecuteCommandline (INT argc, CHAR *argv[])
 Parsing the command line options for scripts.
VOID CommandScript (vector< CommandToken > CommandTokens, string Command)
 .script command handler

Variables

BOOLEAN g_ExecutingScript
 Shows whether the target is executing a script form '.script' command or executing script by an argument.

Detailed Description

.script command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-07-03

Function Documentation

◆ CommandScript()

VOID CommandScript ( vector< CommandToken > CommandTokens,
string Command )

.script command handler

Parameters
CommandTokens
Command
Returns
VOID
264{
265 vector<string> PathAndArgs;
266 BOOLEAN IsFirstCommand = FALSE;
267
268 if (CommandTokens.size() == 1)
269 {
270 ShowMessages("please specify a file\n\n");
272 return;
273 }
274
275 for (auto Section : CommandTokens)
276 {
277 if (!IsFirstCommand)
278 {
279 IsFirstCommand = TRUE;
280 continue;
281 }
282
283 //
284 // Add the path and the arguments
285 //
286 PathAndArgs.push_back(GetCaseSensitiveStringFromCommandToken(Section));
287 }
288
289 //
290 // Parse the file and the possible arguments
291 //
293}
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
VOID CommandScriptHelp()
help of the .script command
Definition script.cpp:27
VOID HyperDbgScriptReadFileAndExecuteCommand(std::vector< std::string > &PathAndArgs)
Read file and run the script.
Definition script.cpp:108

◆ CommandScriptHelp()

VOID CommandScriptHelp ( )

help of the .script command

Returns
VOID
28{
29 ShowMessages(".script : runs a HyperDbg script.\n\n");
30
31 ShowMessages("syntax : .script [FilePath (string)] [Args (string)]\n");
32
33 ShowMessages("\n");
34 ShowMessages("\t\te.g : .script C:\\scripts\\script.ds\n");
35 ShowMessages("\t\te.g : .script C:\\scripts\\script.ds 95 85 @rsp\n");
36 ShowMessages("\t\te.g : .script \"C:\\scripts\\hello world.ds\"\n");
37 ShowMessages("\t\te.g : .script \"C:\\scripts\\hello world.ds\" @rax\n");
38 ShowMessages("\t\te.g : .script \"C:\\scripts\\hello world.ds\" @rax @rcx+55 $pid\n");
39 ShowMessages("\t\te.g : .script \"C:\\scripts\\hello world.ds\" 12 55 @rip\n");
40}

◆ CommandScriptRunCommand()

VOID CommandScriptRunCommand ( std::string Input,
vector< string > PathAndArgs )

Run the command.

Returns
VOID
49{
50 INT CommandExecutionResult = 0;
51 CHAR * LineContent = NULL;
52 INT i = 0;
53
54 //
55 // Replace the $arg*s
56 // This is not a good approach to replace between strings,
57 // but we let it work this way and in the future versions
58 // we'll integrate the command parsing in the debugger with
59 // the script engine's command parser
60 //
61 for (auto item : PathAndArgs)
62 {
63 string ToReplace = "$arg" + std::to_string(i);
64 i++;
65
66 ReplaceAll(Input, ToReplace, item);
67 }
68
69 //
70 // Convert script to char*
71 //
72 LineContent = (CHAR *)Input.c_str();
73
74 if (IsEmptyString(LineContent))
75 {
76 return;
77 }
78
79 //
80 // Show current running command
81 //
83
84 ShowMessages("%s\n", LineContent);
85
86 CommandExecutionResult = HyperDbgInterpreter(LineContent);
87
88 ShowMessages("\n");
89
90 //
91 // if the debugger encounters an exit state then the return will be 1
92 //
93 if (CommandExecutionResult == 1)
94 {
95 //
96 // Exit from the debugger
97 //
98 exit(0);
99 }
100}
int INT
Definition BasicTypes.h:43
char CHAR
Definition BasicTypes.h:33
BOOLEAN IsEmptyString(CHAR *Text)
Is empty character.
Definition common.cpp:765
VOID HyperDbgShowSignature()
Show signature of HyperDbg.
Definition interpreter.cpp:1086
INT HyperDbgInterpreter(CHAR *Command)
Interpret commands.
Definition interpreter.cpp:800
VOID ReplaceAll(string &str, const string &from, const string &to)
NULL()
Definition test-case-generator.py:530

◆ HyperDbgScriptReadFileAndExecuteCommand()

VOID HyperDbgScriptReadFileAndExecuteCommand ( std::vector< std::string > & PathAndArgs)

Read file and run the script.

Returns
VOID
109{
110 std::string Line;
111 BOOLEAN IsOpened = FALSE;
112 BOOLEAN Reset = FALSE;
113 string CommandToExecute = "";
114 string PathOfScriptFile = "";
115
116 //
117 // Parse the script file,
118 // the first argument is the path
119 //
120 PathOfScriptFile = PathAndArgs.at(0);
121 ReplaceAll(PathOfScriptFile, "\"", "");
122
123 ifstream File(PathOfScriptFile);
124
125 if (File.is_open())
126 {
127 IsOpened = TRUE;
128
129 //
130 // Indicate that it's a script
131 //
133
134 //
135 // Reset multiline command
136 //
137 Reset = TRUE;
138
139 while (std::getline(File, Line))
140 {
141 //
142 // Check for multiline commands
143 //
144 if (CheckMultilineCommand((char *)Line.c_str(), Reset))
145 {
146 //
147 // if the reset is true, we should make the saving buffer empty
148 //
149 if (Reset)
150 {
151 CommandToExecute.clear();
152 }
153
154 //
155 // The command is expected to be continued
156 //
157 Reset = FALSE;
158
159 //
160 // Append to the previous command
161 //
162 CommandToExecute += Line + "\n";
163
164 continue;
165 }
166 else
167 {
168 //
169 // Reset for the next commands round
170 //
171 Reset = TRUE;
172
173 //
174 // Append this line too
175 //
176 CommandToExecute += Line;
177 }
178
179 //
180 // Run the command
181 //
182 CommandScriptRunCommand(CommandToExecute, PathAndArgs);
183
184 //
185 // Clear the command
186 //
187 CommandToExecute.clear();
188 }
189
190 //
191 // Check for some probably not ended commands
192 //
193 if (!CommandToExecute.empty())
194 {
195 CommandScriptRunCommand(CommandToExecute, PathAndArgs);
196
197 //
198 // Clear the command
199 //
200 CommandToExecute.clear();
201 }
202
203 //
204 // Indicate that script is finished
205 //
207
208 File.close();
209 }
210
211 if (!IsOpened)
212 {
213 ShowMessages("err, invalid file specified for the script\n");
214 }
215}
BOOLEAN CheckMultilineCommand(CHAR *CurrentCommand, BOOLEAN Reset)
check for multi-line commands
Definition interpreter.cpp:1132
VOID CommandScriptRunCommand(std::string Input, vector< string > PathAndArgs)
Run the command.
Definition script.cpp:48
BOOLEAN g_ExecutingScript
Shows whether the target is executing a script form '.script' command or executing script by an argum...
Definition globals.h:502

◆ HyperDbgScriptReadFileAndExecuteCommandline()

INT HyperDbgScriptReadFileAndExecuteCommandline ( INT argc,
CHAR * argv[] )

Parsing the command line options for scripts.

Parameters
argc
argv
Returns
INT
226{
227 vector<string> Args;
228
229 //
230 // Convert it to the array
231 //
232 for (SIZE_T i = 2; i < argc; i++)
233 {
234 std::string TempStr(argv[i]);
235 Args.push_back(TempStr);
236 }
237
238 //
239 // Check if the target path and args for script is not empty
240 //
241 if (!Args.empty())
242 {
244 printf("\n");
245 }
246 else
247 {
248 printf("err, invalid command line options passed to the HyperDbg!\n");
249 return FALSE;
250 }
251
252 return TRUE;
253}
printf("ho")
char ** argv
Definition symbol-parser.h:47

Variable Documentation

◆ g_ExecutingScript

BOOLEAN g_ExecutingScript
extern

Shows whether the target is executing a script form '.script' command or executing script by an argument.