0

I am checking a piece of Python code I found online (http://www.exploit-db.com/exploits/18305/), and I'm stuck over a piece of code.

To be honest I don't know Python, but I have experience in other programming languages.

The method _computeCollisionChars generates a number of characters and then adds them to a dictionary if they are different. Below is the method signature along with the relevant part for the question. Note: The actual code can be found on the linked source.

def _computeCollisionChars(self, function, count, charrange):
    baseStr = ""
    baseHash = function(baseStr) # What is this?

    for item in source:
        tempStr = ""

        if tempStr == baseStr:
            continue
        if function(tempStr) == baseHash: # What type of comparison is this?
            # logic goes here...

    return

My questions are:

  1. What does the function parameter mean in the method signature?
  2. Most importantly what does function(string) do? Why are there two checks, and what sort of output does function(tempStr) generate then?

Thanks a lot guys!

2 Answers 2

3

Apparently you can pass any callable object as function to _computeCollisionChars. Then baseHash is initialised as the result of calling function with an empty string as parameter. Inside the loop, the condition reads: if the result of function called with an empty string as parameter equals the baseHash do this and that. Which is kind of senseless, because tempStr is always '' and baseHash never changes (or you didn't post that part).

In the current snippet the second if is never reached, because invariably tempStr == baseStr == ''.

As the commentors pointed out, in the real code tempStr and baseStr do indeed change and function is expected to be a hashing-function (but any other function which takes a string as argument should work).

Sign up to request clarification or add additional context in comments.

3 Comments

"Which is kind of senseless, because tempStr is always '' and baseHash never changes (or you didn't post that part)." Yeah, the actual code says tempstr = ''.join(item)
Yeah, in the source, _computeCollisionChars is called in other methods such as _computePHPCollisionChars and different hashing functions (DJBX31A for example) are passed through as function.
@Hyperboreus - Ok, now it does make sense :) Thanks a lot for your explanation and all the detail you've provided, and sorry if I've confused you with the empty string code snippet.
1

In Python functions are first class objects, so they can be passed as arguments to other functions just fine. So function(baseStr) is calling the function object passed to _computeCollisionChars.

Note that Python doesn't check that it is a function object passed as an argument - it just implicitly expects this (and the program would crash it is wasn't, raising a TypeError exception).

>>> def f1():
        print "Hello world"

>>> def f2(function):
        function()

>>> f2(f1)
Hello World

>>> f2("not a callable function")
TypeError: 'str' object is not callable

2 Comments

Thank you for the explanation and also for the explanation and preview of how the function works and is passed around! Unfortunately Hyperboreus had a more thorough answer. As much as I'd like to I do not have enough reputation to upvote your answer. Sorry.
But I do have. +1 from me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.