ReFirmLabs / binwalk

Firmware Analysis Tool
MIT License
10.85k stars 1.54k forks source link

Fix breakage on Windows caused by use of unavailable pwd module #577

Closed nmantani closed 3 weeks ago

nmantani commented 2 years ago

Binwalk has been broken on Windows since the commit 0d6bf607c979b51b46f3d13c9e56267ba54688bb due to import of pwd module that is not available on Windows. I have added checks whether pwd module is available or not with os.name variable. pwd module is available if os.name is "posix" (on Linux, FreeBSD, and so on). On Windows, os.name is "nt". This pull request fixes the issues #547 and #562.

The following tests used the latest code of the commit fa0c0bd59b8588814756942fe4cb5452e76c1dcd

Before fix:

PS C:\Users\nobut\Desktop> py -3
Python 3.9.8 (tags/v3.9.8:bb3fdcf, Nov  5 2021, 20:48:33) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import binwalk
>>> for module in binwalk.scan("test.png", signature=True, quiet=True, string=False):
...     for result in module.results:
...         print("Offset: 0x%x\t%s" % (result.offset, result.description))
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\nobut\AppData\Local\Programs\Python\Python39\lib\site-packages\binwalk-2.3.3-py3.9.egg\binwalk\__init__.py", line 10, in scan
    objs = m.execute()
  File "C:\Users\nobut\AppData\Local\Programs\Python\Python39\lib\site-packages\binwalk-2.3.3-py3.9.egg\binwalk\core\module.py", line 782, in execute
    for module in self.list():
  File "C:\Users\nobut\AppData\Local\Programs\Python\Python39\lib\site-packages\binwalk-2.3.3-py3.9.egg\binwalk\core\module.py", line 699, in list
    import binwalk.modules
  File "C:\Users\nobut\AppData\Local\Programs\Python\Python39\lib\site-packages\binwalk-2.3.3-py3.9.egg\binwalk\modules\__init__.py", line 16, in <module>
    from binwalk.modules.extractor import Extractor
  File "C:\Users\nobut\AppData\Local\Programs\Python\Python39\lib\site-packages\binwalk-2.3.3-py3.9.egg\binwalk\modules\extractor.py", line 7, in <module>
    import pwd
ModuleNotFoundError: No module named 'pwd'
>>>

After fix:

PS C:\Users\nobut\Desktop> py -3
Python 3.9.8 (tags/v3.9.8:bb3fdcf, Nov  5 2021, 20:48:33) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import binwalk
>>> for module in binwalk.scan("test.png", signature=True, quiet=True, string=False):
...     for result in module.results:
...         print("Offset: 0x%x\t%s" % (result.offset, result.description))
...
Offset: 0x0     PNG image, 471 x 1062, 8-bit/color RGBA, non-interlaced
Offset: 0x5b    Zlib compressed data, compressed
>>>
nmantani commented 2 years ago

Thank you for your review! I have committed as you suggested.

nmantani commented 2 years ago

Hi, when will this pull request be merged?

lioliy commented 2 years ago

Hi, when will this pull request be merged?

I cannot wait just use yours repo. Working fine on windows 11 thx bro

mightymos commented 2 years ago

I'm not sure what difference it makes compared to current fix, but I was able to use this pwd alternative on Windows: https://pypi.org/project/pwdpp/

pwdpp does require fix for install_requires in setup.py to allow installation. Then install pwdpp via setup.py (apparently support for pip install would be preferred...), import pwdpp instead of pwd in binwalk extractor.py, and works.

I have zero python experience, but willing to help. Here's the modified setup.py: https://gist.github.com/mightymos/04856d71e631a82d60e98c69a6938c8f

kazegh commented 2 years ago

I have the same question now : binwalk v2.3.3 on 11 Sep 2021 Windows 11 Python 3.9.10 (tags/v3.9. , Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)] on win32

kingfish22 commented 2 years ago

I'm not sure what difference it makes compared to current fix, but I was able to use this pwd alternative on Windows: https://pypi.org/project/pwdpp/

pwdpp does require fix for install_requires in setup.py to allow installation. Then install pwdpp via setup.py (apparently support for pip install would be preferred...), import pwdpp instead of pwd in binwalk extractor.py, and works.

I have zero python experience, but willing to help. Here's the modified setup.py: https://gist.github.com/mightymos/04856d71e631a82d60e98c69a6938c8f

Hi, I have been working for 2 days now trying to get Binwalk to work in Windows 10, and I keep getting the “no module named pwd” error. I was going to try your fix, but I have no idea how to edit the setup.py to get past the install_requires error. I have only been working with Python for a couple days, and am very unfamiliar with how to edit or use raw code from GitHub. Any help you can provide on updating or creating a new setup.py file for pwdpp would be great. Thank you

mightymos commented 2 years ago

I'm not sure what difference it makes compared to current fix, but I was able to use this pwd alternative on Windows: https://pypi.org/project/pwdpp/ pwdpp does require fix for install_requires in setup.py to allow installation. Then install pwdpp via setup.py (apparently support for pip install would be preferred...), import pwdpp instead of pwd in binwalk extractor.py, and works. I have zero python experience, but willing to help. Here's the modified setup.py: https://gist.github.com/mightymos/04856d71e631a82d60e98c69a6938c8f

Hi, I have been working for 2 days now trying to get Binwalk to work in Windows 10, and I keep getting the “no module named pwd” error. I was going to try your fix, but I have no idea how to edit the setup.py to get past the install_requires error. I have only been working with Python for a couple days, and am very unfamiliar with how to edit or use raw code from GitHub. Any help you can provide on updating or creating a new setup.py file for pwdpp would be great. Thank you

These instructions will get binwalk to run which gives some information about image signatures. However extraction is not working because pwdpp does not provide everything that pwd did. I guess this is the difference between users/permission between linux and windows. Anyway, so you will need a utility to manually extract archives (e.g., 7-zip).

I'm using MSYS2 with Python 3.10.7 installed. There is a learning curve to using MSYS2 also...

In MSYS2 terminal install pactoys so full architecture (x86, x86_64, etc.) is not required to be specified at each use of package manager for installing packages: pacman -S pactoys

Then: pacboy -S python python-psutils python-binwalk

Choose Download files from: https://pypi.org/project/pwdpp/

The file as of 10/1/2022 is named pwdpp-1.0.6.tar.gz

Then extract pwdpp archive and replace setup.py with modified file I posted on gist. (Click Raw on github and save that file.) (NOTE: I have no idea how to submit pull request on PyPi for this module...instead of posting random gists...)

Then, install pwdpp with: python setup.py install

(NOTE: you can get path to extractor.py by running binwalk and it should fail at this point) Then modify extractor.py installed from binwalk in your file systems to replace:

import pwd

with: import pwdpp

Finally running binwalk from MSYS2 terminal should work. However again extractor options will not work.

I tested binwalk on this firmware for an ethernet/wifi camera: https://amcrest-firmwares.s3.amazonaws.com/Curent+firmware/38+seires/IP8M-VT2879EW-AI/Amcrest_IPC-HX3XXX-Dalton_Eng_NP_Stream3_AMCREST_V2.800.00AC001.0.R.220715.bin

I think you also want to install: pacboy -S binutils

For the [strings] utility.

Some examples here for analyzing firmwares: https://embeddedbits.org/reverse-engineering-router-firmware-with-binwalk/

FroggMaster commented 7 months ago

Another one that would be great to be merged. This affects us Windows users, binwalk is unable to be used after install otherwise. Looks like there hasn't been a release in over a year though, perhaps the developers have abandoned the tool?

nmantani commented 7 months ago

Hi, there is a fork (https://github.com/OSPG/binwalk) of this repository that is actively maintained. This pull request have already been merged at there (https://github.com/OSPG/binwalk/commit/93463c678da8d5db892cc5432d67c1454728975e).