Skip to content

Commit

Permalink
band aid fix for bugged types and logging for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Glumboi committed Apr 20, 2024
1 parent 9630e81 commit c9afc9d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
71 changes: 60 additions & 11 deletions src/GlummysHookerKitchen/CPPFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,39 @@ public CPPParamter(string param)
if (param.IndexOf(' ') != -1)
{
var splitBySpace = param.Split(' ');

Name = splitBySpace.Last();
Type = splitBySpace.First();

foreach (var pair in TypeDatabaseConstants.GhidraTypeFriendlyTypeMap.Where(pair => Type == pair.Key))
/* int spaceCount = splitBySpace.Length - 1;
if (spaceCount >
1) //Allow double types like long long when they are defined with a space between (didnt test this)
{
int indexOfNameStart = Name.IndexOf(Name.First());
Type = param.Substring(0, param.Length - indexOfNameStart - 1);
}*/
/*else
{*/
string tempType = splitBySpace.First();
string tempTypeNoPtr = tempType.Replace("*", "").Replace("&", "");

int len = TypeDatabaseConstants.GhidraTypeFriendlyTypeMap.Count;
for (int i = 0; i < len; i++)
{
KeyValuePair<string, string> pair =
TypeDatabaseConstants.GhidraTypeFriendlyTypeMap.ElementAt(i);
if (tempTypeNoPtr == pair.Key)
tempType = tempType.Replace(pair.Key, pair.Value);
}

for (int j = 0; j < TypeDatabaseConstants.DoubleTypeDatabase.Count; j++)
{
Type = pair.Value;
KeyValuePair<string, string> pair2 =
TypeDatabaseConstants.DoubleTypeDatabase.ElementAt(j);
if (tempTypeNoPtr == pair2.Key)
tempType = tempType.Replace(pair2.Key, pair2.Value);
}

Type = tempType;
//}
}

//Do nothing if parameter only consists of void, so we have clean code
Expand Down Expand Up @@ -60,12 +85,28 @@ public string ReturnType
{
get
{
foreach (var pair in TypeDatabaseConstants.GhidraTypeFriendlyTypeMap)
int len = TypeDatabaseConstants.GhidraTypeFriendlyTypeMap.Count;
string tempType = backPartSplitted.First();
string tempTypeNoPtr = tempType.Replace("*", "").Replace("&", "");


for (int i = 0; i < len; i++)
{
if (backPartSplitted.First() == pair.Key) return pair.Value;
KeyValuePair<string, string> pair =
TypeDatabaseConstants.GhidraTypeFriendlyTypeMap.ElementAt(i);
if (tempTypeNoPtr == pair.Key)
tempType = tempType.Replace(pair.Key, pair.Value);
}

return backPartSplitted.First();
for (int j = 0; j < TypeDatabaseConstants.DoubleTypeDatabase.Count; j++)
{
KeyValuePair<string, string> pair2 =
TypeDatabaseConstants.DoubleTypeDatabase.ElementAt(j);
if (tempTypeNoPtr == pair2.Key)
tempType = tempType.Replace(pair2.Key, pair2.Value);
}

return tempType;
}
}

Expand All @@ -81,12 +122,12 @@ public string CallingConvention
{
get
{
if (backPartSplitted.Length < 3)
return "__stdcall"; //Only __stdcall for now due to some issues with undefined calling conventions
/*if (backPartSplitted.Length < 3)
{
return "__stdcall";
}
return backPartSplitted[^1]; //Calling convention should be between name and return type
return backPartSplitted[^1]; //Calling convention should be between name and return type*/
}
}

Expand Down Expand Up @@ -181,8 +222,16 @@ private string GetFunctionHook()
}

builder.Append(')');

StringBuilder printMsgBuilder =
new StringBuilder($"\n\tTOPSEPERATOR << \"{Name}\" << \" called with parameters:\\n\"");
foreach (var parameter in Parameters)
{
printMsgBuilder.Append($"\n\t<< \"{parameter.Name}: \" << {parameter.Name} << \"\\n\"");
}

builder.Append(
$"\n{{\n\tDEBUG_TO_CONSOLE(\"{Name}\" << \" called!\\n\");\n\n\treturn {Name}_o({returnParametersBuilder.ToString()});\n}}");
$"\n{{\n\tDEBUG_TO_CONSOLE({printMsgBuilder});\n\n\treturn {Name}_o({returnParametersBuilder.ToString()});\n}}");
return builder.ToString();
}

Expand Down
3 changes: 3 additions & 0 deletions src/GlummysHookerKitchen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ static void ParseDump(string target, List<string> exports)
std::cout << msg; \
}
#define TOPSEPERATOR ""================================================\n""
#define BOTTOMSEPERATOR ""\n================================================\n""
#pragma region Helpers
template<typename targetType>
Expand Down
10 changes: 10 additions & 0 deletions src/GlummysHookerKitchen/TypeDatabaseConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ public class TypeDatabaseConstants
{
//TODO: Add more
{ "undefined", "bool" }, //1 byte type, can be many things, I personally expect a bool usually
{ "undefined2", "short" }, //2 bytes type
{ "undefined4", "int32_t" }, //4 bytes type
{ "undefined8", "int64_t" } //8 bytes type
};

public static readonly Dictionary<string, string> DoubleTypeDatabase = new()
{
//TODO: Add more
{ "longlong", "long long" },
{ "ulonglong", "unsigned long long" },
{ "uint", "unsigned int" },
{ "ushort", "unsigned short" },
};
}

0 comments on commit c9afc9d

Please sign in to comment.