<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" 
  xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:atom="http://www.w3.org/2005/Atom" 
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
  xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>esp8266 on Mal Breaks Things</title>
    <link>https://sec.gd/blog/tags/esp8266/</link>
    <description>Recent content in esp8266 on Mal Breaks Things</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>mal@sec.gd (mal)</managingEditor>
    <webMaster>mal@sec.gd (mal)</webMaster>
    <copyright>©2022. <a href="/blog/about/">License and Info</a></copyright>
    <lastBuildDate>Fri, 25 Jun 2021 04:49:17 +0000</lastBuildDate>
    <sy:updatePeriod>daily</sy:updatePeriod>
    
        <atom:link href="https://sec.gd/blog/tags/esp8266/index.xml" rel="self" type="application/rss+xml" />
    

      
      <item>
        <title>Minimally-Invasive Smart Outlet Surgery</title>
        <link>https://sec.gd/blog/posts/smart-outlet-hacking/</link>
        <pubDate>Fri, 25 Jun 2021 04:49:17 +0000</pubDate>
        <author>mal@sec.gd (mal)</author>
        <atom:modified>Fri, 25 Jun 2021 04:49:17 +0000</atom:modified>
        <guid>https://sec.gd/blog/posts/smart-outlet-hacking/</guid>
        <description>&lt;p&gt;I have several &lt;a href=&#34;https://smile.amazon.com/gp/product/B07DCJ7TDR/&#34;&gt;EFUN SH331W&lt;/a&gt;
smart outlets for controlling various lights. They&amp;rsquo;re based on a whitelabel
ESP8266 design by Tuya, so I usually use &lt;a href=&#34;https://github.com/ct-Open-Source/tuya-convert&#34;&gt;tuya-convert&lt;/a&gt;
to flash them with &lt;a href=&#34;https://tasmota.github.io/&#34;&gt;Tasmota&lt;/a&gt; so I can control them
with MQTT. Unfortunately tuya-convert is a rather tedious and error-prone
process, and recently I managed to soft-brick one.&lt;/p&gt;</description>
        <content:encoded>&lt;p&gt;I have several &lt;a href=&#34;https://smile.amazon.com/gp/product/B07DCJ7TDR/&#34;&gt;EFUN SH331W&lt;/a&gt;
