radareorg / radare2-r2pipe

Access radare2 via pipe from any programming language!
389 stars 99 forks source link

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

Open sdt27 opened 2 years ago

sdt27 commented 2 years ago

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
themaks commented 2 years ago

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 ?

abay12676 commented 2 years ago

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

filipe-cogo commented 2 years ago

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.

radare commented 2 years ago

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

stiv-kupchik commented 2 years ago

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:]
radare commented 2 years ago

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.

trufae commented 2 years ago

Fixed in master. thanks for submitting the fix!

jacob-pro commented 2 years ago

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

trufae commented 2 years ago

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.

jacob-pro commented 2 years ago

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

trufae commented 2 years ago

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

jacob-pro commented 2 years ago

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("")
trufae commented 2 years ago

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.

dobin commented 1 year ago

Still an issue for me, sadly:

image

image

image

radare commented 1 year ago

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 commented 1 year ago

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

radare commented 1 year ago

Looks like a racist condition 🤔

spotted-deer commented 1 year ago

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.

radare commented 1 year ago

Fixed in r2pipe-1.8.0 please confirm :)