2

Radare2 supports a w which writes a string.

w foobar             write string 'foobar'

However, it doesn't seem to work for me,

$ touch foo

$ radare2 ./foo
w foobar

The file foo remains empty. Am I supposed to flush or save?

5

You should open the file in write mode, using the -w flag.

To quote from radare2's help (r2 -h):

-w open file in write mode

And there is no better way to explain it than by example.

First, let's open foo with radare2 in write mode, using the -w flag. Then, let's print 32 bytes using the px 32 command.

$ r2 -w foo

[0x00000000]> px 32
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  ffff ffff ffff ffff ffff ffff ffff ffff  ................
0x00000010  ffff ffff ffff ffff ffff ffff ffff ffff  ................

As you can see, there is nothing written in the first 32 bytes -- the file is empty. Now, let's write "Hello World!" to the file using the w command.

[0x00000000]> w Hello World!

And print 32 bytes:

[0x00000000]> px 32
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  4865 6c6c 6f20 576f 726c 6421 ffff ffff  Hello World!....
0x00000010  ffff ffff ffff ffff ffff ffff ffff ffff  ................

Great! Now we can see that "Hello World!" is written to the file. Let's quit radare2 and cat the file to see the content:

[0x00000000]> q

$ cat foo
Hello World!

So to sum things up, radare2 opens a file in read-only mode by default. In order to write to the file, you need to pass the -w command line flag to it. This will make sure the file is opened in write mode.

What happens if you are in a read-only session and you want to write to the file? Well, simply execute oo+ which is a command that used to re-open the file in write mode.

Bonus: you can also mimic a writing to the file by using e io.cache=true which enables the cache-mode of radare2. That means that now you can write to the file while it is in radare2 but the changes are not really written to file.

$ r2 foo
[0x00000000]> px 32
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  4865 6c6c 6f20 576f 726c 6421 ffff ffff  Hello World!....
0x00000010  ffff ffff ffff ffff ffff ffff ffff ffff  ................

[0x00000000]> e io.cache=true

[0x00000000]> w Goodbyte World! :)

[0x00000000]> px 32
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  476f 6f64 6279 7465 2057 6f72 6c64 2120  Goodbyte World!
0x00000010  3a29 ffff ffff ffff ffff ffff ffff ffff  :)..............

[0x00000000]> q

$ cat foo
Hello World!
  • What does -w do outside of write mode? – Evan Carroll Nov 13 '18 at 20:50
  • 1
    Well, it allows radare2 to write to the file. i.e modifying the original file. This is risky and should be careful while using it. While working with write-mode you can work regularly with radare2 as you are used to. It doesn't change a thing. Also, please see my edition the the answer -- regardless your comment, I elaborated more :) – Megabeets Nov 13 '18 at 20:54
  • The follow up question is though what does w foobar do if radare opens the file in read-only mode? Is it just a noop? – Evan Carroll Nov 13 '18 at 20:56
  • 1
    yup. Not doing a thing. I suggest you explore the powers of the w command and subcommands of radare2. I use it on a daily basis - check out w? for commands like wtf, wox, wopD... – Megabeets Nov 13 '18 at 20:57
0

Just to update @Megabeets answer.

When you start with an empty file (created by touch), the w command will not work by default even if the r2 will be started in write mode. The missing information is, in that case, the mapping

[0x00000000]> om
[0x00000000]

Returns an empty result. Apart from listing, the om command can also be used to create the mapping:

Usage: om[-] [arg]   # map opened files
| om                                       list all defined IO maps
...
| om fd vaddr [size] [paddr] [rwx] [name]  create new io map
...

In order to do the mapping, one needs to specify (for example) the following command

[0x00000000]> om 3 0x0 12
[0x00000000]> om
 1 fd: 3 +0x00000000 0x00000000 - 0x0000000b rwx

That will create, for the file description 3, a 12 bytes in size mapping starting from an address 0x0.

After that w will work:

[0x00000000]> w Hello world!
[0x00000000]> px 32
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  4865 6c6c 6f20 776f 726c 6421 ffff ffff  Hello world!....
0x00000010  ffff ffff ffff ffff ffff ffff ffff ffff  ................
[0x00000000]>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.