radare2-r2pipe icon indicating copy to clipboard operation
radare2-r2pipe copied to clipboard

The outputs of 'cmd()' is empty on Windows 10

Open sdt27 opened this issue 3 years ago • 19 comments

Environment

2022/01/14
5.5.4  r2
Windows 10 Enterprise
1.6.5 r2pipe

Description

There seems to be a bug here, the cmd command does not output the results correctly. This looks like an issue with newlines.

Test

examples>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import r2pipe
>>> r=r2pipe.open('hello.exe')
>>> print(r.cmd('uname'))
>>>#### the result is empty
>>> print(r.cmd('i'))
Windows 10 Enterprise
>>>#### the result is previous

sdt27 avatar Jan 14 '22 07:01 sdt27

image Here is a horrible work around :)

There seems to be an extra null-byte in the pipe at the start of the r2 process ?

themaks avatar Jan 18 '22 10:01 themaks

That's not a good work around, things are failing randomly due to these null bytes long after the pipe is opened.

abay12676 avatar Jan 25 '22 02:01 abay12676

Apparently changing zro = foo.find(b"\x00") to zro = foo.rfind(b"\x00") on _cmd_process(self, cmd) at open_sync.py solves the issue.

filipe-cogo avatar Jan 28 '22 22:01 filipe-cogo

The first message after spawning r2 its a null byte. The rest of messages do not have this. So an rfind should break other things. I cant repro on linux/mac.

On Fri, 28 Jan 2022 at 23:14, Filipe R. Côgo @.***> wrote:

Apparently changing zro = foo.find(b"\x00") to zro = foo.rfind(b"\x00") on _cmd_process(self, cmd) at open_sync.py solves the issue.

— Reply to this email directly, view it on GitHub https://github.com/radareorg/radare2-r2pipe/issues/146#issuecomment-1024692356, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG75FRHOQJWVRFJK5RBK3DUYMIMJANCNFSM5L6CC6NA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- --pancake

radare avatar Feb 06 '22 15:02 radare

I had the same issue, and it seems that it's not only the first message after spawn that has a null byte - every command return started with \x00. I added a check after foo.read for this and it seemed to solve the issue

if os.name == "nt":
    if len(foo) > 1 and foo.startswith(b"\x00"):
        foo = foo[1:]

stiv-kupchik avatar Apr 25 '22 16:04 stiv-kupchik

Can you send a pullrequest instead?

On 25 Apr 2022, at 18:11, stiv-kupchik @.***> wrote:

 I had the same issue, and it seems that it's not only the first message after spawn that has a null byte - every command return started with \x00. I added a check after foo.read for this and it seemed to solve the issue

if os.name == "nt": if len(foo) > 1 and foo.startswith(b"\x00"): foo = foo[1:] — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

radare avatar Apr 25 '22 17:04 radare

Fixed in master. thanks for submitting the fix!

trufae avatar Apr 26 '22 08:04 trufae

I'm afraid this still doesn't work for me on Windows 11 Using r2 5.5.4

And I installed the latest from git:

pip install -e 'git+https://github.com/radareorg/radare2-r2pipe.git#egg=r2pipe&subdirectory=python'

With the above patch, but I still face the same problem, about 90% of the time the first cmd is empty, and the results don't come through unless I call r.cmd('') afterwards

jacob-pro avatar Jun 01 '22 15:06 jacob-pro

r2 5.5.4 is pretty old, like 1 year old. there was a release of the r2pipe module so no need to install it from git. i'll look into that if that's not the case.

trufae avatar Jun 02 '22 16:06 trufae

Last release of r2pipe on PyPi is 1.6.5 from November 2021, and doesn't have the code from #147 ?

jacob-pro avatar Jun 02 '22 16:06 jacob-pro

omg i forgot to publish 1.6.6 🤦 i just pushed 1.7.0 with some more changes. can you try again? thanks for the headsup

trufae avatar Jun 03 '22 11:06 trufae

Thanks @trufae but it still doesn't work, what changes are you referring to? Like I said I already tried commit https://github.com/radareorg/radare2-r2pipe/commit/e3b54c17e9812cbbdb3e84a4e96adf3c74b6b772 by downloading directly from git, but it still doesn't work unless I call r.cmd() twice.

I'm currently just doing this in my code to work around it:

def double_call(r, cmd):
    first = r.cmd(cmd)
    return first if len(first) > 0 else r.cmd("")

jacob-pro avatar Jun 03 '22 12:06 jacob-pro

i had some time (finally) to test and fix it. Please try again with r2pipe 1.7.1 :) it seems to work well on windows linux and mac now. i need to improve the testsuite.

i have also realized that if you have outdated plugins, the "please run r2pm .." message is breaking r2pipe too.

trufae avatar Jun 05 '22 08:06 trufae

Still an issue for me, sadly:

  • python 3.11.2
  • r2pipe 1.7.4
  • radare2 5.8.2-w64

image

image

image

dobin avatar Mar 02 '23 10:03 dobin

I dont have windows 10, but on windows11 it works for me if i spawn python from inside r2. Like this:

cmd> radare2 -
[0x00000000]> #!pipe python
Python 3.10 ..
>>> import r2pipe
>>> r2 = r2pipe.open()
>>> r2.cmd("uname")
"Windows 10 Pro\n"

But it fails if i spawn r2 from the r2pipe instance.. so that means that i can repro, and it should be easy to fix. (hold my beer)

tempImagegDy1Xr

radare avatar Mar 02 '23 15:03 radare

uhm .wtf why this thing works from a script but not from prompt? IMAGE 2023-03-02 17:53:40

radare avatar Mar 02 '23 16:03 radare

Looks like a racist condition 🤔

radare avatar Mar 02 '23 18:03 radare

Still an issue for me, sadly:

  • python 3.11.2
  • r2pipe 1.7.4
  • radare2 5.8.2-w64

image

image

image

I also meet this problem on Windows10 and Windows11. It work well in shell. But in r2pipe, I cannot get result from first time use r2pipe.cmd(xx), only second time use r2pipe.cmd(''), I can get really result.

spotted-deer avatar Mar 09 '23 13:03 spotted-deer

Fixed in r2pipe-1.8.0 please confirm :)

radare avatar Mar 10 '23 10:03 radare