Lassi
Lassi

Reputation: 13

How to edit a binary at specific address locations using Nim?

I'm trying to understand how to modify a Windows executable using Nim to write NOPs in specific locations of the file. In Python I do this as follows:

    with open('file.exe', 'r+b') as f:
        f.seek(0x0014bc0f)
        f.write(binascii.unhexlify('9090'))
        f.seek(0x0014bc18)
        f.write(binascii.unhexlify('9090'))

But in Nim I can't find the necessary methods to accomplish the seeking to a specific address and how to input these op-codes properly. I'm pretty new to Nim and the different file operation possibilities are a little confusing.

Upvotes: 1

Views: 239

Answers (1)

shirleyquirk
shirleyquirk

Reputation: 1598

the docs for io are quite good, but to start you off:

  • opening a file for reading and writing (equivalent of 'r+') is:
let f = open("file.exe",fmReadWriteExisting)
  • the equivalent of with here, to make sure to close the file on any error:
defer: f.close
  • seeking to an offset (by default bytes relative to the start):
f.setFilePos(0x014bc0f)
  • writing two bytes at that position (nop nop)
f.write("\x90\x90")

check out the documentation for each proc for more options

Upvotes: 1

Related Questions