<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>exploit - Grohs Fabian</title>
	<atom:link href="https://grohsfabian.com/tag/exploit/feed/" rel="self" type="application/rss+xml" />
	<link>https://grohsfabian.com/tag/exploit/</link>
	<description>Yet another web developer with a blog 🤷‍♂️.</description>
	<lastBuildDate>Fri, 04 Nov 2022 21:24:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://grohsfabian.com/wp-content/uploads/2020/03/6iK6Agse_400x400-150x150.jpg</url>
	<title>exploit - Grohs Fabian</title>
	<link>https://grohsfabian.com/tag/exploit/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Fixing XSS Through SVG File Uploads PHP</title>
		<link>https://grohsfabian.com/xss-through-svg-file-uploads-how-to-fix-with-php/</link>
					<comments>https://grohsfabian.com/xss-through-svg-file-uploads-how-to-fix-with-php/#respond</comments>
		
		<dc:creator><![CDATA[grohsfabian]]></dc:creator>
		<pubDate>Fri, 04 Nov 2022 21:23:59 +0000</pubDate>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[vulnerability]]></category>
		<category><![CDATA[xss]]></category>
		<guid isPermaLink="false">https://grohsfabian.com/?p=423</guid>

					<description><![CDATA[<p>Using Javascript code inside of an SVG file and then uploading it to a website that accepts SVG files &#38; does not sanitize their content. This is how a ton of websites fail and are directly exposed to this XSS vulnerability. You have two choices: How is this exploit working? Any type of JS code ... <a title="Fixing XSS Through SVG File Uploads PHP" class="read-more" href="https://grohsfabian.com/xss-through-svg-file-uploads-how-to-fix-with-php/" aria-label="Read more about Fixing XSS Through SVG File Uploads PHP">Read more</a></p>
<p>The post <a href="https://grohsfabian.com/xss-through-svg-file-uploads-how-to-fix-with-php/">Fixing XSS Through SVG File Uploads PHP</a> appeared first on <a href="https://grohsfabian.com">Grohs Fabian</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Using Javascript code inside of an SVG file and then uploading it to a website that accepts SVG files &amp; does not sanitize their content.</p>



<p>This is how a ton of websites fail and are directly exposed to this XSS vulnerability.</p>



<p>You have two choices:</p>



<ul class="wp-block-list">
<li>Do not allow users to upload SVG files</li>



<li>Allow users to upload SVG files, but use an SVG cleaner on upload</li>
</ul>



<span id="more-423"></span>



<h2 class="wp-block-heading">How is this exploit working?</h2>



<p>Any type of JS code can be added inside an SVG file.</p>



<p>You upload the SVG image file to a website that allows SVG file uploads and does not clean them.</p>



<p>If you manage to get the URL of that uploaded SVG file &amp; that file is saved in the same domain of the main website, the exploit is complete.</p>



<p>You can write malicious JS code to dump cookies (for example) wherever you want to.</p>



<p>You can then simply send the SVG URL to any user of that particular website which you are also a part of, and if that user opens it the JS code will execute.</p>



<h2 class="wp-block-heading">How to fix it?</h2>



<p>The fix to the SVG XSS vulnerability is simple.</p>



<p>Let&#8217;s assume that you still want your users to be able to upload SVG files.</p>



<p>In that case, you would need to use an SVG cleaner before you store the uploaded file.</p>



<p>We&#8217;re going to use an SVG cleaner library: <a href="https://github.com/darylldoyle/svg-sanitizer" target="_blank" rel="noreferrer noopener nofollow">daryll/svg-sanitizer</a></p>



<h2 class="wp-block-heading">Code example</h2>



<p>I&#8217;m going to write an extremely simple example to illustrate how this can be solved with ease.</p>



<p>First off, make sure that you download the SVG-sanitizer code from the above SVG cleaner library.</p>



<p>If you use composer, simply require the library into your code.</p>



<p>Let&#8217;s say you have the following upload processing code</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
/* Load the autoload file from vendor */
require_once './vendor/autoload.php';

/* Check for form submission */
if(!empty($_POST)) {
    $file_extension = explode('.', $_FILES['image']['name']);
    $file_extension = mb_strtolower(end($file_extension));
    $file_temp = $_FILES['image']['tmp_name'];

    /* THIS IS JUST AN EXAMPLE */
    /* Here you typically do your form processing and validations */

    /* Generate a new file name */
    $file_new_name = md5(time(). rand()) . '.' . $file_extension;

    /* Upload the file */
    move_uploaded_file($file_temp, realpath(__DIR__) . '/images/' . $file_new_name);
}
?></pre>



<p>As mentioned in the code, this is just an example. </p>



<p>Normally, you have more validations and error checks before you upload the file.</p>



<h2 class="wp-block-heading">Clean the SVG</h2>



<p>Now, let&#8217;s clean the SVG file with the library that we have imported.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if($file_extension == 'svg') {
    $svg_sanitizer = new \enshrined\svgSanitize\Sanitizer();
    $dirty_svg = file_get_contents($file_temp);
    $clean_svg = $svg_sanitizer->sanitize($dirty_svg);
    file_put_contents($file_temp, $clean_svg);
}</pre>



<p>This code will only run if the file extension is &#8216;svg&#8217;.</p>



<p>It will take the temporary file which was uploaded.</p>



<p>Then put it through the SVG Cleaner.</p>



<p>And save it back into the initial temporary file.</p>



<h2 class="wp-block-heading">Final code example</h2>



<p>The full PHP code would look something like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
require_once './vendor/autoload.php';

/* Check for form submission */
if(!empty($_POST)) {
    $file_extension = explode('.', $_FILES['image']['name']);
    $file_extension = mb_strtolower(end($file_extension));
    $file_temp = $_FILES['image']['tmp_name'];

    /* THIS IS JUST AN EXAMPLE */
    /* Here you typically do your form processing and validations */

    if($file_extension == 'svg') {
        $svg_sanitizer = new \enshrined\svgSanitize\Sanitizer();
        $dirty_svg = file_get_contents($file_temp);
        $clean_svg = $svg_sanitizer->sanitize($dirty_svg);
        file_put_contents($file_temp, $clean_svg);
    }


    $file_new_name = md5(time(). rand()) . '.' . $file_extension;

    /* Upload the original */
    move_uploaded_file($file_temp, realpath(__DIR__) . '/images/' . $file_new_name);
}
?></pre>



<h2 class="wp-block-heading">Video tutorial</h2>



<p>I have also recorded a video tutorial on how you can implement this library and fix this XSS SVG exploit in under 5 minutes.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="XSS Through SVG File Uploads - How to fix - PHP" width="920" height="518" src="https://www.youtube.com/embed/1YojtYHc-Ng?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Please, make sure that you clean all the SVG file uploads, as otherwise, you are exposing yourself to risk for no reason.</p>



<p>Hope you&#8217;ve found this useful, take care!</p>
<p>The post <a href="https://grohsfabian.com/xss-through-svg-file-uploads-how-to-fix-with-php/">Fixing XSS Through SVG File Uploads PHP</a> appeared first on <a href="https://grohsfabian.com">Grohs Fabian</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://grohsfabian.com/xss-through-svg-file-uploads-how-to-fix-with-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