smart outlets for controlling various lights. They&amp;rsquo;re based on a whitelabel
ESP8266 design by Tuya, so I usually use &lt;a href=&#34;https://github.com/ct-Open-Source/tuya-convert&#34;&gt;tuya-convert&lt;/a&gt;
to flash them with &lt;a href=&#34;https://tasmota.github.io/&#34;&gt;Tasmota&lt;/a&gt; so I can control them
with MQTT. Unfortunately tuya-convert is a rather tedious and error-prone
process, and recently I managed to soft-brick one.&lt;/p&gt;
&lt;p&gt;Recovery for some devices is easy, you follow the normal
&lt;a href=&#34;https://tasmota.github.io/docs/Getting-Started/&#34;&gt;Tasmota install instructions&lt;/a&gt;:
Pop it open, connect TX, RX, GPIO0 to ground, GND, and 3.3v. When GPIO0
(aka pin IO0) is pulled down the ESP boots to flashing mode, and you can use
&lt;a href=&#34;https://github.com/espressif/esptool&#34;&gt;esptool&lt;/a&gt; to flash tasmota directly.&lt;/p&gt;
&lt;p&gt;Opening this device is less than straightforward — it&amp;rsquo;s a sealed plastic
shell, and even with the most careful disassembly it would be pretty annoying
to glue back together.&lt;/p&gt;
&lt;p&gt;On the other hand, it would be a shame to have to toss a device that&amp;rsquo;s only &lt;em&gt;soft&lt;/em&gt;-bricked&amp;hellip;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s use another one with bad flash memory&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; to see what&amp;rsquo;s going on inside.&lt;/p&gt;
&lt;h2 id=&#34;investigatory-dissection&#34;&gt;Investigatory Dissection&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Or vivisection, I guess?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;After a lot of careful cutting and prying around the edges of the unit, and a
small blood sacrifice&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;, the innards were revealed. The ESP is its own
module, sticking up from the main board, with all of the contacts we need
hiding between the main board and the plastic back of the unit.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../images/smart-outlet-hacking/esp-module.jpg&#34; alt=&#34;ESP module on board&#34;&gt;&lt;/p&gt;
&lt;p&gt;The module EFUN used is an &lt;a href=&#34;https://fccid.io/2AKBPESP8266-S3/User-Manual/User-Manual-3594791&#34;&gt;ESP8266-S3&lt;/a&gt;
by Hysiry. We need access to pins 7 (IO0/GPIO0) to pull down for flashing
mode, 11 and 12 (URXD/UART0_RXD, UTXD/UART0_TXD) for communication, and 13
and 14 (VCC/VDD, GND) for power. On many devices GPIO0 is connected to the
external button, but the &lt;a href=&#34;https://templates.blakadder.com/efun_SH331W.html&#34;&gt;Tasmota template&lt;/a&gt;
for this device shows it&amp;rsquo;s used by a status LED instead.&lt;/p&gt;
&lt;p&gt;At this point I drilled five small holes in the plastic, expecting to stick
jumper wires through to touch the solder joints for the module. After spending
far too much time trying to hold all five in place with one hand while typing
&lt;code&gt;picocom&lt;/code&gt; commands with the other, I gave up and desoldered the board from
the mains pins so I could add wires.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../images/smart-outlet-hacking/board-back.jpg&#34; alt=&#34;Bottom of circuit board with five fine wires soldered on&#34;&gt;
&lt;img src=&#34;../../images/smart-outlet-hacking/board-front.jpg&#34; alt=&#34;Top of circuit board with wires coming from underneath&#34;&gt;&lt;/p&gt;
&lt;p&gt;Now able to make a reliable physical connection, it was time to find several
hundred ways not to communicate with it. To make a long story short, the
&lt;a href=&#34;https://www.sparkfun.com/products/12942&#34;&gt;bus pirate&lt;/a&gt; continues its legacy of
technically working and being just helpful enough to not warrant replacing.&lt;/p&gt;
&lt;p&gt;It seems the ESP, at least at this clock speed, uses a baudrate of 74880.
This isn&amp;rsquo;t one the bus pirate offers, so I had to use the &amp;ldquo;BRG raw value&amp;rdquo; option.&lt;/p&gt;
&lt;p&gt;BRG refers to the PIC Baud Rate Generator and the register used to configure
it. It&amp;rsquo;s described in section 17.1 of the
&lt;a href=&#34;https://ww1.microchip.com/downloads/en/devicedoc/39881e.pdf&#34;&gt;PIC24FJ64GA datasheet&lt;/a&gt;.
So for this bus pirate&amp;rsquo;s microcontroller, clock frequency, and firmware that
uses BRGH=1, &lt;code&gt;brg=(4000000/baudrate)-1&lt;/code&gt;, so for 74880 baud we use a BRG of 52.&lt;/p&gt;
&lt;h2 id=&#34;connecting&#34;&gt;Connecting&lt;/h2&gt;
&lt;p&gt;For wiring, use the bus pirate&amp;rsquo;s GND to pin 7 and either pin 14 or the neutral
prong, 3V3 to pin 13, MOSI to pin 11, and MISO to pin 12. &lt;strong&gt;Do not connect mains power.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The final incantation for the bus pirate:&lt;br&gt;
&lt;code&gt;$ picocom -b 115200 /dev/ttyUSB0&lt;/code&gt;&lt;br&gt;
&lt;code&gt;b&lt;/code&gt; to change host&amp;lt;-&amp;gt;bp baudrate&lt;br&gt;
&lt;code&gt;10&lt;/code&gt; for &amp;ldquo;BRG raw value&amp;rdquo;&lt;br&gt;
&lt;code&gt;52&lt;/code&gt; for 74880 baud&lt;br&gt;
&lt;code&gt;Ctrl+a Ctrl+b 74880&lt;/code&gt; to set picocom&amp;rsquo;s baudrate&lt;br&gt;
&lt;code&gt;m&lt;/code&gt; to change the bp&amp;lt;-&amp;gt;device mode&lt;br&gt;
&lt;code&gt;2&lt;/code&gt; for UART&lt;br&gt;
&lt;code&gt;10&lt;/code&gt; for &amp;ldquo;BRG raw value&amp;rdquo;&lt;br&gt;
&lt;code&gt;52&lt;/code&gt; for 74880 baud&lt;br&gt;
Default data bits and parity, stop bits, and receive polarity (8N1, Idle 1)&lt;br&gt;
&lt;code&gt;2&lt;/code&gt; for &amp;ldquo;Normal (H=3.3V, L=GND)&amp;rdquo; output&lt;br&gt;
&lt;code&gt;W&lt;/code&gt; to enable the power supply&lt;br&gt;
&lt;code&gt;(1)&lt;/code&gt; to start transparent bridge mode&lt;/p&gt;
&lt;p&gt;You should then be able to see a bootloader message when removing and
replacing power for the device. You can exit picocom with &lt;code&gt;Ctrl+a Ctrl+q&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With picocom closed, you can use &lt;code&gt;esptool&lt;/code&gt; to communicate with the ESP.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Test communication:&lt;br&gt;
&lt;code&gt;esptool -p /dev/ttyUSB0 --chip esp8266 -b 74880 read_mac&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flash tasmota (tasmota.bin from the &lt;a href=&#34;https://github.com/arendst/Tasmota/releases&#34;&gt;releases page&lt;/a&gt;)&lt;br&gt;
&lt;code&gt;esptool -p /dev/ttyUSB0 --chip esp8266 -b 74880 write_flash -e -fs 1MB -fm dout 0x0 tasmota.bin&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;../../images/smart-outlet-hacking/wires.jpg&#34; alt=&#34;Bus pirate connected to vivisection subject&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;the-living-patient&#34;&gt;The Living Patient&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;It&amp;rsquo;s go time.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now that I can directly flash the sacrificial device, it&amp;rsquo;s time to set up the
one I hope to save.&lt;/p&gt;
&lt;p&gt;Even knowing I was communicating with the ESP correctly, I wasn&amp;rsquo;t particularly
excited about holding jumper wires in place. I realized I could reduce the
number of wires I needed by one if I could connect to the mains neutral
instead of the ESP&amp;rsquo;s ground contact, which worked. I also decided to use hot
pins to poke through the plastic, since sufficiently precise pin placement
would mean minimal manual holding.&lt;/p&gt;
&lt;p&gt;The only ESP module information I could find gave exactly one relevant
measurement, that the edge connector was 16.4mm. Calipers provided the rest&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Based on these measurements, I needed pins at the following locations:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;pin&lt;/th&gt;
&lt;th&gt;mm from plug end&lt;/th&gt;
&lt;th&gt;mm from ground side&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GPIO0&lt;/td&gt;
&lt;td&gt;22.45&lt;/td&gt;
&lt;td&gt;5.65&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTXD&lt;/td&gt;
&lt;td&gt;27.50&lt;/td&gt;
&lt;td&gt;7.90&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;URXD&lt;/td&gt;
&lt;td&gt;27.50&lt;/td&gt;
&lt;td&gt;5.65&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VCC&lt;/td&gt;
&lt;td&gt;30.02&lt;/td&gt;
&lt;td&gt;5.65&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Lastly, GND connects to neutral, the flat prong farther from the button.&lt;/p&gt;
&lt;p&gt;I measured with calipers, marked the grid with a knife, held each pin with
pliers above a lighter to heat them until they glowed, and stuck them straight
down at each location.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;One ended up a bit off-vertical, so I bent the pin in the other direction and
reinserted it. Two more had plastic or soot on the tip, so those needed to be
pulled, cleaned, and reinserted. Finally, with all the leads attached and
gentle pressure on the pinheads&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../images/smart-outlet-hacking/under-the-knife.jpg&#34; alt=&#34;Partly-living smart outlet with pins and alligator clips connected&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt; ets Jan  8 2013,rst cause:1, boot mode:(1,0)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;It lives! Disconnect picocom, run esptool, and&amp;hellip;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Wrote 473856 bytes (328288 compressed) at 0x00000000 it 44.8 seconds (effective 84.6 kbit/s)...
Hash of data verified.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Pull the pins off, plug it in, and!&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../images/smart-outlet-hacking/tasmota-ssid.png&#34; alt=&#34;Screenshot of tasmota SSID connected on phone&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I spent several afternoons saving a $5 smart outlet.
And then too long (in both words and time) writing it up, but I&amp;rsquo;m trying to
document more stuff I do on the off chance some part of it ends up being
useful to someone. Half the time that someone is just me, six months later.&lt;/p&gt;
&lt;p&gt;This is probably also not the best idea from a standpoint of not lighting
things on fire - it&amp;rsquo;s doing things that weren&amp;rsquo;t meant to be done to a device
that controls up to 20 amps at 120 volts. Your device may also not be
identical to mine. Attempting to follow these steps may break your smart
outlet, make it burn your house down, or call your dog a bad girl, and I
accept no responsibility for any of it.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;It pretends to work fine, but forgets its settings about once a week.
Useful for this, since I can test flashing but don&amp;rsquo;t feel bad about destroying it.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;
&lt;p&gt;If you don&amp;rsquo;t have the right tool handy, just go find it.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34;&gt;
&lt;p&gt;1.25mm plastic wall above ledge, 3mm plug side board inset, 10mm from
board edge to ESP module connector edge, 20.85mm from ESP module other end to
other board end, 4mm button side board inset, 1.25mm wall. Sum of 56.75mm,
measured size is 57mm - not bad. From the ground side, 1.25mm wall, 1.65mm
board inset, module 2.75mm from board edge.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:4&#34;&gt;
&lt;p&gt;No blood sacrifice was demanded at this step, but I still should have
gone and dug out the pliers &lt;em&gt;before&lt;/em&gt; the first mild burn.&amp;#160;&lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content:encoded>
        <dc:creator>mal</dc:creator>
        <media:content url="https://sec.gd/blog/images/smart-outlet-hacking/thumbnail.jpg" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        <media:content url="https://sec.gd/blog/images/smart-outlet-hacking/under-the-knife.jpg" medium="image"><media:title type="html">meta image</media:title></media:content>
        
          
            
              <category>fixed</category>
            
          
            
              <category>esp8266</category>
            
          
            
              <category>smart home</category>
            
          
            
              <category>hardware</category>
            
          
            
              <category>embedded</category>
            
          
        
        
        
      </item>
      

    
  </channel>
</rss>
