Debugging and security testing with Firefox OS

  • Revision slug: Mozilla/Firefox_OS/Debugging_and_Security_Testing_with_Firefox_OS
  • Revision title: Debugging and Security Testing with Firefox OS
  • Revision id: 371027
  • Created:
  • Creator: freddyb
  • Is current revision? No
  • Comment

Revision Content

 Security Testing on Firefox OS

This guide is aimed at security testers wanting to start testing Firefox OS, to help community members audit Apps and the Firefox OS platform itself. If you haven't already, start by reading the Firefox OS Security Overview  - this will provide a basic background in the the terminology and architecture discussed below.

This guide covers the following techniques:

  • Part 1: Getting started with a desktop build of Firefox OS, and using Marionette to inspect and control Apps running on Firefox OS
  • Part 2: Intercepting traffic using a proxy
  • Part 3: Modifying Gaia: how to run B2G desktop with a custom profile, and how to create and install your own Apps

All of our testing will happen against b2g-desktop, a native Firefox OS built for your desktop computer. Start downloading your flavor of nightly build (Linux, Mac OS, Windows) before you read on.

Getting started with b2g-desktop

Okay, did you already download your flavor of nightly build (Linux, Mac OS, Windows)? Then let's go.

Linux

Setting up b2g-desktop is as simple as extracting the archive and running the b2g binary:

tar xf b2g-something-something.tar.bz2
cd b2g
./b2g

Mac

Open the .dmg file, and copy it to your /Applications directory. Once copied, launch b2g-desktop by clicking the b2g.app icon. Alternatively you can launch it from terminal as follows:

/Applications/B2G.app/Contents/MacOS/b2g

Windows

Download and extract the zip file to a convenient location. Double-click on b2g.exe to start b2g-desktop.

At the time of writing, there is an issue running b2g-desktop on windows. You might be able to try the Firefox OS simulator instead.

Getting started tips

You can now play with a Firefox OS in a desktop window. Go play around: Open the browser (lower right window) and visit a web page, try opening a few apps. You will notice that some device specific functionality, such as the dialer, camera, radio etc wont work for obvious reasons.

Make yourself comfortable with b2g-desktop. The key bindings are as follows:

  • Home button: Home key (Mac: function + left )
  • Power button: End key (Mac: function + right )
  • Volume button: Page Up/Down keys (Mac: function + up/down )
  • Open Cards View: long press to Home key

Now to the real deal. We want to make JavaScript calls from inside.

Marionette: Getting a JavaScript Debugging Shell for Firefox OS

Marionette is based on the Selenium/WebDriver API to automate Firefox OS testing. This is helping us to get a JavaScript shell within Firefox OS or a running app. First, get the marionette client from github (written in Python) and follow the installation instructions given there.

Note: Recent testings have shown that you might have problems with certain versions of mozbase. The last known-working state of mozbase is at commit 9ee2de.

Setting up b2g-desktop with marionette is as easy as a pie, just make it that your b2g profile (like in the Firefox browser, all user settings are stored within a profile) has marionette enabled:

To do so, add the following line to your prefs.js file in gaia/profile/.

user_pref("marionette.force-local", true);

This will enable marionette-debugging to listen on port 2828 and allow the installed client to connect. This will suffice to get started, but if you want to know more look at the full Marionette docs at MDN. After we have enabled marionette-debugging, restart the b2g binary. You will see no difference in the b2g window, but it is already listening on port 2828.

Now, instead of spinning up a Python console, importing the marionette-client library and creating a debugging session, we can do something else: Stefan Arentz has created a simple JavaScript console that runs on top of the marionette-client library. So, let's download fxos-repl.py.

For our example, we will remote-control the Browser App. Start it by clicking the browser icon in the b2g home screen's lower right with your mouse. Is the b2g binary showing the browser App? Good, here we go debugging it:

$ python fxos-repl.py list
app://homescreen.gaiamobile.org/index.html#root
app://browser.gaiamobile.org/index.html
app://keyboard.gaiamobile.org/index.html

Aha, these are the running Apps and their URIs.
Now let's connect to the browser App, and investigate:

$ python fxos-reply.py connect app://browser.gaiamobile.org/index.html
Connected to app://browser.gaiamobile.org/index.html

Let's use the querySelector API to find a <menu> tag with id="toolbar-start" and view it's HTML:

>>> document.querySelector("menu#toolbar-start").outerHTML
<menu type="toolbar" id="toolbar-start">
        <form id="url-bar" novalidate="">
          <input id="ssl-indicator" value="" type="image">
          <input id="url-input" placeholder="Enter search or address" data-l10n-id="enter-search-or-address" x-inputmode="verbatim" type="text">
          <input style="background-image: url(&quot;style/images/go.png&quot;);" id="url-button" value="" type="image">
        </form>
        <span id="tabs-badge">1<span id="more-tabs">›</span></span>
        <button id="awesomescreen-cancel-button"></button>
        <div id="throbber"></div>
      </menu>

Does it look familiar? It's the toolbar that contains the address bar and the New Tab button!

Let's click this button. It's <span id=tabs-badge>:

>>> document.querySelector("#tabs-badge").click()

The New Tab UI should show now! Let's try something else:

>>> alert(location.href)

This is it for executing your JavaScript code with in a running application's scope.

Now go, play!

The next part

Intercept Firefox OS Traffic Using a Proxy

Revision Source

<h2 id=".C2.A0Security_Testing_on_Firefox_OS">&nbsp;Security Testing on Firefox OS</h2>
<p>This guide is aimed at security testers wanting to start testing Firefox OS, to help community members audit Apps and the Firefox OS platform itself. If you haven't already, start by reading the <a href="/en-US/docs/Mozilla/Firefox_OS/Security/Security_model" title="/en-US/docs/Security/Firefox_Security_Basics_For_Developers">Firefox OS Security Overview&nbsp;</a> - this will provide a basic background in the the terminology and architecture discussed below.</p>
<p>This guide covers the following techniques:</p>
<ul>
  <li>Part 1: Getting started with a desktop build of Firefox OS, and using Marionette to inspect and control Apps running on Firefox OS</li>
  <li>Part 2: Intercepting traffic using a proxy</li>
  <li>Part 3: Modifying Gaia: how to run B2G desktop with a custom profile, and how to create and install your own Apps</li>
</ul>
<p>All of our testing will happen against <em>b2g-desktop</em>, a native Firefox OS built for your desktop computer. Start downloading your <a data-mce-="" href="http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-b2g18/">flavor of nightly build (Linux, Mac OS, Windows)</a> before you read on.</p>
<h2 id="Getting_started_with_b2g-desktop">Getting started with b2g-desktop</h2>
<p>Okay, did you already download your <a data-mce-="" href="http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-b2g18/">flavor of nightly build (Linux, Mac OS, Windows)</a>? Then let's go.</p>
<h4 id="Linux">Linux</h4>
<p>Setting up b2g-desktop is as simple as extracting the archive and running the b2g binary:</p>
<pre>
tar xf b2g-something-something.tar.bz2
cd b2g
./b2g</pre>
<h4 id="Mac">Mac</h4>
<p>Open the .dmg file, and copy it to your /Applications directory. Once copied, launch b2g-desktop by clicking the b2g.app icon. Alternatively you can launch it from terminal as follows:</p>
<pre>
/Applications/B2G.app/Contents/MacOS/b2g</pre>
<h4 id="Windows">Windows</h4>
<p>Download and extract the zip file to a convenient location. Double-click on b2g.exe to start b2g-desktop.</p>
<p>At the time of writing, there is an issue running b2g-desktop on windows. You might be able to try the <a href="https://addons.mozilla.org/en-US/firefox/addon/firefox-os-simulator/" title="https://addons.mozilla.org/en-US/firefox/addon/firefox-os-simulator/">Firefox OS simulator</a> instead.</p>
<h3 id="Getting_started_tips">Getting started tips</h3>
<p>You can now play with a Firefox OS in a desktop window. Go play around: Open the browser (lower right window) and visit a web page, try opening a few apps. You will notice that some device specific functionality, such as the dialer, camera, radio etc wont work for obvious reasons.</p>
<p>Make yourself comfortable with <em>b2g-desktop</em>. The key bindings are as follows:</p>
<ul>
  <li>Home button: Home key (Mac: function + left )</li>
  <li>Power button: End key (Mac: function + right )</li>
  <li>Volume button: Page Up/Down keys (Mac: function + up/down )</li>
  <li>Open Cards View: long press to Home key</li>
</ul>
<p>Now to the real deal. We want to make JavaScript calls from inside.</p>
<h2 id="Marionette.3A_Getting_a_JavaScript_Debugging_Shell_for_Firefox_OS">Marionette: Getting a JavaScript Debugging Shell for Firefox OS</h2>
<p>Marionette is based on the Selenium/WebDriver API to automate Firefox OS testing. This is helping us to get a JavaScript shell within Firefox OS or a running app. First, get the <a data-mce-="" href="https://github.com/mozilla/marionette_client">marionette client from github</a> (written in Python) and follow the installation instructions given there.</p>
<p style="margin-left: 40px;"><strong>Note</strong>: Recent testings have shown that you might have problems with certain versions of mozbase. The last known-working state of mozbase is at <a href="https://github.com/mozilla/mozbase/tree/9ee2dee5fdde6cf5d6bf7caac4db6673112df79d/" title="https://github.com/mozilla/mozbase/tree/9ee2dee5fdde6cf5d6bf7caac4db6673112df79d/">commit 9ee2de</a>.</p>
<p>Setting up <em>b2g-desktop </em>with marionette is as easy as a pie, just make it that your b2g profile (like in the Firefox browser, all user settings are stored within a profile) has marionette enabled:</p>
<p>To do so, add the following line to your prefs.js file in gaia/profile/.</p>
<pre>
user_pref("marionette.force-local", true);</pre>
<p>This will enable marionette-debugging to listen on port 2828 and allow the installed client to connect. This will suffice to get started, but if you want to know more look at the <a data-mce-="" href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Platform/Testing/Setting_up_Marionette">full Marionette docs at MDN.</a> After we have enabled marionette-debugging, restart the b2g binary. You will see no difference in the b2g window, but it is already listening on port 2828.</p>
<p>Now, instead of spinning up a Python console, importing the marionette-client library and creating a debugging session, we can do something else: Stefan Arentz has created a simple JavaScript console that runs on top of the marionette-client library. So, let's download <a data-mce-="" href="https://raw.github.com/st3fan/fxos-repl/master/fxos-repl.py">fxos-repl.py</a>.</p>
<p>For our example, we will remote-control the Browser App. Start it by clicking the browser icon in the b2g home screen's lower right with your mouse. Is the b2g binary showing the browser App? Good, here we go debugging it:</p>
<pre>
$ <strong>python fxos-repl.py list</strong>
app://homescreen.gaiamobile.org/index.html#root
app://browser.gaiamobile.org/index.html
app://keyboard.gaiamobile.org/index.html</pre>
<p>Aha, these are the running Apps and their URIs.<br />
  Now let's connect to the browser App, and investigate:</p>
<pre>
$ <strong>python fxos-reply.py connect app://browser.gaiamobile.org/index.html</strong>
Connected to app://browser.gaiamobile.org/index.html</pre>
<p>Let's use the querySelector API to find a <code>&lt;menu&gt;</code> tag with <code>id="toolbar-start"</code> and view it's HTML:</p>
<pre>
&gt;&gt;&gt; <strong>document.querySelector("menu#toolbar-start").outerHTML</strong>
&lt;menu type="toolbar" id="toolbar-start"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;form id="url-bar" novalidate=""&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input id="ssl-indicator" value="" type="image"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input id="url-input" placeholder="Enter search or address" data-l10n-id="enter-search-or-address" x-inputmode="verbatim" type="text"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input style="background-image: url(&amp;quot;style/images/go.png&amp;quot;);" id="url-button" value="" type="image"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/form&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;span id="tabs-badge"&gt;1&lt;span id="more-tabs"&gt;›&lt;/span&gt;&lt;/span&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;button id="awesomescreen-cancel-button"&gt;&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;div id="throbber"&gt;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/menu&gt;</pre>
<p>Does it look familiar? It's the toolbar that contains the address bar and the New Tab button!</p>
<p>Let's click this button. It's <em><code>&lt;span id=tabs-badge&gt;</code></em>:</p>
<pre>
&gt;&gt;&gt; <strong>document.querySelector("#tabs-badge").click()</strong></pre>
<p>The New Tab UI should show now! Let's try something else:</p>
<pre>
&gt;&gt;&gt; <strong>alert(location.href)</strong></pre>
<p>This is it for executing your JavaScript code with in a running application's scope.</p>
<p>Now go, play!</p>
<h3 id="The_next_part">The next part</h3>
<p><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Intercept_Firefox_OS_Traffic_Using_a_Proxy" title="/en-US/docs/Mozilla/Firefox_OS/https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Intercept_Firefox_OS_Traffic_Using_a_Proxy">Intercept Firefox OS Traffic Using a Proxy</a></p>
Revert to this revision