{"id":296102,"date":"2019-12-19T21:00:10","date_gmt":"2019-12-19T21:00:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=296102"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=296102","title":{"rendered":"\u0412 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 LD_PRELOAD"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479858\/\">\u042d\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0430 \u0431\u044b\u043b\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 2014-\u043c \u0433\u043e\u0434\u0443, \u043d\u043e \u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u043e\u043f\u0430\u043b \u043f\u043e\u0434 \u0440\u0435\u043f\u0440\u0435\u0441\u0441\u0438\u0438 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0438 \u043e\u043d\u0430 \u043d\u0435 \u0443\u0432\u0438\u0434\u0435\u043b\u0430 \u0441\u0432\u0435\u0442. \u0417\u0430 \u0432\u0440\u0435\u043c\u044f \u0431\u0430\u043d\u0430 \u044f \u043f\u0440\u043e \u043d\u0435\u0451 \u0437\u0430\u0431\u044b\u043b, \u0430 \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0448\u0451\u043b \u0432 \u0447\u0435\u0440\u043d\u043e\u0432\u0438\u043a\u0430\u0445. \u0414\u0443\u043c\u0430\u043b \u0431\u044b\u043b\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u043d\u043e \u0430\u0432\u043e\u0441\u044c \u043a\u043e\u043c\u0443 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/o1\/8i\/wf\/o18iwf72_dlx0j9-qjr8zni5unq.png\"><\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u044f\u0442\u043d\u0438\u0447\u043d\u043e\u0435 \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u043e\u0435 \u0447\u0442\u0438\u0432\u043e \u043d\u0430 \u0442\u0435\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u0430 \u00ab\u0432\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0433\u043e\u00bb <i>LD_PRELOAD<\/i>.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>1. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0437\u0430\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439<\/h2>\n<p>  \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a <b>\u043f.2<\/b>.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt;  int main() {   srand (time(NULL));   for(int i=0; i&lt;5; i++){     printf (\"%d\\n\", rand()%100);   } } <\/code><\/pre>\n<p>  \u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0444\u043b\u0430\u0433\u043e\u0432:<\/p>\n<pre><code class=\"plaintext\">$ gcc .\/ld_rand.c -o ld_rand <\/code><\/pre>\n<p>  \u0418, \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c 5 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043c\u0435\u043d\u044c\u0448\u0435 100:<\/p>\n<pre><code class=\"plaintext\">$ .\/ld_rand 53 93 48 57 20 <\/code><\/pre>\n<p>  \u041d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u0430 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u043e.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">int rand(){   return 42; } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ gcc -shared -fPIC .\/o_rand.c -o ld_rand.so <\/code><\/pre>\n<p>  \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432\u043f\u043e\u043b\u043d\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"plaintext\"># LD_PRELOAD=$PWD\/ld_rand.so .\/ld_rand 42 42 42 42 42 <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u0442\u0440\u044e\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0435\u0449\u0451 \u0431\u043e\u043b\u0435\u0435 \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u044e\u0449\u0438\u043c, \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0447\u0435\u0440\u0435\u0437 <\/p>\n<pre><code class=\"plaintext\">$ export LD_PRELOAD=$PWD\/ld_rand.so <\/code><\/pre>\n<p>  \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c<\/p>\n<pre><code class=\"plaintext\"># echo \"$PWD\/ld_rand.so\" &gt; \/etc\/ld.so.preload <\/code><\/pre>\n<p>  \u0430 \u0437\u0430\u0442\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435. \u041c\u044b \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u043d\u0438 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u0432 \u043a\u043e\u0434\u0435 \u0441\u0430\u043c\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043d\u043e \u0435\u0451 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u0440\u043e\u0448\u0435\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0444\u0430\u043b\u044c\u0448\u0438\u0432\u044b\u0439 <i>rand<\/i> \u0434\u0430\u0436\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043b.<\/p>\n<p>  \u0427\u0442\u043e \u0436\u0435 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u043b\u043e \u043d\u0430\u0448\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u043b\u044c\u043d\u044b\u0439 <i>rand<\/i>? \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u043e \u0448\u0430\u0433\u0430\u043c.<br \/>  \u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f, \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <i>ldd<\/i>:<\/p>\n<pre><code class=\"plaintext\"># ldd .\/ld_rand         linux-vdso.so.1 (0x00007ffc8b1f3000)         libc.so.6 =&gt; \/lib\/x86_64-linux-gnu\/libc.so.6 (0x00007fe3da8af000)         \/lib64\/ld-linux-x86-64.so.2 (0x00007fe3daa7e000) <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u0435\u043d \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0432\u0435\u0440\u0441\u0438\u0438 OS, \u043d\u043e \u0442\u0430\u043c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0444\u0430\u0439\u043b <i>libc.so<\/i>. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a <i>open<\/i>, <i>malloc<\/i>, <i>printf<\/i> \u0438 \u0442. \u0434. \u041d\u0430\u0448 <i>rand<\/i> \u0442\u0430\u043a\u0436\u0435 \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u0438\u0445 \u0447\u0438\u0441\u043b\u043e. \u0423\u0431\u0435\u0434\u0438\u043c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c:<\/p>\n<pre><code class=\"plaintext\"># nm -D \/lib\/x86_64-linux-gnu\/libc.so.6 | grep \" rand$\" 000000000003aef0 T rand <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043b\u0438 \u043d\u0430\u0431\u043e\u0440 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <i>LD_PRELOAD<\/i><\/p>\n<pre><code class=\"plaintext\"># LD_PRELOAD=$PWD\/ld_rand.so ldd .\/ld_rand         linux-vdso.so.1 (0x00007ffea52ae000)         \/scripts\/c\/ldpreload\/ld_rand.so (0x00007f690d3f9000)         libc.so.6 =&gt; \/lib\/x86_64-linux-gnu\/libc.so.6 (0x00007f690d230000)         \/lib64\/ld-linux-x86-64.so.2 (0x00007f690d405000) <\/code><\/pre>\n<p>  \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <i>LD_PRELOAD<\/i> \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c\u0441\u044f \u043d\u0430\u0448\u0443 <i>ld_rand.so<\/i> \u0434\u0430\u0436\u0435, \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0441\u0430\u043c\u0430 \u0435\u0451 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442. \u0418, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <i>\u00abrand\u00bb<\/i> \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u043d\u044c\u0448\u0435 \u0447\u0435\u043c <i>rand<\/i> \u043e\u0442 <i>libc.so<\/i>, \u0442\u043e \u043e\u043d\u0430 \u0438 \u043f\u0440\u0430\u0432\u0438\u0442 \u0431\u0430\u043b\u043e\u043c.<\/p>\n<p>  Ok, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u043e\u0434\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430\u043c \u0443\u0434\u0430\u043b\u043e\u0441\u044c, \u043d\u043e \u043a\u0430\u043a \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 \u0435\u0451 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0441\u044f \u0438 \u043d\u0435\u043a\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c. \u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448 \u0440\u0430\u043d\u0434\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE #include &lt;dlfcn.h&gt; #include &lt;stdio.h&gt;   typedef int (*orig_rand_f_type)(void);   int rand() {   \/* \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043d\u0435\u043a\u0438\u0439 \u043a\u043e\u0434 *\/   printf(\"Evil injected code\\n\");      orig_rand_f_type orig_rand;   orig_rand = (orig_rand_f_type)dlsym(RTLD_NEXT,\"rand\");   return orig_rand(); } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043d\u0430\u0448\u0435\u0439 \u00ab\u0434\u043e\u0431\u0430\u0432\u043a\u0438\u00bb \u043c\u044b \u043b\u0438\u0448\u044c \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0430, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044f <i>rand<\/i>. \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u0434\u0440\u0435\u0441\u0430 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f <i>dlsym<\/i> \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <i>libdl<\/i>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0439\u0434\u0435\u0442 \u043d\u0430\u0448 <i>rand<\/i> \u0432 \u0441\u0442\u0435\u043a\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043c\u044b \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u043c \u0435\u0451 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <i>&#171;-ldl&#187;<\/i> \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435:<\/p>\n<pre><code class=\"plaintext\">$ gcc -ldl -shared -fPIC .\/o_rand_evil.c -o ld_rand_evil.so <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ LD_PRELOAD=$PWD\/ld_rand_evil.so .\/ld_rand Evil injected code 66 Evil injected code 28 Evil injected code 93 Evil injected code 93 Evil injected code 95 <\/code><\/pre>\n<p>  \u0418 \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u00ab\u0440\u043e\u0434\u043d\u043e\u0439\u00bb <i>rand<\/i>, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u0432 \u043d\u0435\u043a\u0438\u0435 \u043d\u0435\u043f\u043e\u0442\u0440\u0435\u0431\u043d\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<h2>2. \u041c\u0443\u043a\u0438 \u043f\u043e\u0438\u0441\u043a\u0430<\/h2>\n<p>  \u0417\u043d\u0430\u044f \u043e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0443\u0433\u0440\u043e\u0437\u0435, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c, \u0447\u0442\u043e <i>preload<\/i> \u0431\u044b\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d. \u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u043d\u0433\u0430 \u2014 \u0437\u0430\u043f\u0438\u0445\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0432 \u044f\u0434\u0440\u043e, \u043d\u043e \u043c\u0435\u043d\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043b\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432 \u044e\u0437\u0435\u0440\u0441\u043f\u044d\u0439\u0441\u0435. <\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043f\u0430\u0440\u0430\u043c\u0438 \u043f\u043e\u0439\u0434\u0443\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 \u043e\u043f\u0440\u043e\u0432\u0435\u0440\u0436\u0435\u043d\u0438\u0435.<\/p>\n<h2>2.1. \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e<\/h2>\n<p>  \u041a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c \u0440\u0430\u043d\u0435\u0435, \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <i>LD_PRELOAD<\/i> \u0438\u043b\u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0432 \u0435\u0451 \u0432 \u0444\u0430\u0439\u043b\u0435 <i>\/etc\/ld.so.preload<\/i>. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0432\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0445 \u0434\u0435\u0442\u0435\u043a\u0442\u043e\u0440\u0430. <\/p>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0439 \u2014 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;fcntl.h&gt;  int main() {   char*  pGetenv = getenv(\"LD_PRELOAD\");   pGetenv != NULL ?     printf(\"LD_PRELOAD (getenv) [+]\\n\"):     printf(\"LD_PRELOAD (getenv) [-]\\n\"); } <\/code><\/pre>\n<p>  \u0412\u0442\u043e\u0440\u043e\u0439 \u2014 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;fcntl.h&gt;  int main() {   open(\"\/etc\/ld.so.preload\", O_RDONLY) != -1 ?     printf(\"LD_PRELOAD (open) [+]\\n\"):     printf(\"LD_PRELOAD (open) [-]\\n\"); } <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u0433\u0440\u0443\u0437\u0438\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code class=\"plaintext\">$ export LD_PRELOAD=$PWD\/ld_rand.so $ echo \"$PWD\/ld_rand.so\" &gt; \/etc\/ld.so.preload  $ .\/detect_base_getenv LD_PRELOAD (getenv) [+] $ .\/detect_base_open LD_PRELOAD (open) [+] <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 [+] \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435. <br \/>  \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, [-] \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0431\u0445\u043e\u0434 \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  \u041d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0435\u043d\u0435\u043d \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u0435\u043b\u044c? \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0439\u043c\u0451\u043c\u0441\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;dlfcn.h&gt;  char* (*orig_getenv)(const char *) = NULL; char* getenv(const char *name) {     if(!orig_getenv) orig_getenv = dlsym(RTLD_NEXT, \"getenv\");     if(strcmp(name, \"LD_PRELOAD\") == 0) return NULL;     return orig_getenv(name); } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ gcc -shared -fpic -ldl .\/ld_undetect_getenv.c -o .\/ld_undetect_getenv.so $ LD_PRELOAD=.\/ld_undetect_getenv.so .\/detect_base_getenv LD_PRELOAD (getenv) [-] <\/code><\/pre>\n<p>  \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u0438 \u043e\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 <i>open<\/i>:<\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;dlfcn.h&gt; #include &lt;errno.h&gt;  int (*orig_open)(const char*, int oflag) = NULL;  int open(const char *path, int oflag, ...) {     char real_path[256];     if(!orig_open) orig_open = dlsym(RTLD_NEXT, \"open\");     realpath(path, real_path);     if(strcmp(real_path, \"\/etc\/ld.so.preload\") == 0){         errno = ENOENT;         return -1;     }     return orig_open(path, oflag); } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ gcc -shared -fpic -ldl .\/ld_undetect_open.c -o .\/ld_undetect_open.so $ LD_PRELOAD=.\/ld_undetect_open.so .\/detect_base_open LD_PRELOAD (open) [-] <\/code><\/pre>\n<p>  \u0414\u0430, \u0437\u0434\u0435\u0441\u044c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0444\u0430\u0439\u043b\u0443, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a, <i>open64<\/i>, <i>stat<\/i> \u0438 \u0442.\u0434., \u043d\u043e, \u043f\u043e \u0441\u0443\u0442\u0438, \u0434\u043b\u044f \u0438\u0445 \u043e\u0431\u043c\u0430\u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0442\u0435 \u0436\u0435 5-10 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430.<\/p>\n<h2>2.2. \u0414\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435<\/h2>\n<p>  \u0412\u044b\u0448\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <i>getenv()<\/i> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <i>LD_PRELOAD<\/i>, \u043d\u043e \u0435\u0441\u0442\u044c \u0436\u0435 \u0438 \u0431\u043e\u043b\u0435\u0435 \u00ab\u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439\u00bb \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e <i>ENV<\/i>-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u043c\u0441\u044f \u043a \u043c\u0430\u0441\u0441\u0438\u0432\u0443 <i>**environ<\/i>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043a\u043e\u043f\u0438\u044f \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;string.h&gt;  extern char **environ; int main(int argc, char **argv) {   int i;   char env[] = \"LD_PRELOAD\";   if (environ != NULL)     for (i = 0; environ[i] != NULL; i++)     {       char * pch;       pch = strstr(environ[i],env);       if(pch != NULL)       {         printf(\"LD_PRELOAD (**environ) [+]\\n\");         return 0;       }     }   printf(\"LD_PRELOAD (**environ) [-]\\n\");   return 0; } <\/code><\/pre>\n<p>  \u0422\u0430\u043a \u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0447\u0438\u0442\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0438\u0442\u044c, \u0438 \u043d\u0430\u0448 <i>undetect_getenv<\/i> \u0443\u0436\u0435 \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0442\u043e\u0440\u0436\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"plaintext\">$ LD_PRELOAD=.\/ld_undetect_getenv.so .\/detect_environ LD_PRELOAD (**environ) [+] <\/code><\/pre>\n<p>  \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0440\u0435\u0448\u0435\u043d\u0430? \u0412\u0441\u0451 \u0435\u0449\u0451 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <i>LD_PRELOAD<\/i> \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0437\u043b\u043e\u043c\u0449\u0438\u043a\u0430\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0435\u0451 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c, \u043f\u043b\u043e\u0445\u043e\u0439 \u0441\u0442\u0438\u043b\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u043e \u0440\u0430\u0437\u0432\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0442\u043e\u0433\u043e, \u043a\u0442\u043e \u0438 \u0442\u0430\u043a \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u0436\u0435\u043b\u0430\u0435\u0442 \u043d\u0430\u043c \u0434\u043e\u0431\u0440\u0430?<\/p>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0444\u0435\u0439\u043a\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <i>init()<\/i>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0438\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0439 <i>LD_PRELOAD<\/i> \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0451 \u043d\u0430\u0448\u0435\u043c\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u043e\u0432\u0449\u0438\u043a\u0443:<\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;dlfcn.h&gt; #include &lt;stdlib.h&gt;  extern char **environ; char *evil_env; int (*orig_execve)(const char *path, char *const argv[], char *const envp[]) = NULL;   \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0444\u0435\u0439\u043a\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e init \/\/ \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \/\/ \u0434\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439  void evil_init() {   \/\/ \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 LD_PRELOAD   static const char *ldpreload = \"LD_PRELOAD\";   int len = strlen(getenv(ldpreload));   evil_env = (char*) malloc(len+1);   strcpy(evil_env, getenv(ldpreload));    int i;   char env[] = \"LD_PRELOAD\";   if (environ != NULL)     for (i = 0; environ[i] != NULL; i++) {       char * pch;       pch = strstr(environ[i],env);       if(pch != NULL) {         \/\/ \u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e LD_PRELOAD         unsetenv(env);         break;       }     } }  int execve(const char *path, char *const argv[], char *const envp[]) {   int i = 0, j = 0, k = -1, ret = 0;   char** new_env;   if(!orig_execve) orig_execve = dlsym(RTLD_NEXT,\"execve\");    \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u043c \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 LD_PRELOAD   for(i = 0; envp[i]; i++){     if(strstr(envp[i], \"LD_PRELOAD\")) k = i;   }   \/\/ \u0415\u0441\u043b\u0438 LD_PRELOAD \u043d\u0435 \u0431\u044b\u043b\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0434\u043e \u043d\u0430\u0441, \u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0433\u043e   if(k == -1){     k = i;     i++;   }   \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043d\u043e\u0432\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435   new_env = (char**) malloc((i+1)*sizeof(char*));    \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0430\u0440\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c LD_PRELOAD   for(j = 0; j &lt; i; j++) {     \/\/ \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u043c LD_PRELOAD     if(j == k) {       new_env[j] = (char*) malloc(256);       strcpy(new_env[j], \"LD_PRELOAD=\");       strcat(new_env[j], evil_env);     }     else new_env[j] = (char*) envp[j];   }   new_env[i] = NULL;   ret = orig_execve(path, argv, new_env);   free(new_env[k]);   free(new_env);   return ret; } <\/code><\/pre>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<pre><code class=\"plaintext\">$ gcc -shared -fpic -ldl -Wl,-init,evil_init  .\/ld_undetect_environ.c -o .\/ld_undetect_environ.so $ LD_PRELOAD=.\/ld_undetect_environ.so .\/detect_environ LD_PRELOAD (**environ) [-] <\/code><\/pre>\n<p>  <\/p>\n<h2>2.3. \/proc\/self\/<\/h2>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0430\u043c\u044f\u0442\u044c \u2014 \u044d\u0442\u043e \u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043c\u0435\u0441\u0442\u043e, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u043f\u043e\u0434\u043c\u0435\u043d\u0443 <i>LD_PRELOAD<\/i>, \u0435\u0441\u0442\u044c \u0436\u0435 \u0435\u0449\u0451 \u0438 <i>\/proc\/<\/i>. \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0433\u043e <i>\/proc\/{PID}\/environ<\/i>.<\/p>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0435\u0441\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f <i>undetect&#8217;\u0430<\/i> <i>**environ<\/i> \u0438 <i>\/proc\/self\/environ<\/i>. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u00ab\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c\u00bb \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 <i>unsetenv(env)<\/i>.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">void evil_init() {   \/\/ \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 LD_PRELOAD   static const char *ldpreload = \"LD_PRELOAD\";   int len = strlen(getenv(ldpreload));   evil_env = (char*) malloc(len+1);   strcpy(evil_env, getenv(ldpreload));     int i;   char env[] = \"LD_PRELOAD\";   if (environ != NULL)     for (i = 0; environ[i] != NULL; i++) {       char * pch;       pch = strstr(environ[i],env);       if(pch != NULL) {         \/\/ \u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e LD_PRELOAD          \/\/unsetenv(env);         \/\/ \u0412\u043c\u0435\u0441\u0442\u043e unset \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043d\u0443\u043b\u0438\u043c \u043d\u0430\u0448\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e         for(int j = 0; environ[i][j] != '\\0'; j++) environ[i][j] = '\\0';         break;       }     } } <\/code><\/pre>\n<pre><code class=\"plaintext\">$ gcc -shared -fpic -ldl -Wl,-init,evil_init  .\/ld_undetect_environ_2.c -o .\/ld_undetect_environ_2.so $ (LD_PRELOAD=.\/ld_undetect_environ_2.so cat \/proc\/self\/environ; echo) | tr \"\\000\" \"\\n\" | grep -F LD_PRELOAD $ <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u0448\u043b\u0438 \u0438 <i>\/proc\/self\/environ<\/i> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u00ab\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043d\u044b\u0435\u00bb \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441 \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u00ab\u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439\u00bb:<\/p>\n<pre><code class=\"plaintext\">$ (LD_PRELOAD=.\/ld_undetect_environ.so cat \/proc\/self\/environ; echo) | tr \"\\000\" \"\\n\" | grep -F LD_PRELOAD LD_PRELOAD=.\/ld_undetect_environ.so <\/code><\/pre>\n<p>  <i>cat<\/i> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0432\u0441\u0451 \u0442\u043e\u0442 \u0436\u0435 <i>open()<\/i>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u0445\u043e\u0434\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0443\u0436\u0435 \u0434\u0435\u043b\u0430\u043b\u043e\u0441\u044c \u0432 \u043f.2.1, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u043a\u0443\u0434\u0430 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0441\u0442\u0438\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0431\u0435\u0437 \u0441\u0442\u0440\u043e\u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 <i>LD_PRELOAD<\/i>.<\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE #include &lt;dlfcn.h&gt;  #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;fcntl.h&gt; #include &lt;sys\/stat.h&gt; #include &lt;unistd.h&gt; #include &lt;limits.h&gt; #include &lt;errno.h&gt;  #define BUFFER_SIZE 256  int (*orig_open)(const char*, int oflag) = NULL; char *soname = \"fakememory_preload.so\";  char *sstrstr(char *str, const char *sub) {   int i, found;   char *ptr;   found = 0;   for(ptr = str; *ptr != '\\0'; ptr++) {     found = 1;     for(i = 0; found == 1 &amp;&amp; sub[i] != '\\0'; i++){       if(sub[i] != ptr[i]) found = 0;     }     if(found == 1)       break;   }   if(found == 0)     return NULL;   return ptr + i; }  void fakeMaps(char *original_path, char *fake_path, char *pattern) {   int fd;   char buffer[BUFFER_SIZE];   int bytes = -1;   int wbytes = -1;   int k = 0;    pid_t pid = getpid();    int fh;   if ((fh=orig_open(fake_path,O_CREAT|O_WRONLY))==-1) {     printf(\"LD: Cannot open write-file [%s] (%d) (%s)\\n\", fake_path, errno, strerror(errno));     exit (42);   }   if((fd=orig_open(original_path, O_RDONLY))==-1) {     printf(\"LD: Cannot open read-file.\\n\");     exit(42);   }   do   {     char t = 0;     bytes = read(fd, &amp;t, 1);     buffer[k++] = t;     \/\/printf(\"%c\", t);     if(t == '\\0') {       \/\/printf(\"\\n\");          if(!sstrstr(buffer, \"LD_PRELOAD\")) {         if((wbytes = write(fh,buffer,k))==-1) {           \/\/printf(\"write error\\n\");         }         else {           \/\/printf(\"writed %d\\n\", wbytes);         }       }       k = 0;     }   }   while(bytes != 0);        close(fd);   close(fh); }  int open(const char *path, int oflag, ...) {   char real_path[PATH_MAX], proc_path[PATH_MAX], proc_path_0[PATH_MAX];   pid_t pid = getpid();   if(!orig_open)   orig_open = dlsym(RTLD_NEXT, \"open\");   realpath(path, real_path);   snprintf(proc_path, PATH_MAX, \"\/proc\/%d\/environ\", pid);      if(strcmp(real_path, proc_path) == 0) {     snprintf(proc_path, PATH_MAX, \"\/tmp\/%d.fakemaps\", pid);     realpath(proc_path_0, proc_path);          fakeMaps(real_path, proc_path, soname);     return orig_open(proc_path, oflag);   }   return orig_open(path, oflag); } <\/code><\/pre>\n<p>  \u0418 \u044d\u0442\u043e\u0442 \u044d\u0442\u0430\u043f \u043f\u0440\u043e\u0439\u0434\u0435\u043d:<\/p>\n<pre><code class=\"plaintext\">$ (LD_PRELOAD=.\/ld_undetect_proc_environ.so cat \/proc\/self\/environ; echo) | tr \"\\000\" \"\\n\" | grep -F LD_PRELOAD $ <\/code><\/pre>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u2014 <i>\/proc\/self\/maps<\/i>. \u0417\u0430\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043d\u0435\u043c \u043d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443: \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u0437\u0430 \u0432\u044b\u0447\u0435\u0442\u043e\u043c \u0441\u0442\u0440\u043e\u043a \u043c\u0435\u0436\u0434\u0443 <i>libc.so<\/i> \u0438 <i>ld.so<\/i>.<\/p>\n<h2>2.4. \u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0442 Chokepoint<\/h2>\n<p>  \u042d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043c\u043d\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u043e\u0439. \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u044b\u0445 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437 <i>libc<\/i>, \u0438 \u00abNEXT\u00bb-\u0430\u0434\u0440\u0435\u0441\u0430. <\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE  #include &lt;stdio.h&gt; #include &lt;dlfcn.h&gt;  #define LIBC \"\/lib\/x86_64-linux-gnu\/libc.so.6\"  int main(int argc, char *argv[]) {   void *libc = dlopen(LIBC, RTLD_LAZY); \/\/ Open up libc directly   char *syscall_open = \"open\";   int i;   void *(*libc_func)();   void *(*next_func)();      libc_func = dlsym(libc, syscall_open);   next_func = dlsym(RTLD_NEXT, syscall_open);   if (libc_func != next_func) {     printf(\"LD_PRELOAD (syscall - %s) [+]\\n\", syscall_open);     printf(\"Libc address: %p\\n\", libc_func);     printf(\"Next address: %p\\n\", next_func);   }   else {     printf(\"LD_PRELOAD (syscall - %s) [-]\\n\", syscall_open);   }   return 0; } <\/code><\/pre>\n<p>  \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u043e\u043c <i>\u00abopen()\u00bb<\/i> \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<pre><code class=\"plaintext\">$ export LD_PRELOAD=$PWD\/ld_undetect_open.so $ .\/detect_chokepoint LD_PRELOAD (syscall - open) [+] Libc address: 0x7fa86893b160 Next address: 0x7fa868a26135 <\/code><\/pre>\n<p>  \u041e\u043f\u0440\u043e\u0432\u0435\u0440\u0436\u0435\u043d\u0438\u0435 \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0435\u0449\u0451 \u043f\u0440\u043e\u0449\u0435:<\/p>\n<pre><code class=\"plaintext\">#define _GNU_SOURCE #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;dlfcn.h&gt;  extern void * _dl_sym (void *, const char *, void *); void * dlsym (void * handle, const char * symbol) {   return _dl_sym (handle, symbol, dlsym); } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\"># LD_PRELOAD=.\/ld_undetect_chokepoint.so .\/detect_chokepoint LD_PRELOAD (syscall - open) [-] <\/code><\/pre>\n<p>  <\/p>\n<h2>2.5. Syscalls<\/h2>\n<p>  \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451, \u043d\u043e \u0435\u0449\u0451 \u043f\u043e\u0431\u0430\u0440\u0430\u0445\u0442\u0430\u0435\u043c\u0441\u044f. \u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u043c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043a \u044f\u0434\u0440\u0443, \u0442\u043e \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043e\u0431\u043e\u0439\u0442\u0438 \u0432\u0435\u0441\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0438\u0436\u0435, \u0440\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0435 (<i>x86_64<\/i>). \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f <i>ld.so.preload<\/i>.<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;sys\/stat.h&gt; #include &lt;fcntl.h&gt;  #define BUFFER_SIZE 256  int syscall_open(char *path, long oflag) {     int fd = -1;     __asm__ (              \"mov $2, %%rax;\" \/\/ Open syscall number              \"mov %1, %%rdi;\" \/\/ Address of our string              \"mov %2, %%rsi;\" \/\/ Open mode              \"mov $0, %%rdx;\" \/\/ No create mode              \"syscall;\"       \/\/ Straight to ring0              \"mov %%eax, %0;\" \/\/ Returned file descriptor              :\"=r\" (fd)              :\"m\" (path), \"m\" (oflag)              :\"rax\", \"rdi\", \"rsi\", \"rdx\"              );     return fd;  } int main() {     syscall_open(\"\/etc\/ld.so.preload\", O_RDONLY) &gt; 0 ?       printf(\"LD_PRELOAD (open syscall) [+]\\n\"):       printf(\"LD_PRELOAD (open syscall) [-]\\n\");          } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ .\/detect_syscall LD_PRELOAD (open syscall) [+] <\/code><\/pre>\n<p>  \u0418 \u0434\u0430\u043d\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u0412\u044b\u0434\u0435\u0440\u0436\u043a\u0430 \u0438\u0437 <i>man<\/i>&#8216;\u0430:  <\/p>\n<blockquote><p>ptrace \u2014 \u044d\u0442\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0435\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0442\u0435\u043a\u0430\u043d\u0438\u0435 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430, \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u043e\u0447\u0435\u043a \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043e\u0442\u043b\u0430\u0434\u043a\u0438 \u0438 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>  \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0430\u0442\u044c \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0443, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0437\u0432\u0430\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e fork(2), \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c PTRACE_TRACEME, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u043c (\u043e\u0431\u044b\u0447\u043d\u043e) \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 exec(3). \u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0430\u0442\u044c \u043e\u0442\u043b\u0430\u0434\u043a\u0443 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 PTRACE_ATTACH.<\/p>\n<p>  \u041f\u0440\u0438 \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0441\u0438\u0433\u043d\u0430\u043b\u0430, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u0441\u0438\u0433\u043d\u0430\u043b \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f. (\u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f SIGKILL, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u043e\u0431\u044b\u0447\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.) \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 wait(2), \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0434\u043e \u0435\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u043c\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443, \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044f \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u043c\u044b\u0439 \u0435\u043c\u0443 \u0441\u0438\u0433\u043d\u0430\u043b \u0438\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044f \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b).  <\/p><\/blockquote>\n<p>  \u0422\u0435\u043c \u0441\u0430\u043c\u044b\u043c, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044f \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u043c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u043c \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0438, \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043b\u043e\u0432\u0443\u0448\u043a\u0438.<\/p>\n<pre><code class=\"cpp\">#define _GNU_SOURCE #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;limits.h&gt; #include &lt;sys\/ptrace.h&gt; #include &lt;sys\/wait.h&gt; #include &lt;sys\/reg.h&gt; #include &lt;sys\/user.h&gt; #include &lt;asm\/unistd.h&gt;   #if defined(__x86_64__) #define REG_SYSCALL ORIG_RAX #define REG_SP rsp #define REG_IP rip  #endif  long NOHOOK = 0;  long evil_open(const char *path, long oflag, long cflag)  {     char real_path[PATH_MAX], maps_path[PATH_MAX];     long ret;     pid_t pid;     pid = getpid();     realpath(path, real_path);     if(strcmp(real_path, \"\/etc\/ld.so.preload\") == 0)     {         errno = ENOENT;         ret = -1;     }     else     {         NOHOOK = 1; \/\/ Entering NOHOOK section         ret = open(path, oflag, cflag);     }     \/\/ Exiting NOHOOK section     NOHOOK = 0;     return ret; }  void init() {     pid_t program;     \/\/ \u0424\u043e\u0440\u043a\u0430\u0435\u043c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441     program = fork();     if(program != 0) {         int status;         long syscall_nr;         struct user_regs_struct regs;         \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u043c\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443         if(ptrace(PTRACE_ATTACH, program) != 0) {             printf(\"Failed to attach to the program.\\n\");             exit(1);         }         waitpid(program, &amp;status, 0);         \/\/ \u041e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e SYSCALLs         ptrace(PTRACE_SETOPTIONS, program, 0, PTRACE_O_TRACESYSGOOD);         while(1) {             ptrace(PTRACE_SYSCALL, program, 0, 0);             waitpid(program, &amp;status, 0);             if(WIFEXITED(status) || WIFSIGNALED(status)) break;             else if(WIFSTOPPED(status) &amp;&amp; WSTOPSIG(status) == SIGTRAP|0x80) {                 \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u043e\u043c\u0435\u0440 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430                 syscall_nr = ptrace(PTRACE_PEEKUSER, program, sizeof(long)*REG_SYSCALL);                 if(syscall_nr == __NR_open) {                     \/\/ \u0427\u0438\u0442\u0430\u0435\u043c \u0441\u043b\u043e\u0432\u043e \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430                     NOHOOK = ptrace(PTRACE_PEEKDATA, program, (void*)&amp;NOHOOK);                     \/\/ \u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u044b\u0437\u043e\u0432                     if(!NOHOOK) {                                                  \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430                         \/\/ \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e regs \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e                         ptrace(PTRACE_GETREGS, program, 0, &amp;regs);                         \/\/ Push return address on the stack                         regs.REG_SP -= sizeof(long);                         \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441\u043b\u043e\u0432\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430                         ptrace(PTRACE_POKEDATA, program, (void*)regs.REG_SP, regs.REG_IP);                         \/\/ \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c RIP \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 evil_open                         regs.REG_IP = (unsigned long) evil_open;                         \/\/ \u0417\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430                         ptrace(PTRACE_SETREGS, program, 0, &amp;regs);                     }                 }                 ptrace(PTRACE_SYSCALL, program, 0, 0);                 waitpid(program, &amp;status, 0);             }         }         exit(0);     }     else {         sleep(0);     } } <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<pre><code class=\"plaintext\">$ .\/detect_syscall LD_PRELOAD (open syscall) [+] $ LD_PRELOAD=.\/ld_undetect_syscall.so .\/detect_syscall LD_PRELOAD (open syscall) [-] <\/code><\/pre>\n<p>  <b>+0-0=5<\/b><\/p>\n<p>  \u041e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e <\/p>\n<p>  <a href=\"https:\/\/github.com\/haxelion\">Charles Hubain<\/a><br \/>  <a href=\"https:\/\/github.com\/chokepoint\">Chokepoint<\/a><br \/>  <a href=\"https:\/\/habr.com\/ru\/users\/valdikss\/\">ValdikSS<\/a><br \/>  <a href=\"https:\/\/github.com\/doegox\">Philippe Teuwen<\/a><br \/>  <a href=\"https:\/\/stackoverflow.com\/users\/2327517\/derhass\"> derhass<\/a><\/p>\n<p>  , \u0447\u044c\u0438 \u0441\u0442\u0430\u0442\u044c\u0438, \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u044f, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0430 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0434\u0435\u0441\u044c.<\/p><\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/479858\/\"> https:\/\/habr.com\/ru\/post\/479858\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479858\/\">\u042d\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0430 \u0431\u044b\u043b\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 2014-\u043c \u0433\u043e\u0434\u0443, \u043d\u043e \u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u043e\u043f\u0430\u043b \u043f\u043e\u0434 \u0440\u0435\u043f\u0440\u0435\u0441\u0441\u0438\u0438 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0438 \u043e\u043d\u0430 \u043d\u0435 \u0443\u0432\u0438\u0434\u0435\u043b\u0430 \u0441\u0432\u0435\u0442. \u0417\u0430 \u0432\u0440\u0435\u043c\u044f \u0431\u0430\u043d\u0430 \u044f \u043f\u0440\u043e \u043d\u0435\u0451 \u0437\u0430\u0431\u044b\u043b, \u0430 \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0448\u0451\u043b \u0432 \u0447\u0435\u0440\u043d\u043e\u0432\u0438\u043a\u0430\u0445. \u0414\u0443\u043c\u0430\u043b \u0431\u044b\u043b\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u043d\u043e \u0430\u0432\u043e\u0441\u044c \u043a\u043e\u043c\u0443 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/o1\/8i\/wf\/o18iwf72_dlx0j9-qjr8zni5unq.png\"><\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u044f\u0442\u043d\u0438\u0447\u043d\u043e\u0435 \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u043e\u0435 \u0447\u0442\u0438\u0432\u043e \u043d\u0430 \u0442\u0435\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u0430 \u00ab\u0432\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0433\u043e\u00bb <i>LD_PRELOAD<\/i>.  <\/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-296102","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/296102","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=296102"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/296102\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=296102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=296102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=296102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}