示例#1
0
        public string generate_function_code_GlobalFunction(ParsedFunc func)
        {
            string result = "";

            result += "\r\n";
            result += "//////////////////////////////////////////////////////////////////\r\n";
            result += "// " + func.func_name + " Hooking\r\n";
            result += "//////////////////////////////////////////////////////////////////\r\n";
            result += "\r\n";

            //split return type from function description

            Tokenizer t = new Tokenizer(func.return_type);

            string return_type = "";
            string call_type = "";

            string tok = null;
            while (true)
            {
                tok = t.NextToken();

                if (null == tok)
                    break;

                if (tok.Contains("__declspec") || tok.Contains("__stdcall"))
                {
                    if (!tok.Contains("__declspec(dllimport)"))
                    {
                        call_type += tok + " ";
                    }
                }
                else
                {
                    return_type += tok + " ";
                }

            }

            ////////////
            // typedef
            ////////////

            result += "typedef " + return_type;
            result += " (" + call_type + "*" + func.func_name + "_FPTR)(\r\n";

            bool bFirst = true;

            foreach (ParsedArgument arg in func.args_list)
            {
                result += "\t";
                if (!bFirst)
                {
                    result += ",";
                }

                result += arg.arg_type + " ";
                result += arg.arg_name + "\r\n";

                bFirst = false;
            }

            result += "\t);\r\n";

            //////////////////////////
            // orig func pointer
            //////////////////////////

            result += func.func_name + "_FPTR g_" + func.func_name + "_ORIG = NULL;\r\n";

            /////////////////////////////
            // detour function
            /////////////////////////////

            result += return_type + " " + call_type + " " + func.func_name + "_DETOUR (\r\n";

            bFirst = true;

            foreach (ParsedArgument arg in func.args_list)
            {
                result += "\t";
                if (!bFirst)
                {
                    result += ",";
                }

                result += arg.arg_type + " ";
                result += arg.arg_name + "\r\n";

                bFirst = false;
            }

            result += "\t)\r\n";
            result += "{\r\n";

            //assert to test the orig func pointer is valid
            result += "\tassert(g_" + func.func_name + "_ORIG);\r\n";

            //call the orig function

            result += "\t";

            if ("void " != return_type)
            {
                result += "return ";
            }

            result += "\tg_" + func.func_name + "_ORIG(\r\n";

            bFirst = true;
            foreach (ParsedArgument arg in func.args_list)
            {
                result += "\t\t";
                if (!bFirst)
                {
                    result += ",";
                }

                result += arg.arg_name + "\r\n";

                bFirst = false;
            }

            result += "\t\t);\r\n";

            result += "}\r\n";

            // show to hook code itself

            result += "\r\n...\r\n";

            result += "void Init_" + func.func_name + "_Hook()\r\n";
            result += "{\r\n";

            //HMODULE hUser32 = LoadLibrary("user32.dll");

            //TODO: identify module for system functions

            result += "\tHMODULE hmod = LoadLibraryA(\"??????.dll\")\r\n";
            result += "\tassert(hmod);\r\n";
            result += "\tif (hmod)\r\n";
            result += "\t{\r\n";

            result += "\t\tg_" + func.func_name + "_ORIG = (" + func.func_name + "_FPTR) GetProcAddress(hmod,\"" + func.func_name + "\");\r\n";
            result += "\t\tassert(g_" + func.func_name + "_ORIG);\r\n";

            result += "\t\tif (g_" + func.func_name + "_ORIG)\r\n";

            result += "\t\t{\r\n";
            //BOOL hook_res = Mhook_SetHook((PVOID*) &g_MessageBoxA_Orig, MessageBoxA_Detour);
            result += "\t\t\t BOOL hook_res = Mhook_SetHook((PVOID*) &g_" + func.func_name + "_ORIG, " + func.func_name + "_DETOUR);\r\n";
            result += "\t\t\t assert(hook_res);\r\n";

            result += "\t\t}\r\n";

            result += "\t}\r\n";

            result += "}\r\n";

            result += "\r\n...\r\n";

            return result;
        }
示例#2
0
 public CodeGenerator(string text)
 {
     m_ClassDeclarationLocations = new SortedDictionary<string, int>();
     m_MainTokenizer = new Tokenizer(text);
 }