Can the source code be converted after reading the bin file? - Page 1
Forum section: electronics / testgear

author: daisizhou
author_rank: Super Contributor
timestamp: May 17, 2021, 08:47:17 am
content: I read the bin file in the flash memory through the programmer.I want to know the source code, is there a way to achieve it?The mcu model is MC9S12XDP512,Please tell me the tools and steps used, thank you

author: coromonadalix
author_rank: Super Contributor
timestamp: May 17, 2021, 08:51:45 am
content: Normally once converted to an hex it can not reverted or easily decompiled, tools like IDA Ghidra cant decompile completely an HEXIt would be too easy to do reverse engineering loll

author: tv84
author_rank: Super Contributor
timestamp: May 17, 2021, 09:25:20 am
content: Quote from: daisizhou on May 17, 2021, 08:47:17 amThat is a .bin dump, not an .hex file.

author: tunk
author_rank: Super Contributor
timestamp: May 17, 2021, 10:09:43 am
content: You already have a thread about this:https://www.eevblog.com/forum/repair/help-required-how-to-convert-bin-file-to-source-code/

author: tv84
author_rank: Super Contributor
timestamp: May 17, 2021, 10:22:52 am
content: And the OP has been there...

author: radiolistener
author_rank: Super Contributor
timestamp: May 17, 2021, 10:55:44 am
content: Quote from: daisizhou on May 17, 2021, 08:47:17 amNo, compilation from source code to the binary is one way operation. All information needed for human readability will be lost.Recreate source code from binary is reverse engineering. This is very complex work which needs to analyze what machine code doing and then write your own source code which doing the same things. In most cases it's more easy and cheaper to write your own source code from scratch than reverse engineering for existing binary.Some tools like decompilers are exists, but they just help to translate machine code into more readable format for analysis. They are not intended to restore functional source code.

author: daisizhou
author_rank: Super Contributor
timestamp: May 17, 2021, 11:09:09 am
content: Quote from: tunk on May 17, 2021, 10:09:43 amYes i have been looking for a solution

author: Bicurico
author_rank: Super Contributor
timestamp: May 17, 2021, 11:29:07 am
content: Some notes:1) Reverse Engineering of software can be illegal, depending on the purpose:https://en.wikipedia.org/wiki/Reverse_engineering#Legality2) Authors of software are not happy when people try to reverse engineer their work, which leads to the next important question:3) What do you want to achieve? Why do you want to reverse engineer a binary file?While it is not an easy task, it is not impossible, to do so.As already mentioned in both threads that you started, you can use a DECOMPILER. In your case, you have two options: IDA Pro (expensive) or Ghidra (open source).If correctly configured and used, they allow to convert a binary (in one of the supported CPU languages) into human readable source code (assembly language) and/or pseudo C code.This code cannot be edited in a regular SDK and then compiled into a binary again, as it will miss information and be incomplete. To do so, would probably take more time than simply writing a similar program from scratch.However, it does allow to understand some principles and is certainly used for another "illegal" activity: generation of patched and keygens. I do imagine that there are some legal applications, but only few come to my mind."Learning" how to program is certainly NOT an application for reverse engineering! If you want to learn how to program, you should take a course in exactly that subject.Regards,Vitor

author: Bicurico
author_rank: Super Contributor
timestamp: May 17, 2021, 12:33:49 pm
content: Of course you can decompile a HEX file. It is just a representation of the binary file in a text readable format, where each byte is represented as a hexadecimal number.You only need to consider the memory layout represented in the HEX file, which might not be contiguous, but rather list different memory locations

author: daisizhou
author_rank: Super Contributor
timestamp: May 17, 2021, 12:43:33 pm
content: I hope to add some functions that were originally restricted,At the same time some interest in decompilationMy assumption is that if the same environment can be configured, the code and machine language should be interchangeable--Of course this is the ideal state

author: Mechatrommer
author_rank: Super Contributor
timestamp: May 17, 2021, 01:28:57 pm
content: Quote from: tv84 on May 17, 2021, 10:22:52 amno he hasnt... nobody has.. afaik.. but with current age of AI this and AI that, instead of apps that make people's face like shit, efforts should be spent on this kind of thing, if some hackers come to their senses.

author: nctnico
author_rank: Super Contributor
timestamp: May 17, 2021, 02:16:24 pm
content: Quote from: mstatelottery on May 17, 2021, 11:35:45 amActually some decompilers can do a reasonable job for decompiling binary code. In the end compilers follow a certain strategy to convert code into assembly; based on that a readable source can be created which allows to study the flow of the program.

author: tv84
author_rank: Super Contributor
timestamp: May 17, 2021, 03:06:53 pm
content: Quote from: Mechatrommer on May 17, 2021, 01:28:57 pmThe OP has been in the thread advised by @tunk, that what I was referring to.Quote from: nctnico on May 17, 2021, 02:16:24 pmThey sure do. IDA and Ghidra do AWESOME jobs for the processors they can already pseudo-decompile.BTW, one doesn't need a readable source to study the flow. A flow graphic (that they both provide) is already a great help in studying the flow.In more general terms:A big problem is that there are very few schools where reversing is even touched.Erroneously, people then to relate reversing with hacking and keep away from it. BUT they loose all the benefits in debugging skills that reversing provides.And this forum has lots of people that do it in the HW world but much less in the SW world.

author: ucanel
author_rank: Regular Contributor
timestamp: May 17, 2021, 03:21:22 pm
content: I recently ran into a video about the topic:https://youtu.be/q4CxE5P6RUEThat would not be a route that i will take in same situation,writing the code from scratch would be easier.If you will change just some little things like messages on screen etc. it is doable with a little bit effort.

author: tv84
author_rank: Super Contributor
timestamp: May 17, 2021, 03:55:23 pm
content: Quote from: ucanel on May 17, 2021, 03:21:22 pmA nice video to give OP a quick overview about what is at play.Although this uses ARM proc language which Ghidra can decompile.