Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # convert images to Binary Ninja "feature map" images
  4. #
  5. # please share enhancements and cool images you make with andrewl on binja slack
  6. #
  7. # instructions (requires ImageMagick and Netwide Assembler (nasm)):
  8. #
  9. # resize to 128 pixel width:
  10. # $ convert -resize 128 input.png output.png
  11. #
  12. # convert image to black and white with dithering
  13. # $ convert input.jpg -remap pattern:gray50 output.png
  14. #
  15. # run this script to produce out.asm
  16. # $ ./img2binja.py input.jpg
  17. #
  18. # 4) assemble out.asm to whatever binary you want:
  19. # $ nasm -f elf out.asm -o out.elf
  20.  
  21. import sys
  22. from PIL import Image
  23.  
  24. im = Image.open(sys.argv[1])
  25. im = im.convert('RGB')
  26.  
  27. binsize = 0
  28. labelidx = 0
  29. dataidx = 0
  30. asm = []
  31. asm.append('section .text')
  32. asm.append('global _start')
  33.  
  34. # convert all pixels
  35. for y in range(im.height):
  36. for x in range(im.width):
  37. px = im.getpixel((x,y))
  38. # black pixels are orphan null bytes
  39. if px == (0,0,0):
  40. asm.append('db 0')
  41. binsize += 1
  42. # white pixels are small functions
  43. elif px == (255,255,255):
  44. asm.append('loc_%d:' % labelidx)
  45. asm.append('ret')
  46. binsize += 1
  47. labelidx += 1
  48. else:
  49. raise Exception('non black or white color detected:', px)
  50.  
  51. # for all labels created, generate call
  52. asm.append('_start:')
  53. for i in range(labelidx):
  54. asm.append('call loc_%d' % i)
  55. binsize += 5 # e8 XX XX XX XX
  56. asm.append('ret')
  57. binsize += 1
  58.  
  59. # pad file size to get 128-byte scanlines
  60. if 70000 - binsize > 0:
  61. asm.append('resb %d' % (70000 - binsize))
  62.  
  63. # write asm to file
  64. with open('out.asm', 'w') as fp:
  65. fp.write('\n'.join(asm))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement