Hunter的大杂烩 技术学习笔记

2021-09-17

python分析java class字节码工具 javatools demo

Filed under: 技术话题 — hunter @ 7:37 pm

只贴几个主要功能的代码,输出基本跟 javap.exe一样了

 


def cli_class_requires(class_info):
required = class_info.get_requires()
print("File {} requires[{}]:".format(class_info.get_sourcefile(), len(required)))
print(" ", required)

def cli_jar_requires(options, jarinfo):
print("jar requires:")

if True:
requires = jarinfo.get_requires()
filter_name_list = filter_dict(requires)

for filter_name in filter_name_list:
del requires[filter_name]

print(" ", requires)
else:
for entry in ji.get_classes():
ci = ji.get_classinfo(entry)
cli_class_requires(cli)
return
print()

def my_class_info(java_class_info):
print("source file:{}".format(java_class_info.get_sourcefile()))
for java_method in java_class_info.get_all_methods():
if java_method.is_bridge():
continue
print("method:{}".format(java_method.get_name()))
code_info = java_method.get_code()
# ((abs_line, rel_line, [(offset, code, args), ...]),
code_list = code_info.iter_code_by_lines()
print("\tCode:")
for code_desc in code_list:
abs_line = code_desc[0]
rel_line = code_desc[1]
code_line_list = code_desc[2]
for code_line in code_line_list:
func_name_indx_list = code_line[2]
if len(func_name_indx_list) > 0:
def_value = java_class_info.deref_const(func_name_indx_list[0])
def_index = str(func_name_indx_list[0])
else:
def_value = ""
def_index = ""
print("\t{:=3d}: {:<14s} #{:<3s} //{}".format(code_line[0], get_opname_by_code(code_line[1]), def_index, def_value))

if __name__ == '__main__':
if False:
input_file_path=r"xxx.jar"
with JarInfo(filename=input_file_path) as ji:
if options.jar_requires:
cli_jar_requires(options, ji)
else:
for entry in ji.get_classes():
ci = ji.get_classinfo(entry)
my_class_info(ci)
print()
else:
input_file_path=r"xxx.class"
class_file = unpack_classfile(input_file_path)
if options.jar_requires:
cli_class_requires(class_file)
else:
my_class_info(class_file)

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress