{"id":237575,"date":"2014-09-25T19:05:02","date_gmt":"2014-09-25T15:05:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=237575"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=237575","title":{"rendered":"<span class=\"post_title\">\u041f\u0430\u0442\u0447\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0432 Linux \u043d\u0430 \u043b\u0435\u0442\u0443 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 GDB<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t\u0422\u0435\u0445\u043d\u0438\u043a\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 Linux \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435. \u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0441 \u00ab\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438-\u043a\u043b\u043e\u043d\u0430\u043c\u0438\u00bb \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 LD_PRELOAD \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430.<\/p>\n<p>  \u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a LD_PRELOAD \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430. \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0443\u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0438\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0441\u043f\u043b\u0430\u0439\u0441\u0438\u043d\u0433\u00bb \u2014 \u0437\u0430\u043f\u0438\u0441\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0432 Python \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c <code>ctypes<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u044f\u0437\u044b\u043a\u0430 \u0421\u0438 (\u0442.\u0435. \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0421\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0435\u0451 \u0432 Python \u043c\u0435\u0442\u043e\u0434 \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u0432 \u0421-callback \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>ctypes<\/code>.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043a\u043e\u0434\u0430 \u0432 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u043b\u0430\u0434\u0447\u0438\u043a GDB, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Python (<a href=\"https:\/\/sourceware.org\/gdb\/current\/onlinedocs\/gdb\/Python-API.html\">https:\/\/sourceware.org\/gdb\/current\/onlinedocs\/gdb\/Python-API.html<\/a>).  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u044e\u0430\u043d\u0441\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\u041a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0444\u0430\u0439\u043b\u043e\u0432:<\/p>\n<ul>\n<li>pyinject.py \u2014 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 GDB <\/li>\n<li>hook.py \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430\u043c\u0438 <\/li>\n<\/ul>\n<p>  \u0421\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b GDB \u043a\u043e\u0434 \u0443\u0434\u043e\u0431\u043d\u043e \u043e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u041d\u043e\u0432\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044f \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 <code>gdb.Command<\/code>. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 GDB \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434 <code>invoke(argument, from_tty)<\/code>.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044f \u043e\u0442 <code>gdb.Parameter<\/code>. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u0444\u0430\u0439\u043b\u0430 \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430.<\/p>\n<p>  \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u043c\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 <code>PID<\/code> \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043c\u043e\u0434\u0443\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 GDB  <\/p>\n<pre><code class=\"bash\">gdb -ex 'attach PID' -ex 'source pyinject.py' -ex 'set hookfile hook.py'<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u0438 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 GDB, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u00abpyinject\u00bb.  <\/div>\n<\/div>\n<p>  \u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0442\u0440\u0438 \u044d\u0442\u0430\u043f\u0430:<\/p>\n<ol>\n<li>\u0418\u043d\u0436\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python \u0432 \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430<\/li>\n<li>\u0421\u0431\u043e\u0440 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/li>\n<li>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442<\/li>\n<\/ol>\n<p>\u041f\u0443\u043d\u043a\u0442\u044b 1 \u0438 2 \u043f\u0440\u043e\u0449\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043e\u0442\u043b\u0430\u0434\u0447\u0438\u043a\u0430, \u043f\u0443\u043d\u043a\u0442 3 \u0443\u0436\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430.<\/p>\n<h2>\u0418\u043d\u0436\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python<\/h2>\n<p>  \u0411\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c Python \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 GDB \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043e\u0442\u043b\u0430\u0434\u043e\u0447\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439. \u0414\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0435\u0441\u0442\u044c <code>gdb.execute(command, from_tty, to_string)<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 GDB \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0451 \u0432\u044b\u0432\u043e\u0434 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0440\u043e\u043a\u0438.<br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"python\">out = gdb.execute(&quot;info registers&quot;, False, True) <\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 <code>gdb.parse_end_eval(expression)<\/code>, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0449\u0430\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0432\u0438\u0434\u0435 <code>gdb.Value<\/code>.<\/p>\n<p>  \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Python \u0432 \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>dlopen<\/code> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430.<br \/>  \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <code>call<\/code> \u0432 <code>gdb.execute<\/code>, \u043b\u0438\u0431\u043e <code>gdb.parse_and_eval<\/code>:  <\/p>\n<pre><code class=\"python\"># pyinject.py gdb.execute('call dlopen(&quot;libpython2.7.so&quot;, %d)' % RTLD_LAZY) assert long(gdb.history(0)) handle = gdb.parse_and_eval('dlopen(&quot;libpython2.7.so&quot;, %d)' % RTLD_LAZY) assert long(handle) <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440  <\/p>\n<pre><code class=\"python\"># pyinject.py gdb.execute('call PyEval_InitThreads()') gdb.execute('call Py_Initialize()') <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 GIL (global interpreter lock), \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 Python C-API \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e.<\/p>\n<p>  \u0418 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430  <\/p>\n<pre><code class=\"python\"># pyinject.py fp = gdb.parse_and_eval('fopen(&quot;hook.py&quot;, &quot;r&quot;)') assert long(fp) != 0 pyret = gdb.parse_and_eval('PyRun_AnyFileEx(%u, &quot;hook.py&quot;, 1)' % fp) <\/code><\/pre>\n<p><code>PyRun_AnyFileEx<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043a\u043e\u0434 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044f <code>__main__<\/code>.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u044e\u0430\u043d\u0441\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\u0412\u044b\u0448\u0435\u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 Python (\u043a\u0430\u043a \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0438\u043b\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432\u044b\u0439 \u044f\u0437\u044b\u043a). \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a, \u0442\u043e \u0432\u0441\u0451 \u0441\u0435\u0440\u044c\u0451\u0437\u043d\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u043c \u0434\u043b\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Python C-API (\u043a\u0440\u043e\u043c\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <code>Py_AddPendingCall<\/code>).<\/div>\n<\/div>\n<p>  <\/p>\n<h2>\u041c\u043e\u0434\u0443\u043b\u044c hook.py<\/h2>\n<p>  \u041c\u043e\u0434\u0443\u043b\u044c hook.py \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0438 \u0438 \u043a\u043b\u0430\u0441\u0441 <code>Hook<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442.<br \/>  \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>open<\/code> \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u0435\u0451 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0438 \u0432\u0435\u0440\u043d\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0439\u0441\u044f \u0432 \u043f\u043e\u043b\u0435 <code>orig<\/code>  <\/p>\n<pre><code class=\"python\"># hook.py @hook(symbol='open', ctype=CFUNCTYPE(c_int, c_char_p, c_int)) def python_open(fname, oflag):     print &quot;open: &quot;, fname, oflag     return python_open.orig(fname, oflag) <\/code><\/pre>\n<p>  \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 <code>@hook<\/code> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430:<\/p>\n<ul>\n<li>symbol \u2014 \u0438\u043c\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 (\u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 GDB \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0438\u043b\u0438 \u043e\u0442\u043b\u0430\u0434\u043e\u0447\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043d\u043e \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0430\u043c \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432)<\/li>\n<li>ctype \u2014 \u043a\u043b\u0430\u0441\u0441 <code>ctypes<\/code> \u0437\u0430\u0434\u0430\u044e\u0449\u0438\u0439 \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/li>\n<\/ul>\n<p>\u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 Hook \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u044f.  <\/p>\n<pre><code class=\"python\"># hook.py def hook(symbol, ctype):     def deco(func):         Hook.register(symbol, ctype, func)         return func     return deco <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>register<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0435\u0433\u043e \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 <code>all_hooks<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430\u043c \u0432 <code>Hook.all_hooks<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430\u0445.  <\/p>\n<pre><code class=\"python\"># hook.py class Hook(object):     all_hooks = {}     @staticmethod     def register(symbol, *args):         Hook.all_hooks[symbol] = Hook(symbol, *args) <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b GDB \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0443\u0434\u043e\u0431\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <code>Hook<\/code>, \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0437\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442  <\/p>\n<pre><code class=\"python\"># hook.py class Hook(object):     @staticmethod     def hook(symbol, *args):         h = Hook.all_hooks[symbol]         if h.active:             return         h.install(*args) <\/code><\/pre>\n<p>\u0412 <code>*args<\/code> \u0437\u0434\u0435\u0441\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041a\u0430\u043a\u0430\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430.<\/p>\n<h2>\u041c\u0435\u0442\u043e\u0434\u044b \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u00ab\u0441\u043f\u043b\u0430\u0439\u0441\u0438\u043d\u0433\u043e\u043c\u00bb<\/h2>\n<p>  \u0421\u043f\u043b\u0430\u0439\u0441\u0438\u043d\u0433 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0434\u0432\u0438\u0434\u0430 \u043f\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u0412 <b>simple hook<\/b> \u0432\u044b\u0437\u043e\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0448\u0430\u0433\u043e\u0432:<\/p>\n<ol>\n<li>\u043d\u0430\u0447\u0430\u043b\u043e \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u043e\u0439 \u043a\u043e\u043f\u0438\u0438<\/li>\n<li>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432<\/li>\n<li>\u043d\u0430\u0447\u0430\u043b\u043e \u0441\u043d\u043e\u0432\u0430 \u0437\u0430\u0442\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0435\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a<\/li>\n<\/ol>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u044e\u0430\u043d\u0441\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043e\u0447\u0435\u0432\u0438\u0434\u0435\u043d, \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0438 \u0435\u0451 \u043d\u0430\u0447\u0430\u043b\u0430. \u0427\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u044d\u0442\u043e \u043b\u0435\u0447\u0438\u0442\u0441\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041d\u043e \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445 \u043d\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0438\u0447\u044c, \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0441\u043b\u043e\u0432\u0438\u0442\u044c deadlock \u0435\u0441\u043b\u0438 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0442\u0438\u043f\u0430 malloc<\/div>\n<\/div>\n<p>  \u0412 <b>trampoline hook<\/b> \u043d\u0430\u0447\u0430\u043b\u043e \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0432\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0433\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u0432 \u0442\u0435\u043b\u043e \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0412 \u044d\u0442\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043f\u043e \u043d\u043e\u0432\u043e\u043c\u0443 \u0430\u0434\u0440\u0435\u0441\u0443.<\/p>\n<p>  Trampoline hook \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445, \u043d\u043e \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0432 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0438\u0437\u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440. \u041f\u0440\u0438\u0445\u043e\u0434 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b x86_64 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0438\u0437-\u0437\u0430 \u043f\u043e\u0432\u0441\u0435\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430 <code>%rip<\/code> (\u0430\u0434\u0440\u0435\u0441 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b).  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u044e\u0430\u043d\u0441\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>open<\/code> \u0432 GDB:  <\/p>\n<pre><code class=\"perl\">0x7f6cc8aa83e0 &lt;open64+0&gt;:          83 3d ed 33 2d 00 00  cmpl    $0x0,0x2d33ed(%rip) 0x7f6cc8aa83e7 &lt;open64+7&gt;:          75 10                 jne     0x7f6cc8aa83f9 &lt;open64+25&gt; 0x7f6cc8aa83e9 &lt;__open_nocancel+0&gt;: b8 02 00 00 00        mov     $0x2,%eax 0x7f6cc8aa83ee &lt;__open_nocancel+5&gt;: 0f 05                 syscall <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u043f\u0435\u0440\u0432\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 &quot;<code>cmpl $0x0,0x2d33ed(%rip)<\/code>&quot; \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u0430\u0434\u0440\u0435\u0441\u0443, \u0442\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 <code>0x2d33ed(%rip)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0435\u0439\u0447\u0430\u0441 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 <code>0x7f6cc8d7b7d4<\/code>, \u0431\u0443\u0434\u0435\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u0435 \u043c\u0435\u0441\u0442\u043e (\u043f\u0440\u0438\u0432\u0435\u0442 SIGSEGV).<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c trampoline hook \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e:<\/p>\n<ol>\n<li>\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u043c\u0430\u043d\u0434 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/li>\n<li>\u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435 \u0434\u0430\u043b\u044c\u0448\u0435 \u0447\u0435\u043c \u0432 2\u0413\u0411 \u043e\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u0430\u0434\u0440\u0435\u0441\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b cmpl (\u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 <code>0x2d33ed(%rip)<\/code> \u0437\u043d\u0430\u043a\u043e\u0432\u043e\u0435 32-\u0431\u0438\u0442\u043d\u043e\u0435)<\/li>\n<li>\u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0432 \u043d\u043e\u0432\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0438 \u043f\u0440\u043e\u043f\u0430\u0442\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e <code>%rip<\/code> \u0432 <code>cmpl<\/code><\/li>\n<\/ol>\n<p>\u0412 \u0434\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043a\u0430\u0440\u0442\u0438\u043d\u044b, \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043a\u043e\u0440\u043e\u0447\u0435 9 \u0431\u0430\u0439\u0442, \u0442.\u043a. \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u0434\u0432\u0443\u043c\u044f \u0442\u043e\u0447\u043a\u0430\u043c\u0438 \u0432\u0445\u043e\u0434\u0430 \u0438 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <code>0x7f6cc8aa83e9<\/code> \u0443\u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f <code>__open_nocancel<\/code>. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043d\u0430\u0448 \u0442\u0440\u0430\u043c\u043f\u043b\u0438\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043d\u0435 \u0434\u0430\u043b\u044c\u0448\u0435 \u0447\u0435\u043c \u0432 2\u0413\u0411 \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 <code>open<\/code> \u0434\u043b\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 32-\u0431\u0438\u0442\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 (\u0432\u0441\u0435 64-\u0431\u0438\u0442\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u0434\u043b\u0438\u043d\u043d\u0435\u0435 9 \u0431\u0430\u0439\u0442).<\/div>\n<\/div>\n<p>  \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0438\u043c\u0435\u044f \u0432\u0441\u044e \u043c\u043e\u0449\u044c GDB \u0437\u0430 \u0441\u043f\u0438\u043d\u043e\u0439 (<code>gdb.execute()<\/code>), \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c trampoline hook, \u043d\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f simple hook.<\/p>\n<p>  \u0412 <b>simple hook<\/b> \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u044d\u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430.<br \/>  \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0434\u0432\u0430 (\u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445):  <\/p>\n<ul>\n<li>\u041e\u043f\u043a\u043e\u0434 E9 (5 \u0431\u0430\u0439\u0442) \u2014 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 32-\u0431\u0438\u0442\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c (\u043a\u0430\u043a \u0432 trampoline hook) \u0438 \u0443\u0436\u0435 \u043e\u0442\u0442\u0443\u0434\u0430 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 64-\u0431\u0438\u0442\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a.<br \/> \n<pre><code class=\"perl\">0x7f6cc8aa83e0 &lt;open64+0&gt;:          e9 1b 6c 55 37        jmp     0x7f6cfffff000 <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 <code>0x7f6cc8aa83e0 + 0x37556c1b + 5 = 0x7f6cfffff000<\/code><\/li>\n<li>\u041e\u043f\u043a\u043e\u0434 FF 25 (6 \u0431\u0430\u0439\u0442) \u2014 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 64-\u0431\u0438\u0442\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e %rip. \u0414\u043b\u044f \u0430\u0434\u0440\u0435\u0441\u0430 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043d\u0430\u0434\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435 \u0434\u0430\u043b\u044c\u0448\u0435 2\u0413\u0411 \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<br \/> \n<pre><code class=\"perl\">0x00007f6cc8aa83e0 &lt;open64+0&gt;:      ff 25 1a 6c 55 37     jmpq    *0x37556c1a(%rip) <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432 <code>0x7f6cc8aa83e0 + 0x37556c1a + 6 = 0x7f6cfffff000<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0430\u0434\u0440\u0435\u0441 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430.<\/li>\n<\/ul>\n<p>  \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0442\u043e\u0440\u043e\u0439 \u043c\u0435\u0442\u043e\u0434  <\/p>\n<pre><code class=\"python\"># hook.py class Hook(object):     @staticmethod     def get_indlongjmp(srcaddr, proxyaddr):         s = struct.pack('=BBl', 0xff, 0x25, proxyaddr - srcaddr - 6)         return map(ord, s) <\/code><\/pre>\n<p><code>get_indlongjmp<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u044b\u0436\u043a\u0430 \u0441 \u0430\u0434\u0440\u0435\u0441\u0430 <code>srcaddr<\/code> \u043d\u0430 \u0430\u0434\u0440\u0435\u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u0432 QWORD \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <code>proxyaddr<\/code><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u0430 <code>Hook<\/code>. \u041c\u0435\u0442\u043e\u0434 <code>install<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0430\u0434\u0440\u0435\u0441 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>address<\/code> \u0438 \u0430\u0434\u0440\u0435\u0441 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0437\u043e\u043d\u044b <code>proxyaddr<\/code>. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0432 \u0435\u0433\u043e \u0432 <code>self.code<\/code>) \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u043c \u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a  <\/p>\n<pre><code class=\"python\"># hook.py     def install(self, address, proxyaddr):         self.address = address         self.proxyaddr = proxyaddr         proxymemory = (c_void_p * 1).from_address(self.proxyaddr)         proxymemory[0] = Hook.cast_to_void_p(self.cfunc)         self.jmp = self.get_indlongjmp(self.address, self.proxyaddr)         self.memory = (c_ubyte * len(self.jmp)).from_address(self.address)         self.code = list(self.memory)         self.patchmem(self.jmp)         self.pyfunc.orig = self.origfunc()         self.active = True <\/code><\/pre>\n<p>  <code>patchmem<\/code> \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u043e \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438\u0437 <code>src<\/code>  <\/p>\n<pre><code class=\"python\"># hook.py     def patchmem(self, src):         for i in range(len(src)):             self.memory[i] = src[i] <\/code><\/pre>\n<p>  <code>origfunc<\/code> \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u043e\u0434 \u0441\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a.  <\/p>\n<pre><code class=\"python\"># hook.py     def origfunc(self):         ofunc = self.ctype(self.address)         def wrap(*args):             self.patchmem(self.code)             val = ofunc(*args)             self.patchmem(self.jmp)             return val         return wrap <\/code><\/pre>\n<p>  <\/p>\n<h2>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0448\u0442\u0440\u0438\u0445\u0438<\/h2>\n<p>  Python \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d \u0432 \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0444\u0430\u0439\u043b hook.py \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d \u0432 Python. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>Hook.hook(symbol, address, proxyaddr)<\/code> c\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b Python \u043c\u043e\u0434\u0443\u043b\u044f GDB.<\/p>\n<p>  \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0430\u0434\u0440\u0435\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 &quot;<code>open<\/code>&quot;  <\/p>\n<pre><code class=\"python\">line = gdb.execute('info address %s' % &quot;open&quot; False, True) m = re.match(r'.*?(0x[0-9a-f]+)', line) addr = int(m.group(1), 16) <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u044e\u0430\u043d\u0441\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043d\u0430\u0434\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f \u0447\u0442\u043e \u043e\u043d \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u043f\u043e\u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430 (\u0438\u043b\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u0432 \u043d\u0435\u0433\u043e). \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e, \u043e\u0442\u043f\u0430\u0440\u0441\u0438\u0432 \u0432\u044b\u0432\u043e\u0434 <code>gdb.execute(&quot;thread apply all backtrace&quot;)<\/code><\/div>\n<\/div>\n<p>  \u0412\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438 \u043e\u0442 <code>addr<\/code>  <\/p>\n<pre><code class=\"python\">prot = PROT_READ | PROT_WRITE | PROT_EXEC flags = MAP_PRIVATE | MAP_ANONYMOUS maddr = gdb.parse_and_eval('(void*)mmap(0x%x, %d, %d, %d, -1, 0)\\n'                            % (addr | 0x7FFFFFFF, 4096, prot, flags)) maddr = (long(maddr) & 0x00000000FFFFFFFF) | (addr & 0xFFFFFFFF00000000) <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u044e\u0430\u043d\u0441\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u044d\u0442\u043e \u043e\u0431\u0445\u043e\u0434 \u0431\u0430\u0433\u0430 \u0432 GDB, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u044a\u0435\u0434\u0430\u0435\u0442 \u0441\u0442\u0430\u0440\u0448\u0438\u0435 \u0431\u0438\u0442\u044b \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>(addr | 0x7FFFFFFF)<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u0435\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>mmap<\/code> \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0441 \u0430\u0434\u0440\u0435\u0441\u043e\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \u0437\u0430\u043d\u044f\u0442\u043e\u0433\u043e \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e.<\/p>\n<p>  \u0411\u0435\u0437 \u0442\u0440\u044e\u043a\u043e\u0432 \u043f\u043e-\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c\u0443 \u0447\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u043d\u0435\u0435: \u043d\u0430\u0434\u043e \u043e\u0442\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0432\u044b\u0432\u043e\u0434 <code>gdb.execute('info proc mappings', False, True)<\/code>, \u043d\u0430\u0439\u0442\u0438 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0443\u044e \u043a addr \u0434\u044b\u0440\u043a\u0443 \u0432 \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 \u0438 \u0432\u044b\u0432\u0430\u0442\u044c mmap \u0441 <code>MAP_FIXED<\/code>. \u041d\u0443 \u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u043e \u0446\u0435\u043b\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0447\u0435\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/div>\n<\/div>\n<p>  \u0420\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0438\u043d\u0430\u0447\u0435 SIGSEGV)  <\/p>\n<pre><code class=\"python\">gdb.parse_and_eval('mprotect(0x%x, %u, %d)' % (addr & -0x1000, 4096*2, prot)) <\/code><\/pre>\n<p>  \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>Hook.hook<\/code> \u0447\u0435\u0440\u0435\u0437 <code>PyRun_SimpleString<\/code>  <\/p>\n<pre><code class=\"python\">pyret = gdb.parse_and_eval('PyRun_SimpleString(&quot;Hook.hook(\\\\&quot;open\\\\&quot;, 0x%x, 0x%x)&quot;)'                             % (addr, maddr)) <\/code><\/pre>\n<p>  \u0413\u043e\u0442\u043e\u0432\u043e! \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0437\u043e\u0432 &quot;<code>open<\/code>&quot; \u0432 \u0446\u0435\u043b\u0435\u0432\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0447\u0435\u043d \u0438 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u0432 <code>python_open<\/code> \u0438\u0437 hook.py.<\/p>\n<h2>\u0424\u0430\u0439\u043b\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432<\/h2>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 (\u0441 \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a, \u043d\u043e \u0431\u0435\u0437 \u0443\u0447\u0435\u0442\u0430 \u043c\u043d\u043e\u0433\u0438\u0445 \u043d\u044e\u0430\u043d\u0441\u043e\u0432)  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">pyinject.py<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"># pyinject.py import re import os  RTLD_LAZY = 1 PROT_READ = 0x1 PROT_WRITE = 0x2 PROT_EXEC = 0x4 MAP_PRIVATE = 0x2 MAP_FIXED = 0x10 MAP_ANONYMOUS = 0x20 LIBPYTHON = 'libpython2.7.so'  class ParamHookfile(gdb.Parameter):     instance = None     def __init__(self, default=''):         super(ParamHookfile, self).__init__(&quot;hookfile&quot;,                                             gdb.COMMAND_NONE, gdb.PARAM_FILENAME)         self.value = default         ParamHookfile.instance = self      def get_set_string(self):         return self.value      def get_show_string(self, svalue):         return svalue  class CmdHook(gdb.Command):     instance = None     def __init__(self):         super(CmdHook, self).__init__(&quot;pyinject&quot;, gdb.COMMAND_NONE)         self.initialized = False         CmdHook.instance = self      def complete(self, text, word):         matching = [s[4:] for s in dir(self)                      if s.startswith('cmd_')                      and s[4:].startswith(text)]         return matching      def invoke(self, subcmd, from_tty):         self.dont_repeat()         if subcmd.startswith(&quot;hook&quot;):             self.cmd_hook(*gdb.string_to_argv(subcmd))         elif subcmd.startswith(&quot;unhook&quot;):             self.cmd_unhook(*gdb.string_to_argv(subcmd))         else:             gdb.write('unknown sub-command &quot;%s&quot;' % subcmd)      def cmd_hook(self, *args):         self.initialize()         if not self.initialized:             return          pyret = gdb.parse_and_eval('PyRun_SimpleString(&quot;print Hook&quot;)')         if long(pyret) != 0:             hookfile = ParamHookfile.instance.value             if not os.path.exists(hookfile):                 gdb.write('Use &quot;set hookfile &lt;path&gt;&quot;\\n')                 return             fp = gdb.parse_and_eval('fopen(&quot;%s&quot;, &quot;r&quot;)' % hookfile)             assert long(fp) != 0             pyret = gdb.parse_and_eval('PyRun_AnyFileEx(%u, &quot;%s&quot;, 1)' % (fp, hookfile))             if long(pyret) != 0:                 gdb.write('Error loading &quot;%s&quot;\\n' % hookfile)                 return          for symbol in args:             try:                 line = gdb.execute('info address %s' % symbol, False, True)                 m = re.match(r'.*?(0x[0-9a-f]+)', line)                 if m:                     addr = int(m.group(1), 16)             except gdb.error:                 continue             prot = PROT_READ | PROT_WRITE | PROT_EXEC             flags = MAP_PRIVATE | MAP_ANONYMOUS # | MAP_FIXED             maddr = gdb.parse_and_eval('(void*)mmap(0x%x, %d, %d, %d, -1, 0)\\n'                                        % (addr | 0x7FFFFFFF , 4096, prot, flags))             maddr = (long(maddr) & 0x00000000FFFFFFFF) | (addr & 0xFFFFFFFF00000000)             gdb.write(&quot;mmap = 0x%x\\n&quot; % maddr)             if maddr == 0:                 continue             gdb.parse_and_eval('mprotect(0x%x, %u, %d)' % (addr & -0x1000, 4096*2, prot))             pyret = gdb.parse_and_eval('PyRun_SimpleString(&quot;Hook.hook(\\\\&quot;%s\\\\&quot;, 0x%x, 0x%x)&quot;)'                                        % (symbol, addr, maddr))             if long(pyret) == 0:                 gdb.write('hook &quot;%s&quot; OK\\n' % symbol)      def cmd_unhook(self, *args):         for symbol in args:             pyret = gdb.parse_and_eval('PyRun_SimpleString(&quot;Hook.unhook(\\\\&quot;%s\\\\&quot;)&quot;)'                                        % (symbol))             if long(pyret) == 0:                 gdb.write('unhook &quot;%s&quot; OK\\n' % symbol)      def initialize(self):         if self.initialized:             return         handle = gdb.parse_and_eval('dlopen(&quot;%s&quot;, %d)' % (LIBPYTHON, RTLD_LAZY))         if not long(handle):             gdb.write('Cannot load library %s\\n' % LIBPYTHON)             return         if not long(gdb.parse_and_eval('Py_IsInitialized()')):             gdb.execute('call PyEval_InitThreads()')             gdb.execute('call Py_Initialize()')         self.initialized = True  if __name__ == '__main__':     ParamHookfile()     CmdHook() <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<div class=\"spoiler\"><b class=\"spoiler_title\">hook.py<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"># hook.py import struct from ctypes import (CFUNCTYPE, POINTER, c_ubyte, c_int, c_char_p, c_void_p)  class Hook(object):     all_hooks = {}      @staticmethod     def cast_to_void_p(pointer):         return CFUNCTYPE(c_void_p, c_void_p)(lambda x: x)(pointer)      @staticmethod     def register(symbol, *args):         Hook.all_hooks[symbol] = Hook(symbol, *args)      def __init__(self, symbol, ctype, pyfunc):         self.symbol = symbol         self.ctype = ctype         self.pyfunc = pyfunc         self.cfunc = self.ctype(self.pyfunc)         self.address = 0         self.proxyaddr = 0         self.jmp = None         self.memory = None         self.code = None         self.active = False      def install(self, address, proxyaddr):         print &quot;install:&quot;, hex(address)         self.address = address         self.proxyaddr = proxyaddr         proxymemory = (c_void_p * 1).from_address(self.proxyaddr)         proxymemory[0] = Hook.cast_to_void_p(self.cfunc)         self.jmp = self.get_indlongjmp(self.address, self.proxyaddr)         self.memory = (c_ubyte * len(self.jmp)).from_address(self.address)         self.code = list(self.memory)         self.patchmem(self.jmp)         self.pyfunc.orig = self.origfunc()         self.active = True      def uninstall(self):         self.patchmem(self.code)         self.active = False      def origfunc(self):         ofunc = self.ctype(self.address)         def wrap(*args):             self.patchmem(self.code)             val = ofunc(*args)             self.patchmem(self.jmp)             return val         return wrap      def patchmem(self, src):         for i in range(len(src)):             self.memory[i] = src[i]      @staticmethod     def get_indlongjmp(srcaddr, proxyaddr):         # 64-bit indirect absolute jump (6 + 8 bytes)         # ff 25 off32     jmpq  *off32(%rip)         try:             s = struct.pack('=BBl', 0xff, 0x25, proxyaddr - srcaddr - 6)             return map(ord, s)         except:             print hex(proxyaddr), hex(srcaddr), hex(proxyaddr - srcaddr - 6)             raise      @staticmethod     def hook(symbol, address, proxyaddr):         h = Hook.all_hooks[symbol]         if h.active:             return         h.install(address, proxyaddr)      @staticmethod     def unhook(symbol):         h = Hook.all_hooks[symbol]         if not h.active:             return         h.uninstall()  def hook(symbol, ctype):     def deco(func):         Hook.register(symbol, ctype, func)         return func     return deco  #int open (const char *__file, int __oflag, ...) @hook(symbol='open', ctype=CFUNCTYPE(c_int, c_char_p, c_int)) def python_open(fname, oflag):     print &quot;open: &quot;, fname, oflag     return python_open.orig(fname, oflag) <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p> \u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0430 (\u043b\u0443\u0447\u0448\u0435 \u0441 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c\u0438 \u043f\u0443\u0442\u044f\u043c\u0438)  <\/p>\n<pre><code class=\"bash\">gdb -ex 'attach PID' -ex 'source \/path\/pyinject.py' -ex 'set hookfile \/path\/hook.py' (gdb) pyinject hook open (gdb) continue<\/code><\/pre>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/237575\/\"> http:\/\/habrahabr.ru\/post\/237575\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t\u0422\u0435\u0445\u043d\u0438\u043a\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 Linux \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435. \u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0441 \u00ab\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438-\u043a\u043b\u043e\u043d\u0430\u043c\u0438\u00bb \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 LD_PRELOAD \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430.<\/p>\n<p>  \u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a LD_PRELOAD \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430. \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0443\u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0438\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0441\u043f\u043b\u0430\u0439\u0441\u0438\u043d\u0433\u00bb \u2014 \u0437\u0430\u043f\u0438\u0441\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0432 Python \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c <code>ctypes<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u044f\u0437\u044b\u043a\u0430 \u0421\u0438 (\u0442.\u0435. \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0421\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0435\u0451 \u0432 Python \u043c\u0435\u0442\u043e\u0434 \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u0432 \u0421-callback \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>ctypes<\/code>.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-237575","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/237575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=237575"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/237575\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=237575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=237575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=237575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}