Advertisement
Guest User

tripfag

a guest
Jul 31st, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. #!/usr/bin/python2
  2. # TripGenerator.py
  3. #
  4. # Modified from original source by LAMMJohnson
  5. # Licensed under the GPLv3 pending approval from original author, who
  6. # appears to have released it without license (completely restricted)
  7. #
  8. # Source: http://www.mm-rs.org/forums/topic/19070-your-name-in-a-tripcode
  9. # Optimized by The-Soulless
  10. import re, crypt, sys, signal, string, time, crypt, getopt
  11. sys.setcheckinterval(999999)
  12. SIGINT_RECIEVED = False
  13.  
  14. def errout(msg):
  15.     ''' Display error message, provide usage instructions and exit '''
  16.     print msg, "See", sys.argv[0], "-h for usage instructions"
  17.     exit(0)
  18.  
  19. def find_trips(regexes, quantity):
  20.     '''
  21.    Loop to continuously try to find @quantity instances of @regexes
  22.    by iterating a string and hashing using mktripcode.
  23.    ret: List of tuples - strings and matching strings they hash to
  24.    '''
  25.     matches = []
  26.     i = 1
  27.     lregexes = regexes
  28.     lquantity = quantity
  29.     while len(matches) < lquantity:
  30.         if SIGINT_RECIEVED:
  31.             #print "Exiting gracefully on SIGINT."
  32.             return matches
  33.         generated_trip = mktripcode(str(i))
  34.         #if i % 100000 == 0:
  35.         #    print "Generated", i, "tripcodes..."
  36.         for regex in lregexes:
  37.             if re.search(regex, generated_trip):
  38. #                print "FOUND tripcode:", i, "=>", generated_trip
  39.                 matches.append((i, "#" + generated_trip))
  40.         i = i + 1
  41.     return matches
  42.  
  43. def mktripcode(pw):
  44.     ''' Convert a @pw string into a 4chan tripcode hash '''
  45.     pw = pw.decode('utf_8', 'ignore')\
  46.            .encode('shift_jis', 'ignore')\
  47.            .replace('"', '&quot;')\
  48.            .replace("'", '')\
  49.            .replace('<', '&lt;')\
  50.            .replace('>', '&gt;')\
  51.            .replace(',', ',')
  52.     salt = (pw + '...')[1:3]
  53.     salt = re.compile('[^\.-z]').sub('.', salt)
  54.     salt = salt.translate(string.maketrans(':;<=>?@[\\]^_`', 'ABCDEFGabcdef'))
  55.     trip = crypt.crypt(pw, salt)[-10:]
  56.     trip = " " + trip
  57.     return trip
  58.  
  59. def sigint_handler(sig, _):
  60.     ''' For handling sigint messages gracefully '''
  61.     global SIGINT_RECIEVED
  62.     print "\nSIGINT received."
  63.     SIGINT_RECIEVED = True
  64.  
  65. def usage():
  66.     ''' Display help/usage text and then exit '''
  67.     print sys.argv[0], "is a tripcode generation program"
  68.     print """
  69. You must provide at least one regex to match against, and
  70. this program will find a number of tripcodes that match
  71. that regex for you.
  72.  
  73. Flags:
  74.  -h or --help
  75.      Print this help text and exit
  76.  -n <#> or --number <#>     :: (Default 1)
  77.      Number of matches to find before exiting
  78.  -r <str> or --regex <str>
  79.      Provide regex to match against. You may provide multiple
  80.      regexes and they will all be matched against. You MUST provide
  81.      at least one regex to match against
  82.  
  83.  Hint: You may wish to prepend your searches with (?i) to enable
  84.  case-insensitive matching.
  85. """
  86.     print "Example Usage:", sys.argv[0], "-r '(?i)gentoo' -n 3"
  87.     exit(0)
  88.  
  89. def main():
  90.     ''' Main function '''
  91.     t=time.time()
  92.     short_opts = "hn:r:"
  93.     long_opts = "help number regex".split()
  94.     regexes = []
  95.     quantity = 1
  96.  
  97.     # Handle sigint correctly
  98.     signal.signal(signal.SIGINT, sigint_handler)
  99.  
  100.     # Parse arguments
  101.     try:
  102.         opts, args = getopt.gnu_getopt(sys.argv, short_opts, long_opts)
  103.     except:
  104.         errout("Error parsing args.")
  105.  
  106.     for o, a in opts:
  107.         if o == "-h" or o == "--help":
  108.             usage()
  109.         elif o == "-n" or o == "--number":
  110.             quantity = int(a)
  111.         elif o == "-r" or o == "--regex":
  112.             regexes.append(a)
  113.         else:
  114.             errout("Unrecognised argument " + a)
  115.  
  116.     # Handle incorrect options
  117.     if regexes == []:
  118.         errout("No regexes to match supplied.")
  119.     if quantity < 1:
  120.         errout("Error :: Instructed to find less than 1 match")
  121.  
  122.     # Report work to be done to user
  123.     print "Finding", quantity, "matches of:"
  124.     for s in regexes:
  125.         print s
  126.  
  127.     # Generate matches and report findings
  128.     matches = find_trips(regexes, quantity)
  129.     if len(matches) == 0:
  130.         print "No matches found."
  131.     else:
  132.         for num, trip in matches:
  133.             print num, "hashes to", trip
  134.     print time.time()-t
  135. if __name__ == "__main__":
  136.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement