Pages: 1 [2] 3
Author Topic: IDA Pro helper functions  (Read 42659 times)
fluke9
Full Member
***

Karma: +26/-1
Offline Offline

Posts: 113


« Reply #15 on: October 28, 2019, 10:42:53 AM »

Hello there guys,

Im not so good with IDA and python and im a bit puzzled of how exactly i can run the scripts.

For example for the first one how can i define min and max ? Does IDA support any interactive way where you can type the variables ?

Kind regards.

copy script to a file in your disassembly directory as for example script.py

then type: execfile("script.py") in the IDA python console
afterwards type the functionname and arguments in the console, for example:
processrom(0x80000, 0x80FFFF)

« Last Edit: October 29, 2019, 02:46:28 AM by fluke9 » Logged
Chipburn
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


« Reply #16 on: October 28, 2019, 12:53:22 PM »

Thanks for the replies guys Smiley

I will try those. I guess i can load them as snippets as well for faster access.
Logged
Chipburn
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


« Reply #17 on: October 28, 2019, 03:57:54 PM »

Yes, of course you can. But the whole point of the tools is to avoid having to do it via UI because GUIs suck for handling large amounts of data that is best handled by automation.

Thanks Nyet, i was doing the address naming manually etc. What i was asking about "interactive" if you can use a command in the python script that actually popup a window in IDA to define e.g. min max or load a "file"

For now i followed fluke9 guide and works marvelous.

Cheesy
Logged
sonflasch
Full Member
***

Karma: +12/-2
Offline Offline

Posts: 66


« Reply #18 on: October 22, 2020, 12:01:01 PM »

sorry for threadjacking, dumping my hack also here:

Rudimentary damos parer which will output a python script which can be executed in the ida console,
this thing will label variables, also it will create enums for bitfields and apply them to the correct locations.

Also names the variables with repeatable comments,
your disassembly will then look something like this:
Code:
843214 loc_843214:                             ; CODE XREF: sub_842C26+46E↑j
843214                                         ; sub_842C26+47A↑j
843214                 jnb     word_FD7C.B_sa, loc_8432DC
843218                 mov     r4, ATM_bits
84321C                 and     r4, #100h
843220                 jmpa    cc_NZ, loc_8432DC
843224                 jnb     word_FD20.B_atmtpk, loc_843270
843228                 jnb     word_FD7C.B_sa, loc_843270
84322C                 extp    #206h, #1
843230                 movbz   r4, TATMSAE     ; exotherme im Schub
843234                 sub     r4, #0Ah
843238                 movbz   r5, tikatm      ; Abgastemperatur im Katalysator aus Modell
84323C                 cmp     r4, #0


script here, which will generate namebytes.py :

Code:
#!/usr/bin/python

import sys

if len(sys.argv) != 2:
    print 'please specify filename'
    exit

f = open(sys.argv[1], "r")
lines = f.readlines()
f.close()

#print 'loaded ' + sys.argv[1] + ' with ' + str(len(lines)) + ' lines'
print '# -*- coding: latin-1 -*-'

for line in lines:

    data = line.split(',')
    if len(data) > 1:
        data[1] = data[1].strip()

        if data[1].startswith('/SPZ'):
            data = line.split(',')
            varname = data[2].strip()
            varcomment = (line[line.find("{")+1 : line.find("}")] )

            temp = line[line.find("}")+1 : -1]
            aftercomment = temp[temp.find("}")+1 : -1]
            dataac = aftercomment.split(',')
            varoffset = int(dataac[2].strip()[1:], 16)

            print("set_name(" + hex(varoffset) +", \"" + varname + "\");")
            if len(varcomment):
                print("set_cmt(" + hex(varoffset) +", \"" + varcomment.replace('"', '\\"') + "\", 1);")


        if data[1].startswith('/SRC'):
            data = line.split(',')
            varname = data[2].strip()
            varcomment = (line[line.find("{")+1 : line.find("}")] )

            temp = line[line.find("}")+1 : -1]
            aftercomment = temp[temp.find("}")+1 : -1]
            dataac = aftercomment.split(',')
            varoffset = int(dataac[1].strip()[1:], 16)

            print("set_name(" + hex(varoffset) +", \"" + varname + "\");")
            if len(varcomment):
                print("set_cmt(" + hex(varoffset) +", \"" + varcomment.replace('"', '\\"') + "\", 1);")


    if line.startswith('/UMP'):
#        /UMP, {}, afnmn, {Bereichsfenster Aussetzer, minimale Drehzahl}, $3830B5, 513, 160, nmot_ub_q40, 3, $FF, K;

        data = line.split(',')

        temp = line[line.find("}")+1 : -1]
        varcomment = (temp[temp.find("{")+1 : temp.find("}")] )

        temp = line[line.find("}")+1 : -1]
        aftercomment = temp[temp.find("}")+1 : -1]
        dataac = aftercomment.split(',')

        varname = data[2].strip()
        varoffset = int(dataac[1].strip()[1:], 16)
        varmask = dataac[6].strip()[1:]

        if varmask == 'FF' or varmask == 'FFFF':
            #print(varname + " | " + hex(varoffset) + " | " + varcomment + " | " + varmask)
            print("set_name(" + hex(varoffset) +", \"" + varname + "\");")
            if len(varcomment):
                print("set_cmt(" + hex(varoffset) +", \"" + varcomment.replace('"', '\\"') + "\", 1);")
            #print(varmask)
        else:
            enumname = "enum_" + str(hex(varoffset))[2:]
            maskstr = hex(int(varmask, 16))
            print("add_enum(-1, \"" + enumname + "\", 0)")
            print("set_enum_bf(get_enum(\"" + enumname + "\"), 1)")
            print("add_enum_member(get_enum(\"" + enumname + "\"), \"" + varname + "\"," + maskstr + ", " + maskstr + ")")


run with:
parsedamos.py file.dam > namebytes.py

After generating the namebytes.py copy to your ida project dir and execute in the ida console with:
execfile("namebytes.py")



Hello

I have a little question.
everything works so far but the bit fields are not displayed to me.
Enum screen all ok but in the IDA-View i don't see a name

your example
843224                 jnb     word_FD20.B_atmtpk, loc_843270
843228                 jnb     word_FD7C.B_sa, loc_843270

me look jpg.

Logged
locon
Newbie
*

Karma: +3/-0
Offline Offline

Posts: 5



« Reply #19 on: October 22, 2020, 09:25:55 PM »

Move to address 0xFD70 and press 'M' to assign an Enum entry. Select enum_FD70.
Logged
sonflasch
Full Member
***

Karma: +12/-2
Offline Offline

Posts: 66


« Reply #20 on: October 22, 2020, 10:17:53 PM »

Move to address 0xFD70 and press 'M' to assign an Enum entry. Select enum_FD70.

Ok thanks:-)
yes but shouldn't the script do it itself?
Logged
prj
Hero Member
*****

Karma: +915/-426
Online Online

Posts: 5835


« Reply #21 on: October 23, 2020, 07:53:02 AM »

Maybe you think the script should give blowjobs too?  Grin
Improve it to do that if you want it to do it, and post here the result.
Logged

PM's will not be answered, so don't even try.
Log your car properly.
noice
Newbie
*

Karma: +16/-3
Offline Offline

Posts: 23


« Reply #22 on: October 23, 2020, 11:03:33 AM »

Ok thanks:-)
yes but shouldn't the script do it itself?

op_enum(0xFD70, 0, get_enum("enum_fd70"), 0)
Logged
browny23
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 10


« Reply #23 on: October 25, 2021, 07:27:29 AM »

thread revival
hey guys im trying to load a .a2l using the script and i keep getting a line 1 nonetype error
Logged
flamy
Full Member
***

Karma: +6/-0
Offline Offline

Posts: 56


« Reply #24 on: November 03, 2022, 08:19:25 AM »

Here a short script to copy or move bytes from one address to another.

Usage:
- Load script via Alt + F7
- Execute script by function call "moveCode(eas, eae, eat, erase)" in Python console

Function description:
- eas: Start address of code
- eae: End address of code
- eat: Target address where to put code
- erase: 0 = Keep source address space as it is, 1 = Overwrite source address space with "0xFF"

Hint:
- Make sure that target address space can be overwritten. Function does not check, whether there is already code existing, or not!
Logged
fragolas
Jr. Member
**

Karma: +16/-0
Offline Offline

Posts: 39


« Reply #25 on: November 07, 2022, 04:46:22 PM »

small script based on prj dam loader, but written to work with ida 7.5
Code:
fp = open(r"C:\Users\XXXXXXXXX\MY.dam", errors="ignore")
lines = fp.read().split("\n")
lines.pop(0)
print("Found: %d lines" % len(lines))
for line in lines:
    if (len(line) > 0):
        l = line.split(",")
        if (len(l) > 4):
            if (l[1].strip() == "/SRC"):
                addr = l[-5].replace("$", "0x")
                idc.set_name(int(addr, 0), l[2].strip(), 1)
            elif (l[0].strip() == "/UMP"):
                addr = l[-7].replace("$", "0x")
                idc.set_name(int(addr, 0), l[2].strip(), 1)
Logged
flamy
Full Member
***

Karma: +6/-0
Offline Offline

Posts: 56


« Reply #26 on: November 08, 2022, 02:55:35 PM »

Maybe useful for those of you using Keil µVision, I wrote a script to import *.H86 HEX-Files to IDApro.

Usage:
- Go to "Hex View" or "IDA View"
- Click on the address where to put the content of your *.H86 file
- Load script via Alt + F7
- Confirm target address
- Select *.H86-file to import
Logged
Blazius
Hero Member
*****

Karma: +89/-40
Offline Offline

Posts: 1277



« Reply #27 on: November 08, 2022, 04:17:04 PM »

Maybe useful for those of you using Keil µVision, I wrote a script to import *.H86 HEX-Files to IDApro.

Usage:
- Go to "Hex View" or "IDA View"
- Click on the address where to put the content of your *.H86 file
- Load script via Alt + F7
- Confirm target address
- Select *.H86-file to import

This might be good. Cheers.
Logged
fragolas
Jr. Member
**

Karma: +16/-0
Offline Offline

Posts: 39


« Reply #28 on: April 05, 2023, 04:46:28 AM »

i dont know where to put this and i didnt want to start a new thread only because of this so here it goes.

Most of the time the a2l/damos is in german, and when you import it in winols it cumbersome to manualy translate it, so i wrote a simple python script that translates a csv defenition file (from winols export) based on another csv(this could be from a similiar ecu, etc)

it needs some work but for the most part it works. Hope its usefull for someone.

Code:
import csv

de = open(r"C:\Users\me\Desktop\f30.csv")
en = open(r"C:\Users\me\Desktop\f10.csv")
out = open(r"C:\Users\me\Desktop\outputmap.csv","w", newline="")


with de as file_de, en as file_en, out as outfile:
    fieldnames = ['Name', 'IdName', 'AxisX.Name', 'AxisY.Name'] #, 'FolderName'
    writer = csv.DictWriter(outfile, fieldnames=fieldnames, delimiter=";")
    writer.writeheader()
    german = list(csv.DictReader(file_de, delimiter=';'))
    english = list(csv.DictReader(file_en, delimiter=';'))
    cont1=0
    print(len(english))
    for row_de in german:
        cont = 0
        count2 = 0
        for row_en in english:
            if row_de['IdName'].lower() != row_en['IdName'].lower():
                cont = cont + 1
                if cont == len(english): #if after running throught english file no match found print german
                   writer.writerow({'Name': row_de['Name'], 'IdName' : row_de['IdName'], 'AxisX.Name' : row_de['AxisX.Name'], 'AxisY.Name' : row_de['AxisY.Name']}) # 'FolderName' : row_de['FolderName'],
                    #print(row_de['Name'] + ';' + row_de['IdName'] + ";" + row_de["FolderName"] + ";" + row_de["AxisX.Name"] + ";" + row_de["AxisY.Name"])
            else:
                count2 = count2+1
                if count2 == 1: # in case there is more than one "idname" with the same name, this makes sure it only prints once per row of german
                   writer.writerow({'Name': row_en['Name'], 'IdName': row_de['IdName'], 'AxisX.Name': row_en['AxisX.Name'], 'AxisY.Name': row_en['AxisY.Name']}) # 'FolderName': row_de['FolderName'],
                    #print(row_en['Name'] + ';' + row_de['IdName'] + ";" + row_en["FolderName"] + ";" + row_en["AxisX.Name"] + ";" + row_en["AxisY.Name"])

        cont1 = cont1 + 1
        print("percentage = %.2f" % (cont1 / len(german) * 100), "%")
    print("done")



also for the folder name( this one i have to work a bit more)

Code:
import csv

de = open(r"C:\Users\me\Documents\Carros\ECU\mini\WinOLS rcz english.csv")
en = open(r"C:\Users\me\Documents\Carros\ECU\mini\WinOLS psa.csv")
out = open(r"C:\Users\me\Documents\Carros\ECU\mini\folder_translator.csv","w", newline="")


with de as file_de, en as file_en, out as outfile:
    fieldnames = ['FolderName']
    writer = csv.DictWriter(outfile, fieldnames=fieldnames, delimiter=";")
    writer.writeheader()
    german = list(csv.DictReader(file_de, delimiter=';'))
    english = list(csv.DictReader(file_en, delimiter=';'))
    cont1=0
    for row_de in german:
        cont = 0
        count2 = 0
        row_de1 = row_de['FolderName'].lower().split()
        row_de2 = row_de1[0]
        for row_en in english:
            row_en1=row_en['FolderName'].lower().split()
            row_en2=row_en1[0]
            if row_de2 != row_en2:
                cont = cont + 1
                if cont == len(english): #if after running throught english file no match found print german
                    #print(row_de['FolderName'])
                    writer.writerow({'FolderName': row_de['FolderName']})
            else:
                count2 = count2+1
                if count2 == 1: # in case there is more than one "idname" with the same name, this makes sure it only prints once per row of german
                    #print(row_en['FolderName'])
                    writer.writerow({'FolderName': row_en['FolderName']})
        cont1 = cont1 + 1
        print("percentage = %.2f" % (cont1 / len(german) * 100), "%")
    print("done")

Logged
fknbrkn
Hero Member
*****

Karma: +177/-18
Offline Offline

Posts: 1401


mk4 1.8T AUM


« Reply #29 on: February 15, 2024, 04:22:55 PM »

del
« Last Edit: February 15, 2024, 04:25:02 PM by fknbrkn » Logged
Pages: 1 [2] 3
  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Page created in 0.057 seconds with 17 queries. (Pretty URLs adds 0s, 0q)