Skip to content

Commit

Permalink
Add --one-dir mode
Browse files Browse the repository at this point in the history
  • Loading branch information
extremecoders-re authored May 10, 2023
1 parent a77ecd7 commit 04c3d59
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@ Note: Python 3.11 executables are not fully supported yet as the underlying libr
Precompiled binaries for Linux and Windows are provided in [releases](https://github.com/pyinstxtractor/pyinstxtractor-ng/releases).
These are generated using PyInstaller itself, so you don't even need a Python installation to run pyinstxtractor-ng

```
PyInstaller Extractor NG
positional arguments:
filename Path to the file to extract
optional arguments:
-h, --help show this help message and exit
-d, --one-dir One directory mode, extracts the pyz to the same directory
```

Pass the exe filename as an argument or drag & drop the pyinstaller exe file over pyinstxtractor.ng icon on Windows.
```
$ ./pyinstxtractor-ng <filename>
X:\> pyinstxtractor-ng <filename>
```

The `--one-dir` mode extracts the pyz in the same directory as the executable. This is useful if you want to run the extracted files straight-away.

```
X:\> pyinstxtractor-ng --one-dir main.exe
X:\> cd main.exe_extracted
X:\main.exe_extracted\> python main.py
```

## See Also

- [pyinstxtractor-web](https://pyinstxtractor-web.netlify.app/): pyinstxtractor running in the web browser, powered by Go & GopherJS.
Expand Down
66 changes: 38 additions & 28 deletions pyinstxtractor-ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import sys
import zlib
import struct
import argparse

from uuid import uuid4 as uniquename

Expand Down Expand Up @@ -229,7 +230,7 @@ def _writeRawData(self, filepath, data):
with open(nm, "wb") as f:
f.write(data)

def extractFiles(self):
def extractFiles(self, one_dir):
print("[+] Beginning extraction...please standby")
extractionDir = os.path.join(
os.getcwd(), os.path.basename(self.filePath) + "_extracted"
Expand Down Expand Up @@ -312,7 +313,7 @@ def extractFiles(self):
self._writeRawData(entry.name, data)

if entry.typeCmprsData == b"z" or entry.typeCmprsData == b"Z":
self._extractPyz(entry.name)
self._extractPyz(entry.name, one_dir)

# Fix bare pyc's if any
self._fixBarePycs()
Expand Down Expand Up @@ -379,11 +380,14 @@ def _tryDecrypt(self, ct):
cipher = AES.new(key, AES.MODE_CFB, iv)
return cipher.decrypt(ct[CRYPT_BLOCK_SIZE:])

def _extractPyz(self, name):
dirName = name + "_extracted"
# Create a directory for the contents of the pyz
if not os.path.exists(dirName):
os.mkdir(dirName)
def _extractPyz(self, name, one_dir):
if one_dir == True:
dirName = "."
else:
dirName = name + "_extracted"
# Create a directory for the contents of the pyz
if not os.path.exists(dirName):
os.mkdir(dirName)

with open(name, "rb") as f:
pyzMagic = f.read(4)
Expand Down Expand Up @@ -466,29 +470,35 @@ def _extractPyz(self, name):


def main():
if len(sys.argv) < 2:
print("[+] Usage: pyinstxtractor.py <filename>")

else:
arch = PyInstArchive(sys.argv[1])
if arch.open():
if arch.checkFile():
if arch.getCArchiveInfo():
arch.parseTOC()
arch.extractFiles()
arch.close()
print(
"[+] Successfully extracted pyinstaller archive: {0}".format(
sys.argv[1]
)
)
print("")
print(
"You can now use a python decompiler on the pyc files within the extracted directory"
parser = argparse.ArgumentParser(description="PyInstaller Extractor NG")
parser.add_argument("filename", help="Path to the file to extract")
parser.add_argument(
"-d",
"--one-dir",
help="One directory mode, extracts the pyz in the same directory as the executable",
action="store_true",
)
args = parser.parse_args()

arch = PyInstArchive(args.filename)
if arch.open():
if arch.checkFile():
if arch.getCArchiveInfo():
arch.parseTOC()
arch.extractFiles(args.one_dir)
arch.close()
print(
"[+] Successfully extracted pyinstaller archive: {0}".format(
args.filename
)
return
)
print("")
print(
"You can now use a python decompiler on the pyc files within the extracted directory"
)
return

arch.close()
arch.close()


if __name__ == "__main__":
Expand Down

0 comments on commit 04c3d59

Please sign in to comment.