回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试解包android 11图像/从raw.img获取selinux信息、符号链接等信息</p>
<p>我正在使用这个奇妙的工具:<a href="https://github.com/cubinator/ext4/blob/master/ext4.py35.py" rel="nofollow noreferrer">https://github.com/cubinator/ext4/blob/master/ext4.py35.py</a></p>
<p>我的代码如下所示:</p>
<pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python3
import argparse
import sys
import os
import ext4
parser = argparse.ArgumentParser(description='Read <modes, symlinks, contexts and capabilities> from an ext4 image')
parser.add_argument('ext4_image', help='Path to ext4 image to process')
args = parser.parse_args()
exists = os.path.isfile(args.ext4_image)
if not exists:
print("Error: input file " f"[{args.ext4_image}]" " was not found")
sys.exit(1)
file = open(args.ext4_image, "rb")
volume = ext4.Volume(file)
def scan_dir (root_inode, root_path = ""):
for entry_name, entry_inode_idx, entry_type in root_inode.open_dir():
if entry_name == "." or entry_name == "..":
continue
entry_inode = root_inode.volume.get_inode(entry_inode_idx)
entry_inode_path = root_path + "/" + entry_name
if entry_inode.is_dir:
scan_dir(entry_inode, entry_inode_path)
if entry_inode_path[-1] == '/':
continue
xattrs_perms = list(entry_inode.xattrs())
found_cap = False
found_con = False
if "security.capability" in f"{xattrs_perms}": found_cap = True
if "security.selinux" in f"{xattrs_perms}": found_con = True
contexts = ""
capability = ", \"capabilities\", 0x0"
if found_cap:
if found_con:
capability = f"{xattrs_perms[1:2]}"
else:
capability = f"{xattrs_perms[0:1]}"
capability = capability.split(" ")[1][:-3][+2:].encode('utf-8').decode('unicode-escape').encode('ISO-8859-1')
capability = hex(int.from_bytes(capability[4:8] + capability[14:18], "little"))
capability = ", \"capabilities\", " f"{capability}"
capability = f"{capability}"
if found_con:
contexts = f"{xattrs_perms[0:1]}"
contexts = f"{contexts.split( )[1].split('x00')[0][:-1][+2:]}"
contexts = f"{contexts}"
filefolder = ''.join(entry_inode_path.split('/', 1))
print("set_metadata(\""f"{filefolder}" "\", \"uid\", " f"{str(entry_inode.inode.i_uid)}" ", \"gid\", " f"{str(entry_inode.inode.i_gid)}" ", \"mode\", " f"{entry_inode.inode.i_mode & 0x1FF:0>4o}" f"{capability}" ", \"selabel\", \"" f"{contexts}" "\");")
scan_dir(volume.root)
file.close()
</code></pre>
<p>然后我只需要做<code>./read.py vendor.img</code>就行了</p>
<p>直到最近,我尝试了android 11上的这个奇怪的vendor.img,发现了这个奇怪的问题</p>
<pre><code>Traceback (most recent call last):
File "./tools/metadata.py", line 53, in <module>
scan_dir(volume.root)
File "./tools/metadata.py", line 26, in scan_dir
scan_dir(entry_inode, entry_inode_path)
File "./tools/metadata.py", line 26, in scan_dir
scan_dir(entry_inode, entry_inode_path)
File "./tools/metadata.py", line 29, in scan_dir
xattrs_perms = list(entry_inode.xattrs())
File "/home/semaphore/unpacker/tools/ext4.py", line 976, in xattrs
for xattr_name, xattr_value in self._parse_xattrs(inline_data[offset:], 0, prefix_override = prefix_override):
File "/home/semaphore/unpacker/tools/ext4.py", line 724, in _parse_xattrs
xattr_inode = self.volume.get_inode(xattr.e_value_inum, InodeType.FILE)
NameError: name 'xattr' is not defined
</code></pre>
<p>我已尝试删除if,并仅在此处将代码保留在else之后:<a href="https://github.com/cubinator/ext4/blob/master/ext4.py35.py#L722" rel="nofollow noreferrer">https://github.com/cubinator/ext4/blob/master/ext4.py35.py#L722</a></p>
<p>不幸的是没有运气。看来工具还没完成?但没有其他选择</p>
<p>欢迎任何帮助:)</p>
<p>多谢各位</p>
<p>编辑:有人建议用<code>xattr_entry</code>替换<code>xattr</code>
所以我做了,我得到了这个错误:<code>takes 2 positional arguments but 3 were given</code></p>
<p>我试着修复它,结果:</p>
<pre><code>File "/home/semaphore/unpacker/tools/ext4.py", line 724, in _parse_xattrs
xattr_inode = self.volume.get_inode(xattr_entry.e_value_inum)
File "/home/semaphore/unpacker/tools/ext4.py", line 595, in get_inode
inode_table_offset = self.group_descriptors[group_idx].bg_inode_table * self.block_size
IndexError: list index out of range
</code></pre>
<p>我无法修复此错误:(</p>
<p>除了从原始ext4映像获取selinux信息、功能、uid、gid和权限之外,还有其他方法吗</p>