Coder Social home page Coder Social logo

botlabsdev / npkpy Goto Github PK

View Code? Open in Web Editor NEW
21.0 21.0 9.0 174 KB

npkPy is an unpacker tool for MikroTiks custom NPK container format

Home Page: https://pypi.org/project/npkPy

License: GNU General Public License v3.0

Shell 0.81% Python 99.19%
mikrotik npk unpacker

npkpy's People

Contributors

botlabsdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

npkpy's Issues

Better Exception generation

The tools internal functions always tend to throw a RuntimeError independent of the internal problem. This only allows very basic exception handling from the outside.
At least something like an NPKError or similar would be helpful to not simply catch all errors the same.

(Also there is some commented code ๐Ÿ˜‰)

TypeError: 'str' object is not callable

Hello,

Npks from latest stable release are unpackable with npkpy

npkpy --files system-6.48.4.npk  --show-container
Traceback (most recent call last):
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/bin/npkpy", line 8, in <module>
    sys.exit(main())
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/main.py", line 47, in main
    analyse_npk(opts, files)
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/analyse_npk.py", line 13, in analyse_npk
    for file in npk_files:
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/main.py", line 46, in <genexpr>
    files = (Npk(f) for f in (opts.files if opts.files else get_all_nkp_files(opts.src_folder, opts.glob)))
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 35, in __init__
    self.pck_header = self.pck_cnt_list[0]
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 74, in pck_cnt_list
    self.__cnt_list = self.__parse_all_cnt()
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 81, in __parse_all_cnt
    lst.append(self.__get_cnt(offset))
  File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 94, in __get_cnt
    return CNT_HANDLER[cnt_id](data, offset)
TypeError: 'str' object is not callable
(tmp)  โœ˜ ๎‚ฐ afx@proton ๎‚ฐ /mnt/iso ๎‚ฐ 

Am I doing something wrong?

Thank you!

Unpacking zlib compressed part

The zlib compressed part is another compressed archive of files.

The following code is briefly copied from mikrotik-npk and adapted to python3, showing the archive parsing and dumping.

Missing zlib decompression (done using the zlib-flate tool when testing the code), and proper integration into the npkpy code base. Too rusty in Python object model to immediately figure out how this should be done proper.

import os
import argparse
import struct
from pathlib import Path

def parse_data(data):
        res = []
        while len(data)>30:
                header = data[:30]
                dsize = struct.unpack("I", header[24:28])[0]
                fsize = struct.unpack("H", header[28:30])[0]
                if len(data) - 30 - fsize - dsize < 0:
                        dsize = len(data) - 30 - fsize
                fstuff = data[30:30+fsize]
                dstuff = data[30+fsize:30+fsize+dsize]
                res.append({"header": header, "file": fstuff, "data": dstuff})
                data = data[30+fsize+dsize:]
        return res

def dump_data(data):
    print("Files in package:")
    for k in data:
        perm, type, z1, z2, tim = struct.unpack("BBHII", k["header"][:12])
        if type == 65:
            type = "dir"
            if not os.access(k["file"], os.F_OK):
                os.makedirs(k["file"])
        if type == 129:
            type = "fil"
            f = open(k["file"], "wb")
            f.write(k["data"])
            f.close()
            os.chmod(k["file"], int(perm))
        if type == 161:
            type = "sym"
            os.symlink(k["data"],k["file"])
            # os.chmod(k["file"], int(perm))
        if perm == 164:
            perm = "nx"
        if perm == 237:
            perm = "ex"
        print(type, perm, k["file"], tim)

def parse_args():
    parser = argparse.ArgumentParser()

    input_group = parser.add_argument_group("input")
    input_group.add_argument("--files",
                             action='append', type=Path, help="Select one or more files to process")

    return parser.parse_args()

if __name__ == "__main__":
    opts = parse_args()

    for file in opts.files:
        with open(file, "rb") as f:
            data = f.read()
            data = parse_data(data)
            dump_data(data)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.