前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安卓逆向class分析辅助脚本

安卓逆向class分析辅助脚本

作者头像
小小咸鱼YwY
发布2021-06-11 18:48:42
5260
发布2021-06-11 18:48:42
举报
文章被收录于专栏:python-爬虫python-爬虫

原地址https://www.52pojie.cn/thread-687475-1-1.html

一.基于作者改为python3兼容的

代码语言:javascript
复制
import sys,getopt


def bytesToHexString(bs):
    return ''.join(['%02X ' % b for b in bs]).replace(" ",'')



def moshu(name):
	try:
		f=open(name,'rb')
		print ("-------------------------------")
		print ("********** magic **************")
		print ("-------------------------------")
		i=0
		p=[1]*4
		p1=[202,254,186,190]
		while True:
			t=f.read(1)
			t1=bytesToHexString(t)
			p[i]=int(t1,16)
			i=i+1
			if i==4:
				break
			pass
		f.close()
		panduan=1;
		for i in range(0,len(p)):
			if p[i]!=p1[i]:
				panduan=0
			print ("%s"%hex(p[i]),)
			print ("(%d)"%p[i],)
			print ("\n")
			pass
		if(panduan==1):
			print ('This is a java class')
			print ("-------------------------------------------------------------------------------")
			print ('The magic number is the check bit, and the JVM determines whether the magic number is a .class file. \nThe number of spells compiled by the Java compiler is: CA FE BA BE.')
			print ("-------------------------------------------------------------------------------")
		else: 
			print ('Sorry,I am an AI that analyzes class files.')
			print ("I don't want to know about other documents'")
	except IOError : 
			print ("This is bad for input '",name,"'.")
			print ("You can enter -h for help.")

def zhuzhu():
	print ('---------------------------')
	print ('|    class analysis 1.0    |')
	print ('|    -------love zhuzhu    |')
	print ('|               HAI_ZHU    |')
	print ('---------------------------')
def help():
	print ('-h Format:class.py -h 			Mean:help you do somethings')
	print ('-m Format:class.py -m filename   	Mean:Analysis of the magic')
	print ('-v Format:class.py -v filename   	Mean:Analysis of the minor_version')
	print ('-V Format:class.py -V filename   	Mean:Analysis of the major_version')
	print ('-c Format:class.py -c filename   	Mean:Analysis of the constant_pool_count')
	print ('-C Format:class.py -C filename   	Mean:Analysis of the cp_info')
	print ('-a Format:class.py -a filename   	Mean:Analysis of the access_flags')


def minor_version(name):
	try:
		f=open(name,'rb')
		print ("-------------------------------")
		print ("******** minor_version ********")
		print ("-------------------------------")
		f.read(4)
		i=0
		p=[1]*2
		while True:
			t=f.read(1)
			t1=bytesToHexString(t)
			p[i]=int(t1,16)
			if(p[i]!=0):
				print ("Sorry,The subversion number is normally 0 and this is not 0.")
				exit()
			i=i+1
			if i==2:
				break
			pass
		f.close()
		print ("This is a good file.")
		print ("The second version occupies the fifth and sixth bytes, which are generally 0.")
	except IOError : 
			print ("This is bad for input '",name,"'.")
			print ("You can enter -h for help.")

def major_version(name):
	try:
		f=open(name,'rb')
		print ("-------------------------------")
		print ("******** minor_version ********")
		print ("-------------------------------")
		f.read(7)
		t=f.read(1)
		print(t)
		t1=bytesToHexString(t)
		p=int(t1,16)
		f.close()
		if(p>45):
			print ("This is a good file.")
			print ("The jdk version is 1.",p-44)
	except IOError : 
			print ("This is bad for input '",name,"'.")
			print ("You can enter -h for help.")

def constant_pool_count(name):
	try:
		f=open(name,'rb')
		print ("-------------------------------")
		print ("**** constant_pool_count ******")
		print ("-------------------------------")
		f.read(9)
		t=f.read(1)
		t1=bytesToHexString(t)
		p=int(t1,16)
		f.close()
		print ("constant_pool_count is",p,".")
		print ("Next has",p-1,"cp_info.")
	except IOError : 
			print ("This is bad for input '",name,"'.")
			print ("You can enter -h for help.")

def cp_info(name):
	try:
		f=open(name,'rb')
		print ("-------------------------------")
		print ("**** constant_pool_count ******")
		print ("-------------------------------")
		f.read(9)
		t=f.read(1)
		t1=bytesToHexString(t)
		p=int(t1,16)
		i=p
		x=0
		j=1
		while (j<=i):
			t=f.read(1)
			x=x+1;
			t1=bytesToHexString(t)
			p=int(t1,16)
			if(p==10):
				print ("This is CONSTANT_Methodref_info. #",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("class_index #",p)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("name_and_type_index #",p)
				x=x+4
			elif (p==9):
				print ("This is CONSTANT_Fieldref_info.#",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("class_index #",p)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("name_and_type_index #",p)
				x=x+4
			elif (p==7):
				print ("This is CONSTANT_Class_info.#",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("name_index #",p)
				x=x+2
			elif (p==7):
				print ("This is CONSTANT_String_index.#",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("String_index #",p)
				x=x+2
			elif (p==3):
				print ("This is CONSTANT_Integer_info.#",j)
				print ("INT")
				t=f.read(4)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("INI save",p)
				x=x+4
			elif(p==4):
				print ("This is Constant_float_info.#",j)
				print ("FLOAT")
				t=f.read(4)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("FLOAT")
				x=x+4
			elif(p==5):
				print ("This is CONSTANT_Long_info.#",j)
				print ("LONG")
				t=f.read(8)
				x=x+8
			elif(p==6):
				print ("This is Constant_Double_info.#",j)
				print ("DOUBLE")
				t=f.read(8)
				x=x+8
			elif(p==8):
				print ("This is CONSTANT_String_info.#",j)
				print ("STRING")
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("string_index.#",	p)
				x=x+2
			elif (p==1):
				print ("This is CONSTANT_Utf8_info.#",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("save length:",p)
				t=f.read(p)
				x=x+2
				x=x+p
			elif (p==12):
				print ("This is CONSTANT_Name_Type_info.#",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("name_index:#",p)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("descriptor_index:#",p)
				x=x+4
			elif(p==11):
				print ("This is CONSTANT_InterfaceMethodref_info.#",j)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("interface_class_index:#",p)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				print ("name_and_type_index:#",p)
				x=x+4
			j=j+1
			print ("-------------------------------")
		print ("End",x-1,"bytes")

	except IOError : 
			print ("This is bad for input '",name,"'.")
			print ("You can enter -h for help.")

def access_flags(name):
	try:
		f=open(name,'rb')
		print ("-------------------------------")
		print ("******** access_flags *********")
		print ("-------------------------------")
		f.read(9)
		t=f.read(1)
		t1=bytesToHexString(t)
		p=int(t1,16)
		i=p
		x=0
		j=1
		while (j<=i):
			t=f.read(1)
			x=x+1;
			t1=bytesToHexString(t)
			p=int(t1,16)
			if(p==10):
				t=f.read(2)
				t1=bytesToHexString(t)
				print(t1)
				p=int(t1,16)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+4
			elif (p==9):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+4
			elif (p==7):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+2
			elif (p==7):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+2
			elif (p==3):
				t=f.read(4)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+4
			elif(p==4):
				t=f.read(4)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+4
			elif(p==5):
				t=f.read(8)
				x=x+8
			elif(p==6):
				t=f.read(8)
				x=x+8
			elif(p==8):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+2
			elif (p==1):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				t=f.read(p)
				x=x+2
				x=x+p
			elif (p==12):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+4
			elif(p==11):
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				t=f.read(2)
				t1=bytesToHexString(t)
				p=int(t1,16)
				x=x+4
			j=j+1
		t=f.read(1)
		t1=bytesToHexString(t)
		print ("access_flags is:",t1)

	except IOError : 
			print ("This is bad for input '",name,"'.")
			print ("You can enter -h for help.")

def main(argv):
	print(argv)
	inputfile=''
	outputfile=''
	try:
		opts,args=getopt.getopt(argv,"hm:i:o:v:V:c:C:a:")
	except getopt.GetoptError:
		print ("This is bad!")
		print ("You can enter -h for help.")
		sys.exit()
	for opt,arg in opts:
		if opt=='-h':
			help()
			sys.exit()
		elif opt in ("-v"):
			minor_version(arg)
		elif opt in ("-V"):
			major_version(arg)
		elif opt in ("-m"):
			moshu(arg)
		elif opt in ("-c"):
			constant_pool_count(arg)
		elif opt in ("-C"):
			cp_info(arg)
		elif opt in ("-a"):
			access_flags(arg)
zhuzhu()
if __name__=="__main__":
	# main(sys.argv[1:])
	main(['-V', 'first.class'])
	main(['-v', 'first.class'])
	main(['-c', 'first.class'])
	main(['-C', 'first.class'])
	main(['-a', 'first.class'])
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-06-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档