Skip to content

Commit

Permalink
Add "LZMA compress", "LZMA decompress", "XZ compress" and "XZ decompr…
Browse files Browse the repository at this point in the history
…ess"

plugins into "Compression operations" category.
  • Loading branch information
nmantani committed May 12, 2019
1 parent 7c9e6e0 commit 8bfb113
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 7 deletions.
173 changes: 172 additions & 1 deletion Compression operations/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
import StringIO
import zlib

try:
import backports.lzma
lzma_not_installed = False
except ImportError:
lzma_not_installed = True

def aplib_compress(fi):
offset = fi.getSelectionOffset()
length = fi.getSelectionLength()
Expand Down Expand Up @@ -234,7 +240,7 @@ def gzip_decompress(fi):
data = fi.getSelection()
orig = list(fi.getDocument())
orig_len = len(orig)

strio = StringIO.StringIO(data)
gz = gzip.GzipFile(fileobj=strio)
decompressed = gz.read()
Expand Down Expand Up @@ -400,3 +406,168 @@ def raw_inflate(fi):
else:
print "Decompressed %s bytes from offset %s to %s." % (length, hex(offset), hex(offset + length - 1))
print "Added a bookmark to decompressed region."

def lzma_compress(fi):
"""
Compress selected region with LZMA algorithm
"""
if lzma_not_installed:
print "backport.lzma is not installed."
print "Please install it with 'python -m pip install -i https://pypi.anaconda.org/nehaljwani/simple backports.lzma'"
print "and restart FileInsight."
return

offset = fi.getSelectionOffset()
length = fi.getSelectionLength()

if (length > 0):
data = fi.getSelection()
orig = list(fi.getDocument())
orig_len = len(orig)

compressed = list(backports.lzma.compress(data, format=backports.lzma.FORMAT_ALONE))
final_size = len(compressed)
newdata = [0] * (orig_len - (length - final_size))

for i in range(0, offset):
newdata[i] = orig[i]

for i in range(0, final_size):
newdata[offset + i] = compressed[i]

for i in range(0, orig_len - offset - length):
newdata[offset + final_size + i] = orig[offset + length + i]

fi.newDocument("New file", 1)
fi.setDocument("".join(newdata))
fi.setBookmark(offset, final_size, hex(offset), "#c8ffff")

if (length == 1):
print "Compressed one byte from offset %s to %s." % (hex(offset), hex(offset))
else:
print "Compressed %s bytes from offset %s to %s." % (length, hex(offset), hex(offset + length - 1))
print "Added a bookmark to compressed region."

def lzma_decompress(fi):
"""
Decompress selected region with LZMA algorithm
"""
if lzma_not_installed:
print "backport.lzma is not installed."
print "Please install it with 'python -m pip install -i https://pypi.anaconda.org/nehaljwani/simple backports.lzma'"
print "and restart FileInsight."
return

offset = fi.getSelectionOffset()
length = fi.getSelectionLength()

if (length > 0):
data = fi.getSelection()
orig = list(fi.getDocument())
orig_len = len(orig)

decompressed = list(backports.lzma.decompress(data, format=backports.lzma.FORMAT_ALONE))
final_size = len(decompressed)
newdata = [0] * (orig_len - (length - final_size))

for i in range(0, offset):
newdata[i] = orig[i]

for i in range(0, final_size):
newdata[offset + i] = decompressed[i]

for i in range(0, orig_len - offset - length):
newdata[offset + final_size + i] = orig[offset + length + i]

fi.newDocument("New file", 1)
fi.setDocument("".join(newdata))
fi.setBookmark(offset, final_size, hex(offset), "#c8ffff")

if (length == 1):
print "Decompressed one byte from offset %s to %s." % (hex(offset), hex(offset))
else:
print "Decompressed %s bytes from offset %s to %s." % (length, hex(offset), hex(offset + length - 1))
print "Added a bookmark to decompressed region."

def xz_compress(fi):
"""
Compress selected region with XZ format
"""
if lzma_not_installed:
print "backport.lzma is not installed."
print "Please install it with 'python -m pip install -i https://pypi.anaconda.org/nehaljwani/simple backports.lzma'"
print "and restart FileInsight."
return

