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 ScriptReadFileAndExecuteCommandline (INT argc, CHAR *argv[])
 Parsing the command line options for scripts.
 
VOID CommandScript (vector< string > SplitCommand, 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< string > SplitCommand,
string Command )

.script command handler

Parameters
SplitCommand
Command
Returns
VOID
269{
270 vector<string> PathAndArgs;
271
272 if (SplitCommand.size() == 1)
273 {
274 ShowMessages("please specify a file\n");
276 return;
277 }
278
279 //
280 // Trim the command
281 //
282 Trim(Command);
283
284 //
285 // Remove .script from it
286 //
287 Command.erase(0, SplitCommand.at(0).size());
288
289 //
290 // Trim it again
291 //
292 Trim(Command);
293
294 //
295 // Split Path and args
296 //
297 SplitPathAndArgs(PathAndArgs, Command);
298
299 /*
300
301 for (auto item : PathAndArgs)
302 {
303 //
304 // The first argument is the path
305 //
306 ShowMessages("Arg : %s\n", item.c_str());
307 }
308
309 */
310
311 //
312 // Parse the file and the possible arguments
313 //
315}
VOID SplitPathAndArgs(std::vector< std::string > &Qargs, const std::string &Command)
Split path and arguments and handle strings between quotes.
Definition common.cpp:744
void Trim(std::string &s)
trim from both ends and start of a string (in place)
Definition common.cpp:594
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
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:113

◆ 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 //
75 // Check if the it's a command or not
76 //
77 InterpreterRemoveComments(LineContent);
78
79 if (IsEmptyString(LineContent))
80 {
81 return;
82 }
83
84 //
85 // Show current running command
86 //
88
89 ShowMessages("%s\n", LineContent);
90
91 CommandExecutionResult = HyperDbgInterpreter(LineContent);
92
93 ShowMessages("\n");
94
95 //
96 // if the debugger encounters an exit state then the return will be 1
97 //
98 if (CommandExecutionResult == 1)
99 {
100 //
101 // Exit from the debugger
102 //
103 exit(0);
104 }
105}
VOID ReplaceAll(string &str, const string &from, const string &to)
general replace all function
Definition common.cpp:91
BOOLEAN IsEmptyString(char *Text)
Is empty character.
Definition common.cpp:644
VOID InterpreterRemoveComments(char *CommandText)
Remove batch comments.
Definition interpreter.cpp:509
VOID HyperDbgShowSignature()
Show signature of HyperDbg.
Definition interpreter.cpp:566
INT HyperDbgInterpreter(CHAR *Command)
Interpret commands.
Definition interpreter.cpp:280
NULL()
Definition test-case-generator.py:530

◆ HyperDbgScriptReadFileAndExecuteCommand()

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

Read file and run the script.

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

◆ ScriptReadFileAndExecuteCommandline()

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

Parsing the command line options for scripts.

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

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.