Wednesday, November 13, 2019

Unbranding ZYXEL VMG3625-T20A (Telia/Sonera) Router

This operation can possibly brick your router. So do it only if you know what you are doing. Your router is your own responsibility. There are many so called YouTube videos etc. which have unnecessary and dangerous steps. So use your brain to decide what you need and what you do not. I will stick to bare minimum and as safe as possible steps for this specific router model.

Sometimes ISPs brand the routers and disable features, maybe mess with their settings and perhaps spy on you.

Maybe the router is locked into a specific provider and you need to use it with another. There are many reasons why you may want to install generic ZYXEL firmware to an xDSL router.

Luckily it is rather easy to unbrnad the ZYXEL routers. I have performed this operation on a ZYXEL VMG3625-T20A router which was provided free with (also free) VDSL service. Albeit, free service is limited to 10Mbit/s speed but that is a good enough speed for most people.

What Do We Need?

We need few items for this operation.
  • USB TTL Serial adapter
  • Putty or any terminal program which supports serial communication
  • Tftp client (Windows 10 comes with one!) 
 

Opening The Router Case

This router has only 2 screws under the sticker. Some other routers may have 4 screws. You can feel the placements of he screw holes with your finger if you move on the sticker. You can lift the corners of the sticker with a sharp knife and unscrew the screws. I left the screws under the sticker, so they would not go far.

Once the screws are removed, you can use a plastic prying tool to open the clips. The parts are quite tight, so use patience.

I did not make pictures of this as the operation is vey simple. If you can't manage to open a simple plastic cover, you should stop now.

Here is how the router looks like inside. For our purposes, the most important piece is th 4pin connector.






The 4pin connector...


Connecting Rouer To Serial Adapter

We need to connect the router to serial adapter for communication. As you can see, the 4pin connector pins are marked as GND, NC, RX, TX, and 3.3V. For our purposes, we only need to connect RX, TX, and GND pins.

Important: Now you need to set your TTL adapter to use 3.3v. They usually have a jumper or switch for this purpose.

Modem Side - Serial Adapter Side
RX         - TX
TX         - RX
GND        - GND 

The only thing you need to be careful is to connect TX to RX. There is little which can go wrong.

Open your serial communications program and set it to work with 115200 8N1 setting. If everything is corect, you should see boot messages printed to screen when you turn on the router.

Connecting Router To Ethernet

You will also need to connect the router to ethernet as we will use TFTP to push files to the router. You should set your PCs IP address statically to 192.168.1.10 (or anyting else than 192.168.1.1 in 192.168.1.0/24 network).

Getting Ready To Send Firmware

When you turn on the router, it will give you few seconds to abort the boot process. Press a key to abort it!

You should now be at a prompt which says ZHAL> Now we can start some magic.

First run ATSH command to extract some important information. We will not need this information, but it is good to have in case something goes wrong.

The output will look like following
Firmware Version : V5.30(ABIE.6)b2_E0 Bootbase Version : V1.32 | 03/22/2018 16:56:20 Vendor Name : Zyxel Communications Corp. Product Model : VMG3625-T20A Serial Number : SXXXXXXXXXXXX First MAC Address : XXXXXXXXXXX0 Last MAC Address : XXXXXXXXXXXF MAC Address Quantity : 16 Default Country Code : FF Boot Module Debug Flag : 01 RootFS Checksum : e4d28a4b Kernel Checksum : eeba2049 Main Feature Bits : 00 Other Feature Bits : 840291b4: 04050305 00000100 00000000 00000000 840291c4: 00000000 00000000 00000000 0000
Now we need to get the login code. The login code is required for dangerous operaitons. For this purpose we should feed the Product Model from above output usin ATSE command. We will need to decode this. Luckily some nice people provided a C program which can do it. You can compile it with GCC easily. Obviously this program can be used for other ZYXEL routers! Just see the example in comments section of the program to figure out how to proceed!

/* ZyXEL prestige 660HW series password calculator by brainstorm 
  * Thanks to http://www.adslayuda.com/Zyxel650-9.html authors
  *
  * Example usage:
  *
  * Router:
  * ======
  *
  * ATSE
  * 0028D6DF1C03
  * OK
  *
  * Computer:
  * ========
  *
  * ./zyxel 0028D6DF1C03
  * ATEN 1,221E3111
  *
  * Router:
  * ======
  * ATEN 1,221E3111
  * OK
  *
  * "Dangerous" commands enabled :-)
  *
  * */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define magic1  0x10F0A563L
#define magic2  7
#define atse_length 12  /* ATSE command, ZyNOS seed password length */

#define WORD_LENGTH (8*sizeof(value))
int ror(unsigned int value, int places)
{
  return (value>>places)|(value<<(WORD_LENGTH-places));
}


int main (int argc, char* argv[]) {

        char *seed, a[7], c[3];
        unsigned int b,d,e,password;

        if ( argc != 2 ) {
                printf("Only one argument is permitted: 00BDC8667E5B\n");
                exit(-1);

        } else if ( strlen(argv[1]) != atse_length ) {
                printf( "Incorrect parameter length, should be %d characters long\n", atse_length );
                exit (-2);
        }

        seed = argv[1];

        strncpy (a, seed , 6);  //a="ersten" 3Bytes vom seed
        e = strtol(a,NULL,16);  //e=a

        strncpy (c, seed + strlen(seed)-2, 2); //c= last 2 bytes of seed?
        d = strtol(c,NULL,16) & magic2; //d="last byte" AND 7
        b = e + magic1; //

        b = ror(b,d);
        password = b ^ e;
        printf("\nATEN 1,%X\n", password);

        return 0;
}


After entering ATEN command we can finally upload the firmware. If you downloaded the firmware files from ZYXEL site, you will see that there is a BIN file which is about 17Mbyte in size. I renamed this file as zyxel.bin just for easy typing.

Now we need to tell the router that we are about to send it firmware image named, you guessed it, zyxel.bin. The command for tis operation is ZHAL> ATUR zyxel.bin The router will wait you to send the file

Upgrade to rootfs partition 1
TFTP server is started, put your file 'zyxel.bin' to server (IP is 192.168.1.1).

Now from windows we can send the file in binary mode. We need ot use the -i flag for binary transfer with windows tftp client. So the command should look like tftp -i 192.168.1.1 PUT zyxel.bin. We already set the IP address of our machine statically so it should work just fine. So the complete output should look like below:

ZHAL> ATSE VMG3625-T20A
0EA420ED3340
ZHAL> ATEN 1,10F1EDA3
ZHAL> ATUR zyxel.bin
Upgrade to rootfs partition 1
TFTP server is started, put your file 'zyxel.bin' to server (IP is 192.168.1.1).
.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Total 17498534 (0x10B01A6) bytes received

File download to memory address 0x80020000, length is 17498534

Please be patient, start to upgrade RAS!

...........................................................................................................................................................................................................................................................................................................................................................................................................................................

Update boot flag to 1
...
Auto reboot after 2 seconds
ZHAL>


Router will reboot by itself and you need to wait patiently as this will take few minutes at least. Meanwhile, not forget to remove the static IP setting from your ethernet adapter or you may regret not doing it later.

Next Step

Obviously the next step is closing up the router and tightenin the screws. I first installed 5.30(ABIE.4)C0 firmware from ZYXEL site then used the built-in web based upgrade to upgrade to 5.30(ABIE.5)C0 firmware. Also normal VDSL2 connectivity was working fine, including the 6RD tunnel that Telia/Sonera provides. So everything seems to be working fine. I am not sure why Telia/Sonera decided to make their own image, it is slightly suspicious.

Using same method, you can install any firmware to device (as long as it is compatible). For example if you want to brand it as another provider's device. I don't know why you would want to do this, but it is an option.

Caveats

The only problem is that I do not know how to backup existing firmware from the device. Therefore if you want to return back to old firmware, you will need to have access to it as BIN file. If you know how you can backup firmware from a ZYXEL device using serial communications, drop a line.

Saturday, November 9, 2019

Ubuntu 19.10 and Namespaces vs Netplan

I am using OpenVPN and namespaces to isolate access to the VPN connection. Originally, I wrote small scripts to bring up the connection and setup the necessary network interfaces.

I used a bridged ethernet inside the namespace as gateway to outside. For this purpose, normally I use brctl to edit bridge configurations. But now that Ubuntu is using netplan, things have changed a little bit.

Some namespace related operations are unclear and not documented at all.

Setting Up Namespace

First action is to edit /etc/systemd/network/25-veth.netdev and define 2 virtual ethernets.

[NetDev]
Name=veth0
Kind=veth
[Peer]
Name=veth1

Next action is to edit the /etc/netplan/50-cloud-init.yaml and assign one of the ethernets into bridge with main ethernet.

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    version: 2
    ethernets:
        enp5s0:
            addresses: []
            dhcp4: false
        veth0: {}
        veth1: {}

    bridges:
      br0:
        dhcp4: yes
        interfaces:
          - enp5s0
          - veth0


So after this I still had to configure few setting using a script. If you know how to convert this to use netplan, please write to comments.
  ip netns add vpn
  ip netns exec vpn ip link set dev lo up
  ip link set veth1 netns vpn
  ip netns exec vpn ifconfig veth1 inet 192.168.1.252 netmask 255.255.255.0 up
  ip netns exec vpn route add default gw 192.168.1.1 dev veth1
  ip netns exec vpn route add -net 10.8.0.0 netmask 255.255.255.0 gw 192.168.1.1 dev veth1

You probably do not need the last line. I needed to add it so that packets for 10.8.0.0/24 network would not go out through VPN.

Name Resolution

One other problem is that Ubuntu 19.10 seem to be using systemd-resolved and therefore /etc/resolv.conf looks like this:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
search lan

Now the fun part is that it is extremely difficult to route localhost addresses from namespace. So I had to use external resolv.conf and hosts files. These files are put to /etc/netns/vpn/ (here "vpn" is my namespace).

resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4


hosts
127.0.0.1       localhost.localdomain   localhost ubuntu
::1             localhost6.localdomain6 localhost6 ubuntu

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Now the reason for using some outside DNS IP addresses is so that the DNS traffic is routed over VPN once it is established. I was not super paranoid about this. But somebody might want to use up/down scripts with openvpn to edit the nameservers.

I had to setup hosts file because sudo command was giving dns resolving errors for the hostname ubuntu

Ubuntu 19.10 & Kodi - How to remove shutdown/restart etc. from exit menu

I recently upgraded my HTPC to Ubuntu 19.10 and a bunch of stuff was broken. (typical).

I had to figure out how to remove the shutdown/restart etc. options from the exit menu of Kodi. I also had a problem that the power button on remote caused machine to shutdown and power off!

Disabling Shutdown, Restart etc. was accomplished by help of PolicyKit. We simply have to define policy file which disables these actions by users. (this obviously won't effect these operations being executed from shell)

Into /etc/polkit-1/localauthority/50-local.d/disable-shutdown.pkla file, added:

[Disable shutdown etc.]
Identity=unix-user:*
Action=org.freedesktop.login1.reboot;org.freedesktop.login1.reboot-multiple-sessions;org.freedesktop.login1.power-off;org.freedesktop.login1.power-off-multiple-sessions;org.freedesktop.login1.suspend;org.freedesktop.login1.suspend-multiple-sessions;org.freedesktop.login1.hibernate;org.freedesktop.login1.hibernate-multiple-sessions
ResultAny=no
ResultInactive=no
ResultActive=no


You can restart it by issuing sudo systemctl restart polkit

Now, disabling the power button on remote is accomplished by editing /etc/systemd/logind.conf and adding the following line HandlePowerKey=ignore. Then you should restart systemd-logind by issuing sudo systemctl restart systemd-logind

Hopefully this would have resolved the issue for you also. Good luck!