Do I not know enough python to do this?

Put your programming skills to the test in these challenges.

Do I not know enough python to do this?

Post by ghostheadx2 on Fri May 22, 2015 3:17 pm
([msg=88154]see Do I not know enough python to do this?[/msg])

So, I decided to try an easier challenge that uses Python since I already know some Python. I figured maybe this would be something I could solve. I wanted to know if there's an equivalent to the alert() function in JavaScript in Python. I want to know because when I tried to print or return the key or password, it won't show up. When I try to print the password, it can't recognize it as a global variable. When I try to print the key, it won't recognize it as a global variable. I try printing it within the function but the problem is that it gives me this no matter what I try to put:

Code: Select all
Usage: %s [encrypt/decrypt] [in_file] [out_file] [password]


I get syntax errors, or out of range errors, but I can fix those. But it won't print the key or password regardless of what I do.

Here's the original python code without my changes:

Code: Select all
#!/usr/bin/python

import sys

"""
Daedalus Corporation encryption script.
"""

def xor(input_data, key):
    result = ""
    for ch in input_data:
        result += chr(ord(ch) ^ key)

    return result

def encrypt(input_data, password):
    key = 0
    for ch in password:
        key ^= ((2 * ord(ch) + 3) & 0xff)

    return xor(input_data, key)

def decrypt(input_data, password):
    return encrypt(input_data, password)

def usage():
    print("Usage: %s [encrypt/decrypt] [in_file] [out_file] [password]" % sys.argv[0])
    exit()

def main():
    if len(sys.argv) < 5:
        usage()

    input_data = open(sys.argv[2], 'r').read()
    result_data = ""

    if sys.argv[1] == "encrypt":
        result_data = encrypt(input_data, sys.argv[4])
    elif sys.argv[1] == "decrypt":
        result_data = decrypt(input_data, sys.argv[4])
    else:
        usage()

    out_file = open(sys.argv[3], 'w')
    out_file.write(result_data)
    out_file.close()

main()


So first I tried printing the password, like this at the end:

Code: Select all
main()
print password


Then I got rid of that and tried this:

Code: Select all
win32api.MessageBox(0, password, key, 0x00001000)


I thought that was like a JS alert box but it wasn't correct syntax. Even if it was, I don't think it would have worked. I got that second one from here:

http://stackoverflow.com/questions/177287/alert-boxes-in-python

Then I tried printing again within the functions after getting rid of the alert box:

Code: Select all
    if sys.argv[1] == "encrypt":
        result_data = encrypt(input_data, sys.argv[4])
        print password
        print key
    elif sys.argv[1] == "decrypt":
        result_data = decrypt(input_data, sys.argv[4])
        print password
        print key
    else:
        usage()
        print password
        print key


or instead this:

Code: Select all
def encrypt(input_data, password):
    key = 0
    for ch in password:
        key ^= ((2 * ord(ch) + 3) & 0xff)

    return xor(input_data, key)
    print password


What I want to return the result_data variable, it doesn't do anything either:

Code: Select all
def main():
    if len(sys.argv) < 5:
        usage()

    input_data = open(sys.argv[2], 'r').read()
    result_data = ""

    if sys.argv[1] == "encrypt":
        result_data = encrypt(input_data, sys.argv[4])
    elif sys.argv[1] == "decrypt":
        result_data = decrypt(input_data, sys.argv[4])
    else:
        usage()

    return result_data


At this point I'm stumped. Is there anything I could possibly Google that would give me a good answer? It returns the same thing every time it can understand the syntax, and not the flag/password/key. Its infuriating. Could someone please give me a hint OR could someone please tell me if this is above my level? I'd really appreciate it. Thanks.

This is still from PicoCTF. Its just a more novice/beginner friendly challenge.

-- Fri May 22, 2015 3:32 pm --

Also, I found an online tool that might help me. The hint is that its an XOR key. What do I enter into this tool:

http://www.abcel-online.com/tools.php?id=decrypt

I tried copying the code and the separate XOR encrypted file. I want to know which part of the code I copy or if I copy that separate encrypted file. The challenge is the password so theoretically this tool should give it to me right? Am I wrong?
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Do I not know enough python to do this?

Post by cyberdrain on Mon May 25, 2015 2:46 pm
([msg=88183]see Re: Do I not know enough python to do this?[/msg])

It looks like simple XOR encryption, learn the weaknesses of that cipher and use them to your advantage...
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Do I not know enough python to do this?

Post by ghost107 on Mon May 25, 2015 4:23 pm
([msg=88186]see Re: Do I not know enough python to do this?[/msg])

XOR:
Code: Select all
x y |x^y
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0

The XOR operation is a bitwise operation:
- when the bits are the same te result will be 0,
- when the bits are different the result will be 1
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests