Skip to content

Instantly share code, notes, and snippets.

@arsane
Last active September 15, 2015 08:28
Show Gist options
  • Save arsane/4a24bdade2e349430648 to your computer and use it in GitHub Desktop.
Save arsane/4a24bdade2e349430648 to your computer and use it in GitHub Desktop.
dump ijc (java card applet) file.
#!/usr/bin/python
import sys
comp_tag_names = [
"None",
"Header", # 1
"Directory", # 2
"Applet", # 3
"Import", # 4
"ConstantPool", # 5
"Class", # 6
"Method", # 7
"StaticField", # 8
"ReferenceLocation", # 9
"Export", # 10
"Descriptor", # 11
"Deubg" # 12
]
components_install = [1, 2, 4, 3, 6, 7, 8, 10, 5, 9]
def prt(x):
print x
# bytearray -> [(tag, bytearray)]
def get_components(data):
if len(data) == 0:
return []
tag, size = (data[0], 256*data[1] + data[2])
return [(tag, data[3:size+3])] + get_components(data[size+3:])
def get_rawcode(cmps):
raw = bytearray()
cmps_code = sum([filter(lambda (tag, data) : tag == i, cmps) for i in components_install], [])
for x in cmps_code:
tag, data = x
raw += data
return raw
def hexdump(data):
map(lambda x: prt("\t" + " ".join("{:02X}".format(int(i)) for i in x)),
[data[x:x+16] for x in range(0, len(data), 16)])
def dump_component(com):
tag, data = com
print "\n", comp_tag_names[tag], ":", len(data), "bytes"
hexdump(data)
if __name__ == "__main__":
if len(sys.argv) == 1:
print "Help:", sys.argv[0], "[ijc file]"
exit(1)
with open(sys.argv[1], "rb") as f:
comps = get_components(bytearray(f.read()))
map(lambda x: dump_component(x), comps)
hexdump(get_rawcode(comps))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment