---
# System prepended metadata

title: 'R6400 [Firmadyne]'
tags: [firmadyne]

---

---
title: R6400 [Firmadyne]
tags: firmadyne
lang: zh_tw
---

# R6400 [Firmadyne]
[TOC]

# TL;DR
- 在 Kernel 開機參數加上 init=/sbin/preinit
- 環境都在 Ubuntu 14.04 操作
- R6400 Router 部分 Services 成功模擬起來
- httpd 則是 Page Fault 失敗

# Environment
| - | - |
| -------- | -------- |
| 運行 Qemu 之 OS     | Ubuntu 14.04     |
| Compile Kernel 之 OS | Ubuntu 14.04 |
| Kernel | [firmadyne/v4.1](https://github.com/firmadyne/kernel-v4.1) |
| Qemu Version | 2.0.0 |

# Setting
修改 scratch/${IID}/run.sh:
```shell==
#!/bin/bash

set -u

ARCHEND=armel
IID=1

if [ -e ./firmadyne.config ]; then
    source ./firmadyne.config
elif [ -e ../firmadyne.config ]; then
    source ../firmadyne.config
elif [ -e ../../firmadyne.config ]; then
    source ../../firmadyne.config
else
    echo "Error: Could not find 'firmadyne.config'!"
    exit 1
fi

IMAGE=`get_fs ${IID}`
KERNEL=`get_kernel ${ARCHEND}`
QEMU=`get_qemu ${ARCHEND}`
QEMU_MACHINE=`get_qemu_machine ${ARCHEND}`
QEMU_ROOTFS=`get_qemu_disk ${ARCHEND}`
WORK_DIR=`get_scratch ${IID}`

TAPDEV_0=tap${IID}_0
HOSTNETDEV_0=${TAPDEV_0}
echo "Creating TAP device ${TAPDEV_0}..."
sudo tunctl -t ${TAPDEV_0} -u ${USER}

echo "Bringing up TAP device..."
sudo ip link set ${HOSTNETDEV_0} up
sudo ip addr add 192.168.1.2/24 dev ${HOSTNETDEV_0}

echo "Adding route to 192.168.1.1..."
sudo ip route add 192.168.1.1 via 192.168.1.1 dev ${HOSTNETDEV_0}

function cleanup {
    pkill -P $$

    echo "Deleting route..."
    sudo ip route flush dev ${HOSTNETDEV_0}
     
    echo "Bringing down TAP device..."
    sudo ip link set ${TAPDEV_0} down
     
    echo "Deleting TAP device ${TAPDEV_0}..."
    sudo tunctl -d ${TAPDEV_0}
}

trap cleanup EXIT

echo "Starting firmware emulation... use Ctrl-a + x to exit"
sleep 1s

KERNEL="${BINARY_DIR}/zImage.v4.1.17.custom2.armel"

# FIRMADYNE_BOOT_VAR="rdinit=/firmadyne/preInit.sh rw debug ignore_loglevel print-fatal-signals=1 user_debug=31 firmadyne.syscall=0"
FIRMADYNE_BOOT_VAR="init=/sbin/preinit rdinit=/firmadyne/preInit.sh rw debug ignore_loglevel print-fatal-signals=1 user_debug=31 firmadyne.syscall=0"
# FIRMADYNE_BOOT_VAR=""
CUSTOM_BOOT_VAR=""
# CUSTOM_BOOT_VAR=""

CUSTOM_FLAG=""
# CUSTOM_FLAG="-s -S"

# NETWORK_FLAG="-device virtio-net-device,netdev=net1 -netdev socket,id=net1,listen=:2001 \
#               -device virtio-net-device,netdev=net2 -netdev socket,id=net2,listen=:2002 \
#               -device virtio-net-device,netdev=net3 -netdev socket,id=net3,listen=:2003"
NETWORK_FLAG="-device virtio-net-device,netdev=net1 -netdev tap,id=net1,ifname=${TAPDEV_0},script=no \
              -device virtio-net-device,netdev=net2 -netdev socket,id=net2,listen=:2002 \
              -device virtio-net-device,netdev=net3 -netdev socket,id=net3,listen=:2003"

QEMU_AUDIO_DRV=none ${QEMU} \
    ${CUSTOM_FLAG} \
    -m 512 -M ${QEMU_MACHINE} -kernel ${KERNEL} \
    -drive if=none,file=${IMAGE},format=raw,id=rootfs -device virtio-blk-device,drive=rootfs \
    -append "root=${QEMU_ROOTFS} console=ttyS0 nandsim.parts=64,64,64,64,64,64,64,64,64,64 ${FIRMADYNE_BOOT_VAR} ${CUSTOM_BOOT_VAR}" \
    -nographic \
    ${NETWORK_FLAG} \
    | tee ${WORK_DIR}/qemu.final.serial.log
```
- line 56 `zImage.v4.1.17.custom2.armel` 為自行編譯的 Kernel
    - 可以用 firmadyne 原本提供的 zImage.armel 就好
    - 這自行編譯的 Kernel 只是將某些 printk 移除，讓輸出簡化
- 在 line 59 對 Kernel 參數加上 init=/sbin/preinit
- line 76 記憶體從 256 改為 512 後有比較順
- 原本沒有偵測到網路介面，所以原版 [run.sh](#) 中是使用 line 67~69 的網路設定
    - 自己將網卡加上，改為 line 70~72
    - 再次模擬後發現 R6400 有預設 IP 192.168.1.1
    - 回頭修正 line 26~36 41~48 關於網卡的設定

# Result
可以執行到開 shell
![](https://i.imgur.com/4dNYDSF.jpg)
![](https://i.imgur.com/9h5hZBj.jpg)
![](https://i.imgur.com/vRoyxGS.jpg)
![](https://i.imgur.com/R7HKERS.jpg)
![](https://i.imgur.com/Lu0wWUO.jpg)

發現有 TCP port 53 548 631 5000
從外部掃 port
![](https://i.imgur.com/Ko1rHBn.jpg)
的確掃的到，表示目前至少能對這些 port 上的 binary 做 fuzzing

# Problems
1. 輸入輸出時常會卡住，目前猜測是執行速度問題
    加大記憶體後有改善此情況   
2. httpd 失敗
    httpd 在 ps 中有列出，但 port 沒有看到 80 有被綁定，這邊決定重開 httpd
    - 先殺掉舊 httpd process
    - 再下一樣指令執行 httpd
    - 但會發生 unhandled page fault
        ![](https://i.imgur.com/NfTBL4S.jpg)
        ![](https://i.imgur.com/iiAHRed.jpg)
        ![](https://i.imgur.com/g4mcBjt.jpg)
        ![](https://i.imgur.com/hXIJyY4.jpg)

    