一.基于作者改为python3兼容的
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‘])
原文:https://blog.51cto.com/pythonywy/2972024