Introduction: Building Garage IoT: a Hacker’s Journey

Picture of Building Garage IoT: a Hacker’s Journey

Shortly after moving into my new home, I found a key switch (above) near the front of my garage. I tested all the keys I was given and none of them worked. At the time, I had lots of bigger projects to work on, so I left it at that. Eventually, I got around to taking a closer look and I traced the key switch’s wires to the inside button that open and closed my garage. Hmmmm. I quickly cut and striped the wires near the key switch and found that if I touched them together, my garage would open! I did not like this! Anyone with a pair of vise grips, or better yet a dremel, could remove the “security bolts”, unmount the switch from the outside, cut the wires, and touch them to get into my garage. Even if I replaced the key switch with a keypad, or retina scanner for that matter, it would still be vulnerable to the previously outlined attack if it was using those 2 wires. I needed to somehow make the wiring that an attacker could access be independent from the logic to open the garage or not.

I had two ideas. The first idea was to use a microcontroller and a keypad. The keypad would simply communicate number key presses to the microcontroller, which would chose to open the garage or not. This way if an attacker gained access to the wires, there would be nothing they could short to open the garage. Well, that’s not technically true, because they could short the correct button wires in the correct order to open the garage, but you get the point. The second idea was to use a microcontroller running a web server that I could connect to using my phone to open the garage. The beauty here is that there are no wires exposed for an attacker, however, there are other things to consider. For example, the security and access of my garage via this channel hinges on the availability and security of the web server on the microcontroller. I ultimately decided to build the later because it’s way cooler to open my garage from my phone and it would be a good exercise at attempting to build and secure an IoT device. I am penetration tester, so making sure the device is secure was very important to me.

Step 1: The Electronics

Picture of The Electronics

I decided to use a RaspberryPi Zero W. It’s a cheap device (~$10) that boasts 17 GPIO pins, runs full Linux, and has built-in wireless. Before devising a circuit, I had to figure out what the two garage wires were doing. I grabbed my multimeter and found through various tests that one wire supplied 5 volts DC (let’s call it power) and the other opened the garage when supplied the 5 volts (let’s call it input). I had a few options. I could pull the input wire high (meaning give it 5 volts) using the RaspberryPi, but I didn’t know if this would work given the RaspberryPi and garage don’t share a ground. I could use a transistor, but again I didn’t know if it would work given the common ground issue. Additionally, both of those ideas don’t protect the RaspberryPi from the garage, or vise versa, in case of overload or other failures. I ultimately went with a relay, which is a device that uses an electromagnet to mechanically operate a switch. This would provide complete isolation between the RaspberryPi and garage, while removing the potential for any ground related issues.

Lastly, I have a two car garage with two single garage doors, so I will need a circuit for both if I want to control both. With those considerations, here is a schematic and parts list of the final system:

Step 2: GPIO Software

Picture of GPIO Software

When developing the software, I wanted to challenge myself as if I was developing an IoT device for market. I wanted to consider things like ease of use, size, embedded resource limitations, cost, and etc. as if it was a real IoT device.

I had no real hardware experience prior to this project, so I spent lots of time experimenting at the command line with GPIO things to get an understanding of what I wanted my code to do. The most useful tool I found was called gpio. I used it to read and write the state of my pins, as well as change the modes. This allowed me to test and play with my relays relatively quickly. Above are examples of the commands I used most.

After building a decent understand of GPIO and learning how to interact with them using the gpio tool, it was time to bust out some Python. There is a GPIO Python library called gpiozero that contained all the GPIO functionality needed to switch my relays. Above is the snippet of my code that defines and initializes my pins using gpiozero. From here I can pull the pins high or low, which in my case opens or closes the relay, as seen above.

That takes care of all my GPIO interactions, now all I had to do was build a UI and secure it.

Step 3: UI and Securities Software

Picture of UI and Securities Software

As usual, a simple web application is the easiest and fastest UI. Even though I only intended to host it on my internal LAN, I still didn’t want to leave it wide-open for anyone to use. I didn’t necessarily want to mess with users and sessions, as that increases the difficulty of development while decreasing the device’s ease of use. Logging into a web application is the last thing I would want to do when grabbing a moped from the garage or etc. Additionally, no IoT manufacturer would want this either. I decided to use client certificates, which is a type of digital certificate that is used by client systems to make authenticated requests to a remote server (Thanks Wikipedia). This would add robust authentication, without having the hassle of usernames and passwords. Using client certificates, I would simply install the certificate on devices I want to have access, while everything else will get denied. I was able to find a really awesome thread on Reddit that described how to use client certificates in Flask, a web-framework for Python. It’s as simple as building an “SSLContext” and then starting the app object with it, as seen above.

I generated my keys using openssl like so: To setup a device to use the web application, all I had to do was install the “example.p12” certificate into the browser. On Android, I went to “Settings > Security > Install from storage” and then selected “example.p12”. Now when I visit my web application, it asks me what certificate to authenticate with as seen above.

Now with my authentication sorted out, the last thing I wanted to do was mitigate CSRF. If I was a real IoT manufacturer and my devices were in lots of people’s homes, it would be pretty bad if you could get a user’s garage to open just by having that user click a malicious link or visit a hacked site. While Flask doesn’t mitigate CSRF out-of-the-box, WTForms does and there is a Python module to integrate Flask and WTForms, rightfully called Flask WTF. Using WTForms with CSRF enabled, I can create a POST form that ultimately controls my garage and won’t be vulnerable to CSRF. In order for CSRF nonce tokens to be generated, the Flask app object needs to have a secret set. Above are my codes snippets pertaining to setting the secret, creating a WTForm, and building an HTML page to render. At the bottom of the above snippet you can see the page the form POSTs to, which ultimately controls the GPIO relays. If you notice, I’m using the blink() method. I originally just used on() and off(), but later after RTFM’ing on my garage, I found out I needed to pulse the relay twice in order to trigger the garage. The blink() method makes that easy. The flow of the web application is this: A user first lands on the index (“/”) page which has a simple form with a 2 radio buttons to specify which garage to open. After selecting a garage and hitting a “Open” submit button, a POST is made to “/garage”, which triggers the GPIOs and redirects back to the index page. And thats it! Pretty simple. The last step in any UI design, which is arguably the most important step, is to spice it up a bit with some ASCII art.

And there it is! Here’s a link to the source code on GitHub.

Step 4: Final Installation

Picture of Final Installation

All that is left is to put it all together in some sort of case and mount in my garage. Here are some photos of that process. Like I mentioned before, this was my first hardware project, so I had lots of new experiences and fun building this. I’m pretty proud of this device and have been using it like crazy so far. An interesting scenario I never considered until finishing the project revolved around the boot-up state of GPIO pins. If, for whatever reason, the relays pulsed while the RaspberryPi was booting (prior to my server script running), my garage could potentially open. I tested the theory a few times by pulling the power and plugging it back in. Neither garages opened, so I was relieved. I’m always amazed with the power of Python. I was able to build this entire project (web server, client certificate authentication, CSRF protections, GPIO interactions, and all) in just about 100 lines of code, of which 28 lines were ASCII art. Here is a video of it in action!

Comments

Swansong (author)2017-05-03

That's a neat system, I'd love to do that to my garage! I think you're right, using your phone is more fun than a keypad :)

tman67234 (author)Swansong2017-05-03

Thanks! I think so too :)

About This Instructable

2,035views

35favorites

License:

More by tman67234:Building Garage IoT: a Hacker’s Journey
Add instructable to: