Skip to content

Instantly share code, notes, and snippets.

@rikka0w0
Last active October 21, 2019 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikka0w0/32c321d9f9bb5a54536f0b5da25b2a17 to your computer and use it in GitHub Desktop.
Save rikka0w0/32c321d9f9bb5a54536f0b5da25b2a17 to your computer and use it in GitHub Desktop.
Tweak Netgear D6200

Update dynamic DNS records on Google Domain every hour

Script

#!/bin/sh

### Google Domains provides an API to update a DNS "Syntheitc record". This script
### updates a record with the script-runner's public IP, as resolved using a DNS
### lookup.
###
### Google Dynamic DNS: https://support.google.com/domains/answer/6147083
### Synthetic Records: https://support.google.com/domains/answer/6069273

USERNAME=""
PASSWORD=""
HOSTNAME=""

while true
do

# Resolve current public IP
IP=`curl -s http://ifconfig.co`
URL="https://${USERNAME}:${PASSWORD}@domains.google.com/nic/update?hostname=${HOSTNAME}&myip=${IP}"
curl -s $URL --insecure

echo ""
date
echo ""
sleep 3600
done

Using the mips_linux_gcc toolchain to build iperf2 statically:

  1. Tweak GCC First start the making process of the toolchain, then:
cd ~/uclibc-crosstools-gcc-4.4.2-1-full/buildroot-4.4.2-1/output/toolchain/gcc-4.4.2/gcc/
nano unwind-dw2-fde-glibc.c
# Search for dl_iterate_phdr, comment out the if () and return NULL;
  1. Build iperf2
git clone https://github.com/svn2github/iperf2
cd iperf2/branches/2.0-STABLE
./configure --host=mips-linux CFLAGS=-static CXXFLAGS=-static --enable-static --disable-shared
make

Binary will be in iperf2/branches/2.0-STABLE/src

References:

  1. esnet/iperf#544
  2. https://blog.csdn.net/michaelcao1980/article/details/52788916
  3. https://lists.uclibc.org/pipermail/uclibc/2005-October/012844.html

Contents

  1. Log into the shell
  2. Build the cross compiling toolchain
  3. Compile shadowsocks-libev statically
  4. Compile iperf2 statically

Enable debugging

Enable telnet (only accessible from LAN) on Netgear Routers:

http://10.0.0.1/setup.cgi?todo=debug

Mount the rootfs as writable:

mount -o remount, rw /

# Remount as a read-only rootfs
mount -r -o remount /

Open WAN port (3636 TCP)

iptables -A INPUT -p tcp -s 0/0 -d 0/0 --dport 3636 -j ACCEPT
iptables -t nat -I PRE_CNAPT 1 -p tcp -s 0/0 -d 0/0 --dport 3636 -j ACCEPT

Setup dropbear SSH server

# Assume dropbear binaries are in PATH
mkdir -p /root/.ssh

# Generate keys
dropbearkey -t rsa -f /root/.ssh/dropbear_rsa_host_key
dropbearkey -t dss -f /root/.ssh/dropbear_dss_host_key

# Create server starting script
echo "dropbear -d /root/.ssh/dropbear_dss_host_key -r /root/.ssh/dropbear_rsa_host_key -p ????" > /root/.ssh/start_dropbear
chmod +x /root/.ssh/start_dropbear

# Set password for root
passwd

# Make root password permenant
mv /usr/etc/passwd /usr/etc/passwd.bak
cp /etc/passwd /usr/etc/

Use the router in network switch mode

The WAN port is left unused!

  1. Connect a computer to LAN port (Using DHCP), do not connect WAN yet
  2. Change the router IP to 192.168.0.2 (to be used on 192.168.0.x)
  3. Disable DHCP server on the router
  4. Save settings and reboot the router
  5. Plug in WAN cable to LAN port on the router
  6. Telnet into the router and run:
# Delete blackhole rules
ip rule del prio 32763
ip rule del prio 32764
ip rule del prio 32765

# To allow programs on the router to access the internet:
# Set a default gateway for the network bridge (LAN ports)
route add default gw 192.168.0.1 group1
# Set DNS name server:
echo "nameserver 8.8.8.8" > /etc/resolv.conf

References:

  1. http://www.evolware.org/?p=114
  1. Select "openssh" and openssl in "make menuconfig" Package Selection for the target -> Networking ---> ->
  2. Edit buildroot-4.4.2-1/package/openssh/openssh.mk OPENSSH_SITE=https://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/
  3. Make
  4. Edit /etc/sshd_config :
PermitRootLogin yes
UsePrivilegeSeparation no
ClientAliveInterval 60
  1. Generate keys (On host PC):
ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
ssh-keygen -t rsa -f ssh_host_rsa_key -N ""
  1. Copy to device:
# Files to copy:
/lib/libcrypto.so.0.9.8
/usr/sbin/sshd
/usr/lib/openssh/sftp-server

/etc/ssh_host_dsa_key
/etc/ssh_host_rsa_key
/etc/ssh_host_dsa_key.pub
/etc/ssh_host_rsa_key.pub
/etc/sshd_config

# Links
ln -s /usr/lib/openssh/sftp-server /usr/lib/sftp-server

# Adjust permission
chmod 700 /etc/ssh_host_rsa_key
chmod 700 /etc/ssh_host_dsa_key

# Make etc changes permenant
cp /etc/ssh* /usr/etc/

  1. Start (Must be full path) /usr/sbin/sshd -D &
wget https://github.com/arsv/perl-cross/releases/download/1.3/perl-cross-1.3.tar.gz
wget https://github.com/Perl/perl5/archive/v5.30.0.tar.gz
tar -xzf perl-cross-1.3.tar.gz
tar -xzf  v5.30.0.tar.gz
cp -rfv perl-cross-1.3/* perl5-5.30.0
cd perl5-5.30.0

# Rikka's patch for no __builtin_bswap16
# Implement my own!
echo "unsigned short __builtin_bswap16(unsigned short a){return (a<<8)|(a>>8);}" > rikka.c
mips-linux-gcc -c rikka.c

./configure --prefix=/usr --target=mips-linux-uclibc --target-tools-prefix=mips-linux-
make
# Wait until error occurs
mips-linux-gcc -Wl,-E -o perl perlmain.o libperl.a rikka.o  -lm -lcrypt -ldl
# make the rest
make

file ./perl

Not too sure why

# ./perl -del
Can't locate perl5db.pl in @INC (@INC contains: /usr/lib/perl5/site_perl/5.30.0/mips-linux /usr/lib/perl5/site_perl/5.30.0 /usr/lib/perl5/5.30.0/mips-linux /usr/lib/perl5/5.30.0 .).
BEGIN failed--compilation aborted.

Openssl

 ./Configure linux-mips32 shared  -DB_ENDIAN --prefix=/output --openssldir=/root/output
 make CC=mips-linux-gcc
 
 ./configure --host=mips-linux CFLAGS=-static CXXFLAGS=-static --enable-static --disable-shared --prefix=/root/output --disable-examples --enable-openssl

References: https://arsv.github.io/perl-cross/download.html

#mbedtls
ver=2.4.0
wget --no-check-certificate https://tls.mbed.org/download/mbedtls-$ver-gpl.tgz
tar zxf mbedtls-$ver-gpl.tgz
pushd mbedtls-$ver
sed -i "s/DESTDIR=\/usr\/local/DESTDIR=\/root\/dists\/mbedtls/g" Makefile
CC=mips-linux-gcc AR=mips-linux-ar LD=mips-linux-ld LDFLAGS=-static make install
popd

#pcre
ver=8.40
wget https://ftp.pcre.org/pub/pcre/pcre-${ver}.tar.gz
tar zxf pcre-$ver.tar.gz
pushd pcre-$ver
./configure --host=mips-linux --prefix=/root/dists/pcre --disable-shared --enable-utf8 --enable-unicode-properties
make
make install
popd

#libsodium
ver=1.0.11
wget --no-check-certificate https://download.libsodium.org/libsodium/releases/old/unsupported/libsodium-$ver.tar.gz
tar zxf libsodium-$ver.tar.gz
pushd libsodium-$ver
./configure --host=mips-linux --prefix=/root/dists/libsodium --disable-ssp --disable-shared
make
make install
popd

# libev
ver=4.24
wget http://dist.schmorp.de/libev/Attic/libev-$ver.tar.gz
tar zxf libev-$ver.tar.gz
pushd libev-$ver
./configure --host=mips-linux --prefix=/root/dists/libev --disable-shared
make
make install
popd

#libudns
git clone https://github.com/shadowsocks/libudns
pushd libudns
./autogen.sh
./configure --host=mips-linux --prefix=/root/dists/libudns
make && make install
popd

#shadowsocks
ver=3.0.2
git clone https://github.com/shadowsocks/shadowsocks-libev
pushd shadowsocks-libev
git checkout v$ver -b v$ver
git submodule init && git submodule update
./autogen.sh
LIBS="-lpthread -lm" LDFLAGS="-Wl,-static -static -static-libgcc -L/root/dists/libudns/lib -L/root/dists/libev/lib" CFLAGS="-I/root/dists/libudns/include -I/root/dists/libev/include" ./configure --host=mips-linux --prefix=/root/dists/shadowsocks-libev --disable-ssp --disable-documentation --with-mbedtls=/root/dists/mbedtls --with-pcre=/root/dists/pcre --with-sodium=/root/dists/libsodium
make && make install
popd

OpenWrt:

opkg update
opkg install shadowsocks-libev-ss-server

References:

  1. https://haoutil.com/topic/cross-compile-shadowsocks-libev
  2. https://github.com/necan/shadowsocks-libev-static-build/blob/master/%E7%BC%96%E8%AF%91%E8%84%9A%E6%9C%AC/linux/build_shadowsocks.sh

Build MIPS GCC for D6200

Netgear did not provide the toolchain for D6200, however D6200 shares the same CPU with DGN2200v3, so we use the compiler for DGN2200v3 instead.

  1. Install Docker on host system
  2. Start docker Command: qaq Dockerfile:
FROM i686/ubuntu:14.04

RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y nano screen wget unzip bzip2

RUN apt install -y make gcc binutils patch build-essential
RUN apt install -y flex bison gawk libncurses5-dev gettext

# Downgrade texinfo https://askubuntu.com/questions/491184/texinfo-downgrade
RUN wget http://ftp.gnu.org/gnu/texinfo/texinfo-4.13.tar.gz && \
gzip -dc < texinfo-4.13.tar.gz | tar -xf - && \
cd texinfo-4.13 && \
./configure && \
make && make install

RUN apt install -y git autoconf libtool
  1. Download the toolchain from Netgear
  2. Compile
mkdir –p /opt/toolchains
unzip xxxxxx.zip
tar -xjf xxxx.tar.bz2
cd uclibc-crosstools-gcc-4.4.2-1-full
tar -xjf xxxx.tar.bz2
cd buildroot-4.4.2-1
make menuconfig
# save the .config
make
  1. Add MIPS GCC to path
PATH=$PATH:/opt/toolchains/uclibc-crosstools-gcc-4.4.2-1/usr/bin/

file <executable> command should return: ELF 32-bit MSB executable, MIPS, MIPS32 version 1 (SYSV), statically linked, not stripped

References:

  1. http://www.evolware.org/?p=114
  2. https://kb.netgear.com/2649/NETGEAR-Open-Source-Code-for-Programmers-GPL
  3. https://community.netgear.com/t5/DSL-Modems-Routers/Compiling-the-toolchain-for-the-DGND3700v2/td-p/1073898
  4. https://www.linux-mips.org/wiki/Toolchains
  5. https://dev.archive.openwrt.org/ticket/13039
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment