encdec.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. This file is part of the XSSer project, https://xsser.03c8.net
  6. Copyright (c) 2010/2019 | psy <epsylon@riseup.net>
  7. xsser is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation version 3 of the License.
  10. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. details.
  14. You should have received a copy of the GNU General Public License along
  15. with xsser; if not, write to the Free Software Foundation, Inc., 51
  16. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. import urllib.request, urllib.parse, urllib.error
  19. class EncoderDecoder(object):
  20. """
  21. Class to help encoding and decoding strings with different hashing or
  22. encoding algorigthms..
  23. """
  24. # encdec functions:
  25. def __init__(self):
  26. self.encmap = { "Str" : lambda x : self._fromCharCodeEncode(x),
  27. "Hex" : lambda x : self._hexEncode(x),
  28. "Hes" : lambda x : self._hexSemiEncode(x),
  29. "Une" : lambda x : self._unEscape(x),
  30. "Dec" : lambda x : self._decEncode(x),
  31. "Mix" : lambda x : self._unEscape(self._fromCharCodeEncode(x))
  32. }
  33. def _fromCharCodeEncode(self, string):
  34. """
  35. Encode to string.
  36. """
  37. encoded=''
  38. for char in string:
  39. encoded=encoded+","+str(ord(char))
  40. return encoded[1:]
  41. def _hexEncode(self, string):
  42. """
  43. Encode to hex.
  44. """
  45. encoded=''
  46. for char in string:
  47. encoded=encoded+"%"+hex(ord(char))[2:]
  48. return encoded
  49. def _hexSemiEncode(self, string):
  50. """
  51. Encode to semi hex.
  52. """
  53. encoded=''
  54. for char in string:
  55. encoded=encoded+"&#x"+hex(ord(char))[2:]+";"
  56. return encoded
  57. def _decEncode(self, string):
  58. """
  59. Encode to decimal.
  60. """
  61. encoded=''
  62. for char in string:
  63. encoded=encoded+"&#"+str(ord(char))
  64. return encoded
  65. def _unEscape(self, string):
  66. """
  67. Escape string.
  68. """
  69. encoded=''
  70. for char in string:
  71. encoded=encoded+urllib.parse.quote(char)
  72. return encoded
  73. def _ipDwordEncode(self, string):
  74. """
  75. Encode to dword.
  76. """
  77. encoded=''
  78. tblIP = string.split('.')
  79. # In the case it's not an IP
  80. if len(tblIP)!=4:
  81. return 0
  82. for number in tblIP:
  83. tmp=hex(int(number))[2:]
  84. if len(tmp)==1:
  85. tmp='0' +tmp
  86. encoded=encoded+tmp
  87. return int(encoded,16)
  88. def _ipOctalEncode(self, string):
  89. """
  90. Encode to octal.
  91. """
  92. encoded=''
  93. tblIP = string.split('.')
  94. # In the case it's not an IP
  95. if len(tblIP)!=4:
  96. return 0
  97. octIP = [oct(int(s)).zfill(4) for s in tblIP]
  98. return ".".join(octIP)
  99. if __name__ == "__main__":
  100. encdec = EncoderDecoder()
  101. print(encdec._ipOctalEncode("127.0.0.1"))