C程序是从命令行工作,但不是从Python脚本?

时间:2014-04-26 07:51:04

标签: c string python-2.7 hash machine-learning

更新了整个python脚本。

好的,所以我正在Coursera.org上学习机器学习课程,我想看看这些类型的算法可以用加密技术做什么,并测试是否有可能破解加密说用神经网络。首先,我需要为训练集创建一个哈希表,但我遇到了C字符串数组,args和Python将字符串作为args传递给C程序的麻烦。

这是我的名为hashpipe的lil C程序

    #include <stdio.h>
    #define __USE_GNU
    #include <crypt.h>
    #include <string.h>

    int main(int argc, char** argv){
            char * salt = argv[2];
            struct crypt_data data;
            data.initialized = 0;
            char * result = crypt_r(argv[1], id, &data);
            printf("%s\n", result);
            return 0 // This was the bugger!!! I forgot it!!!
    }

调用这个小程序的Python脚本......请注意,我不确定我是否正确使用exit

    #!/usr/bin/env python2.7

    import sys, random, subprocess

    pathToWL = str(sys.argv[1]) #Path to WordList
    pathForHT = str(sys.argv[2]) #Path to create Hash Table; no, its not smokable
    mId = str(sys.argv[3]) #id for use with crypt_r() see 'man 3 crypt_r'

    SaltCharSet = str("a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9")
    SaltCharSet = SaltCharSet.split(" ")

    try:
        fdWL = open(pathToWL, 'r')
    except:
        print "Could not open wordlist file."
        exit()

    try:
        fdHT = open(pathForHT, 'a')
    except:
        print "Could not open file for hash table"
        fdWL.close()
        exit()

    #We have our wordlist now step through the thing and start generating hashes.

    toStop = False
    cursor = 0 #Use the cursor later once this program evolves

    while(not toStop):
        try:
            ln = str(fdWL.readline())
        except:
            toStop = True
            continue
        ln = ln.strip("\n")
        if len(ln) < 6:
            continue
        # create random salts
        # send ln, id, and salts to hashpipe
        salt = []
        lenOfSalt = random.randint(6,16)
        while(len(salt) < lenOfSalt + 1):
            aORn = random.randint(0,1)
            if aORn == 0:# Its a letter
               uORl = random.randint(0,1)
                if uORl == 0:
                    salt.append(SaltCharSet[(random.randint(0,25))].upper())
                elif uORl == 1:
                    salt.append(SaltCharSet[(random.randint(0,25))].lower())
                else:
                    print "Random Int 'uORl' out of bounds"
                    fdHT.close()
                    fdWL.close()
                    toStop = True
                    exit() # I don't know what happened
                    break #in case exit() fails or is used incorrectly

            elif aORn == 1:# Its a number
                salt.append(SaltCharSet[(random.randint(26, 35))])
            else:
                print "Random Int 'aORn' out of bounds"
                fdHT.close()
                fdWL.close()
                toStop = True
                exit() # I don't know what happened
                break #in case exit() fails or is used incorrectly
        #Generated Salt
        salt = "".join(salt)
        wholeArg2 = str("$"+mId+"$"+salt)
        try:
            mHash = str(subprocess.check_output(["hashpipe", ln, wholeArg2]))
        except:
            print " error getting hash"
            #Clean-up
            fdHT.close()
            fdWL.close()
            toStop = True
            exit()
            break
        #Generated hash, now write it to the fdHT file
        print str(ln+" "+wholeArg2+"\t"+mHash)
        fdHT.write(str(ln+"\t"+mHash+"\n"))
        cursor = fdWL.tell()

    fdHT.close()
    fdWL.close()

    return 0 #Yes, I forgot it here too, probably why my script never ended! LOL!!! so simple!!!

我一直在大力改变这一部分,没有任何作用,这笔交易是什么?它适用于命令行,但不适用于python。例如,strip('\0')str(r"$")是我最近做的一些修改,但仍然无法正常工作。也许我只是写一个bash脚本而不是......

注意,编译的C程序在cmd-line上执行它应该做的事情。

1 个答案:

答案 0 :(得分:1)

DOH! 我需要在我的C程序中返回0。它的工作现在只是为了让你知道。创建我的第一个哈希表:)