offset = fi.getSelectionOffset()
length = fi.getSelectionLength()

if (length > 0):
data = fi.getSelection()
orig = list(fi.getDocument())
orig_len = len(orig)

compressed = list(backports.lzma.compress(data))
final_size = len(compressed)
newdata = [0] * (orig_len - (length - final_size))

for i in range(0, offset):
newdata[i] = orig[i]

for i in range(0, final_size):
newdata[offset + i] = compressed[i]

for i in range(0, orig_len - offset - length):
newdata[offset + final_size + i] = orig[offset + length + i]

fi.newDocument("New file", 1)
fi.setDocument("".join(newdata))
fi.setBookmark(offset, final_size, hex(offset), "#c8ffff")

if (length == 1):
print "Compressed one byte from offset %s to %s." % (hex(offset), hex(offset))
else:
print "Compressed %s bytes from offset %s to %s." % (length, hex(offset), hex(offset + length - 1))
print "Added a bookmark to compressed region."

def xz_decompress(fi):
"""
Decompress selected XZ compressed region
"""
if lzma_not_installed:
print "backport.lzma is not installed."
print "Please install it with 'python -m pip install -i https://pypi.anaconda.org/nehaljwani/simple backports.lzma'"
print "and restart FileInsight."
return

offset = fi.getSelectionOffset()
length = fi.getSelectionLength()

if (length > 0):
data = fi.getSelection()
orig = list(fi.getDocument())
orig_len = len(orig)

decompressed = list(backports.lzma.decompress(data))
final_size = len(decompressed)
newdata = [0] * (orig_len - (length - final_size))

for i in range(0, offset):
newdata[i] = orig[i]

for i in range(0, final_size):
newdata[offset + i] = decompressed[i]

for i in range(0, orig_len - offset - length):
newdata[offset + final_size + i] = orig[offset + length + i]

fi.newDocument("New file", 1)
fi.setDocument("".join(newdata))
fi.setBookmark(offset, final_size, hex(offset), "#c8ffff")

if (length == 1):
print "Decompressed one byte from offset %s to %s." % (hex(offset), hex(offset))
else:
print "Decompressed %s bytes from offset %s to %s." % (length, hex(offset), hex(offset + length - 1))
print "Added a bookmark to decompressed region."

6 changes: 5 additions & 1 deletion Compression operations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ def __init__(self):
compression.bzip2_decompress,
compression.gzip_compress,
compression.gzip_decompress,
compression.lzma_compress,
compression.lzma_decompress,
compression.lznt1_compress,
compression.lznt1_decompress,
compression.raw_deflate,
compression.raw_inflate)
compression.raw_inflate,
compression.xz_compress,
compression.xz_decompress)

# Structure for mouse cursor position
class _point_t(ctypes.Structure):
Expand Down
6 changes: 5 additions & 1 deletion Compression operations/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
"Bzip2 decompress",
"Gzip compress",
"Gzip decompress",
"LZMA compress",
"LZMA decompress",
"LZNT1 compress",
"LZNT1 decompress",
"Raw deflate",
"Raw inflate")
"Raw inflate",
"XZ compress",
"XZ decompress")
exit_value = -1

root = Tkinter.Tk()
Expand Down
17 changes: 15 additions & 2 deletions README.ja.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ AES decrypt 等の暗号関係のプラグインについては、PyCryptodome P
https://github.com/Legrandin/pycryptodome からダウンロード、インストールするか、
"c:\Python27\python.exe -m pip install pycryptodomex" を実行してインストールしてください。

LZMA Compress 等の LZMA 関係のプラグインについては、backports.lzma Python モジュールが必要です。
https://github.com/peterjc/backports.lzma/ からダウンロード、インストールするか、
"c:\Python27\python.exe -m pip install -i https://pypi.anaconda.org/nehaljwani/simple backports.lzma"
を実行してインストールしてください。

File type プラグインについては、python-magic Python モジュールが必要です。
https://github.com/ahupp/python-magic からダウンロード、インストールするか、
"c:\Python27\python.exe -m pip install python-magic-bin" を実行してインストール
Expand All @@ -42,14 +47,14 @@ https://github.com/VirusTotal/yara-python からダウンロード、インス
"c:\Python27\python.exe -m pip install yara-python" を実行してインストールして
ください。

以下のコマンドで全ての必要な Python モジュールをインストールできます
以下のコマンドで必要な Python モジュールのうちいくつかをまとめてインストールできます
c:\Python27\python.exe -m pip install -r requirements.txt

カスタマイズ:
Send to プラグインについては、あなたのお好みのプログラムを呼び出せるように
"Misc operations\send_to.json" を編集してください。

プラグインの一覧 (62個):
プラグインの一覧 (66個):
* Basic operations
* Copy to new file
選択範囲を(選択していない場合は全体を)新しいファイルとして開きます。
Expand Down Expand Up @@ -87,6 +92,10 @@ Send to プラグインについては、あなたのお好みのプログラム
選択範囲を gzip 形式で圧縮します。
* Gzip decompress
gzip 形式で圧縮された選択範囲を展開します。
* LZMA compress
選択範囲を LZMA アルゴリズムで圧縮します。
* LZMA decompress
選択範囲を LZMA アルゴリズムで展開します。
* LZNT1 compress
選択範囲を LZNT1 アルゴリズムで圧縮します。
* LZNT1 decompress
Expand All @@ -97,6 +106,10 @@ Send to プラグインについては、あなたのお好みのプログラム
* Raw inflate
ヘッダとチェックサムを付けずに Deflate 圧縮された選択範囲を展開します。
(PHP言語の gzinflate() と同等)
* XZ compress
選択範囲を XZ 形式で圧縮します。
* XZ decompress
選択範囲を XZ 形式として展開します。

* Crypto operations
* AES decrypt
Expand Down
16 changes: 14 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ For crypto-related plugins such as "AES decrypt", it requires PyCryptodome Pytho
Please get it from https://github.com/Legrandin/pycryptodome
or execute "c:\Python27\python.exe -m pip install pycryptodomex".

For LZMA-related plugins such as "LZMA Compress", it requires backports.lzma Python module.
Please get it from https://github.com/peterjc/backports.lzma/
or execute "c:\Python27\python.exe -m pip install -i https://pypi.anaconda.org/nehaljwani/simple backports.lzma".

For the "File type" plugin, it requires python-magic Python module.
Please get it from https://github.com/ahupp/python-magic
or execute "c:\Python27\python.exe -m pip install python-magic-bin" .
Expand All @@ -37,14 +41,14 @@ For the "YARA scan" plugin, it requires yara-python Python module.
Please get it from https://github.com/VirusTotal/yara-python
or execute "c:\Python27\python.exe -m pip install yara-python" .

You can install all required Python modules with the following command.
You can install some of required Python modules with the following command.
c:\Python27\python.exe -m pip install -r requirements.txt

Customization:
For the "Send to" plugin, please edit "Misc operations\send_to.json" to run your
favorite programs.

List of plugins (62 plugins):
List of plugins (66 plugins):
* Basic operations
* Copy to new file
Copy selected region (the whole file if not selected) to new file
Expand Down Expand Up @@ -82,6 +86,10 @@ List of plugins (62 plugins):
Compress selected region with gzip format
* Gzip decompress
Decompress selected gzip compressed region
* LZMA compress
Compress selected region with LZMA algorithm
* LZMA decompress
Decompress selected region with LZMA algorithm
* LZNT1 compress
Compress selected region with LZNT1 algorithm
* LZNT1 decompress
Expand All @@ -92,6 +100,10 @@ List of plugins (62 plugins):
* Raw inflate
Decompress selected Deflate compressed region that does not have header and
checksum (Equivalent to gzinflate() in PHP language)
* XZ compress
Compress selected region with XZ format
* XZ decompress
Decompress selected XZ compressed region

* Crypto operations
* AES decrypt
Expand Down

0 comments on commit 8bfb113

Please sign in to comment.