Reverse Enginering Bafang m500 and m600 Firmwares and BESST

ornias

100 W
Joined
Jul 18, 2021
Messages
172
Bafang locked down the firmware for the m500 and m600, or did they?
I'll drop in some notes after spending a day looking at the current Firmware files and the current version of BESST.
Mostly I was interested in the possibility of increasing the MAX Current for a m500.

BESST

Or rather: WORST security ever.
One could write a complete alternative backend for BEST in about a week or two, Spoofing the login is easily and the whole UI is just an Electron App, so you can just read the Javascript sourcecode yourself. However:
- BESST does not contain the UI elements required to modify firmware
- The settings that can be altered with the right login, are limited. There is no UI or backend element to change the current limit or voltage for example. However: Reading the code I cannot exclude that it is, in fact, possible to use the canbus to alter these settings.
- It looks like the BESST tool (the physical thing) is mostly just a generic CANBUS/UART usb adapter without any special magic to decode firmwares or things like that.
- The BESST code does seem to include some references where certain values can be found on the canbus for it's GET, the users working at reverse enginering the CANBUS should be able to use the Javascript UI to reverse engineer the correct bussaddresses for those values and start trying to SET those.

Firmware
Looking at the firmware is more interesting however. Most of our first guesses where that the firmware was being encrypted. So I started off with an entrophy scanning tool. Luckily the entrophy did not indicate an encrypted firmware file.

Next was a carefull look with binwalk:
- The firmware files do not contain a boot volume or something like that, just one big code blob
- The firmware seems to be deflate compressed
- The firmware seems to be writhen for an ARM based microcontroller
- The Microcontroller seems to be using THUMB instructions

Next was a first look with Ghidra:
- I asummed a ARM, little endian cortex program, as that is the most likely what the controller is running.
- I have no actual bafang m500 or m600 laying around, so if anyone is friendly enough tell us the precise controller: Please do!
- Decompiling the code, I noticed that the Reset related code (that should link to the main function) is 0x0 So this does not seem to be a complete program for this platform, as some portions are missing
- I number of instructions did not decompile, this is most likely due to THUMB and ARM instructions being mixed.
- However, it did find some structures that look like code which does warrant another look.

I'm no ghidra expert in any way, shape or form, but i'm pretty certain that this firmware is reverse engineerable.

*Disclaimer*
The above is for educational purposes only and, under my jurisdiction, I cannot be held liable for discussing reverse engineering.
 
I'm clueless regarding this kind of thing, but THANKS for your time/trouble to jot down and publish theses notes! Hopefully at some point they'll lead to something CAN BUS owners can use easily.
 
Slight update:
Today I focussed on different versions of BESST and the communication protocol between the BESST software and BESST hardware tool.

BESST software:
- Some older versions of BESST (such as the one spread by Luna Cycles some time ago), still include unobfusticated versions of the BESST javascript code
- This means it's easier to read without reverse engineering manually and also includes comments for different functions.
- After some more reading of the annotated and more readable versions of the BEEST software, i'm sure there is no firmware validation in the BESST software.

BESST software <-> Hardware protocol:
- The chip that handles communication is indeed a standard Silicon Labs CP210x based chip.
- The communication is between the PC and hardware tool is actually done with UART
- The BESST hardware tool translates those UART commands to CANBUS commands
- For the commands previously reverse engineered from the CANBUS side, the UI code seems to include the required UART commands to trigger the BESST hardware tool to send the required CAN commands (https://github.com/stancecoke/BionX_Minimal_Translator/blob/Sniffer_for_Bafang/Inc/CAN-Registers.h)

Currently I see three plausible attack vectors:
- If more UART set commands are accepted by the BESST hardware tool (for currently uneditable settings) we could find out how we can trigger them. After that's done we can snoop the CANBUS traffic to reverse engineer the canbus commands
- If more CANBUS set commands are accepted by the controller but no other UART commands are accepted by the BESST hardware tool, we would need to brute force (guess) the CANBUS options that are and are not available.
- As previously discussed it should be possible to reverse firmware files.

It would be super nice of folks that already done work on these motors (such as the folks at Luna Cycles), could share some insight as well.
 
ornias said:
could share some insight as well.
You found my resources already. I have to remark, that my findings are from a BAFANG display on an Ananda controller. Sadly, I can't do further investigations, as I sold the display meanwhile.

Perhaps we should collect all known CAN commands in a wiki. Things spread over several threads are very inconvenient :)

regards
stancecoke
 
stancecoke said:
ornias said:
could share some insight as well.
You found my resources already. I have to remark, that my findings are from a BAFANG display on an Ananda controller. Sadly, I can't do further investigations, as I sold the display meanwhile.

Perhaps we should collect all known CAN commands in a wiki. Things spread over several threads are very inconvenient :)

regards
stancecoke

Yeah, awesome work!
Just started a consolidated resource with @casainho , mind reformating your work a bit and adding it to the repo? :)

https://github.com/OpenSourceEBike/Bafang_M500_M600
 
ornias said:
Just started a consolidated resource with @casainho

Hm, I had a short look at the CAN sniffings of @CiDi, the IDs seem to be completly different from my findings with the Ananda controller. :?

https://github.com/OpenSourceEBike/Bafang_M500_M600/tree/main/CANBUS/DIY%20CANBUS%20by%20CiDi

So maybe Ananda uses a different protocol, although it is a known Bafang display...

regards
stancecoke

index.php
 
fechter said:
I don't have a M600, but I do have a BBSHD. We have the firmware .bin files for those and good chance it's similar. BBSHD uses a ST microcontroller:
http://www.stcmicro.com/STC15W4K56S4.html

Super cool, good to know!
Currently working with Casainho and some other guys in the official thread to consolidate all the info we (and other forums) currently have :)
 
Back
Top