Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure handling of script_decomp.sh #5

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tools/python-uncompyle6"]
path = scripts/python-uncompyle6
url = https://github.com/xforce/python-uncompyle6.git
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ simple_logger = "1.6"
compress = "0.2"
tree_magic = "0.2"
bytesize = "1.0.0"
regex = "1"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Alexander Guettler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ cargo install --path .
Example:

```
npktool x script.npk out
npktool x script.npk
```

This will extract all the files in script.npk to the out `out/script` directory.
This will extract all the files in script.npk to the `out` directory.

You can also supply a list of .npk files, and if those contain a filelist file, the tool
will automatically detect it and put the files in the original file structure:
> In Eve Echoes the filelist file usually resides in res0.npk
```
npktool x res0.npk res1.npk res2.npk res3.npk res4.npk res5.npk res6.npk res7.npk res8.npk res9.npk res10.npk res11.npk res12.npk
```

More info on how to use it can be found in the help section.
`npktool --help`
12 changes: 12 additions & 0 deletions scripts/decompile_pyc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

# NOTE(alexander): DO NOT REORDER THIS!
import sys
import os
dir_path = os.path.dirname(os.path.realpath(__file__))

sys.path.insert(0, os.path.join(dir_path, "python-uncompyle6"))

from uncompyle6.bin.uncompile import main_bin

main_bin()
175 changes: 175 additions & 0 deletions scripts/pyc_decryptor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import zlib
import marshal
import binascii
import argparse
import pymarshal


class PYCEncryptor(object):
def __init__(self):
# LEFT (ORIGNAL) RIGHT (NEOX)
self.opcode_encrypt_map = {
0: 0, # STOP CODE (TODO)
1: 38, # POP TOP
2: 46, # ROT TWO
3: 37, # ROT THREE
4: 66, # DUP TOP
5: 12, # ROT FOUR
# 9: 13, # NOP (TODO)
10: 35, # UNARY POSITIVE
11: 67, # UNARY NEGATIVE
12: 81, # UNARY_NOT
13: 32, # UNARY_CONVERT
15: 9, # UNARY_INVERT
19: 63, # BINARY_POWER
20: 70, # BINARY_MULTIPLY
21: 44, # BINARY_DIVIDE
22: 36, # BINARY_MODULO
23: 39, # BINARY_ADD
24: 57, # BINARY_SUBTRACT
25: 10, # BINARY_SUBSCR
26: 52, # BINARY_FLOOR_DIVIDE
27: 13, # BINARY_TRUE_DIVIDE (TODO)
28: 49, # INPLACE_FLOOR_DIVIDE
# 29: 29, # INPLACE_TRUE_DIVIDE (TODO)
30: 86, # SLICE
31: 87, # SLICE_1
32: 88, # SLICE_2
33: 89, # SLICE_3
40: 24, # STORE_SLICE
41: 25, # STORE_SLICE_1
42: 26, # STORE_SLICE_2
43: 27, # STORE_SLICE_3
50: 14, # DELETE_SLICE
51: 15, # DELETE_SLICE_1
52: 16, # DELETE_SLICE_2
53: 17, # DELETE_SLICE_3
54: 8, # STORE_MAP
55: 21, # INPLACE_ADD
56: 55, # INPLACE_SUBTRACT
57: 82, # INPLACE_MULTIPLY
58: 34, # INPLACE_DIVIDE
59: 22, # INPLACE_MODULO
60: 65, # STORE_SUBSCR
61: 6, # DELETE_SUBSCR
62: 58, # BINARY_LSHIFT
63: 71, # BINARY_RSHIFT
64: 43, # BINARY_AND
65: 30, # BINARY_XOR
66: 19, # BINARY_OR
67: 5, # INPLACE_POWER
68: 60, # GET_ITER
# 70: 75, # PRINT_EXPR (TODO, WIP)
71: 53, # PRINT_ITEM
72: 42, # PRINT_NEWLINE
73: 3, # PRINT_ITEM_TO
74: 48, # PRINT_NEWLINE_TO
75: 84, # INPLACE_LSHIFT
76: 77, # INPLACE_RSHIFT
77: 78, # INPLACE_AND
78: 85, # INPLACE_XOR
79: 47, # INPLACE_OR
80: 51, # BREAK_LOOP
81: 54, # WITH_CLEANUP
82: 50, # LOAD_LOCALS
83: 83, # RETURN_VALUE
84: 74, # IMPORT_STAR
85: 64, # EXEC_STMT
86: 31, # YIELD_VALUE
87: 72, # POP_BLOCK
88: 45, # END_FINALLY
89: 33, # BUILD_CLASS
90: 145, # HAVE_ARGUMENT/ STORE_NAME
91: 159, # DELETE_NAME
92: 125, # UNPACK_SEQUENCE
93: 149, # FOR_ITER
94: 157, # LIST_APPEND
95: 132, # STORE_ATTR
96: 95, # DELETE_ATTR
97: 113, # STORE_GLOBAL
98: 111, # DELETE_GLOBAL
99: 138, # DUP_TOPX
100: 153, # LOAD_CONST
101: 101, # LOAD_NAME
102: 135, # BUILD_TUPLE
103: 90, # BUILD_LIST
104: 99, # BUILD_SET
105: 151, # BUILD_MAP
106: 96, # LOAD_ATTR
107: 114, # COMPARE_OP
108: 134, # IMPORT_NAME
109: 116, # IMPORT_FROM
110: 156, # JUMP_FORWARD
111: 105, # JUMP_IF_FALSE_OR_POP
112: 130, # JUMP_IF_TRUE_OR_POP
113: 137, # JUMP_ABSOLUTE
114: 148, # POP_JUMP_IF_FALSE
115: 172, # POP_JUMP_IF_TRUE
116: 155, # LOAD_GLOBAL
119: 103, # CONTINUE_LOOP
120: 158, # SETUP_LOOP
121: 128, # SETUP_EXCEPT
122: 110, # SETUP_FINALLY
124: 97, # LOAD_FAST
125: 104, # STORE_FAST
126: 118, # DELETE_FAST
130: 93, # RAISE_VARARGS
131: 131, # CALL_FUNCTION
132: 136, # MAKE_FUNCTION
133: 115, # BUILD_SLICE
134: 100, # MAKE_CLOSURE
135: 120, # LOAD_CLOSURE
136: 129, # LOAD_DEREF
137: 102, # STORE_DEREF
140: 140, # CALL_FUNCTION_VAR
141: 141, # CALL_FUNCTION_KW
142: 142, # CALL_FUNCTION_VAR_KW
143: 94, # SETUP_WITH
# SPECIAL NEOX THING, LOAD CONST + LOAD FAST, I think (TODO, GUESS)
173: 173,
146: 109, # SET_ADD
147: 123 # MAP_ADD
}
self.opcode_decrypt_map = {
self.opcode_encrypt_map[key]: key for key in self.opcode_encrypt_map}
self.pyc27_header = "\x03\xf3\x0d\x0a\x00\x00\x00\x00"

def _decrypt_file(self, filename):
os.path.splitext(filename)
content = open(filename).read()
try:
m = pymarshal.loads(content)
except:
try:
m = marshal.loads(content)
except Exception as e:
print("[!] error: %s" % str(e))
return None
# pymarshal.dumps(m, self.opcode_decrypt_map)
return m.co_filename.replace('\\', '/'), pymarshal.dumps(m, self.opcode_decrypt_map)

def decrypt_file(self, input_file, output_file=None):
result = self._decrypt_file(input_file)
if not result:
return
pyc_filename, pyc_content = result
if not output_file:
output_file = os.path.basename(pyc_filename) + '.pyc'
with open(output_file, 'wb') as fd:
fd.write(self.pyc27_header + pyc_content)


def main():
parser = argparse.ArgumentParser(description='onmyoji py decrypt tool')
parser.add_argument("INPUT_NAME", help='input file')
parser.add_argument("OUTPUT_NAME", help='output file')
args = parser.parse_args()
encryptor = PYCEncryptor()
encryptor.decrypt_file(args.INPUT_NAME, args.OUTPUT_NAME)


if __name__ == '__main__':
main()
Loading