Hack Forums
[Python] Split passwords from a file - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Split passwords from a file (/showthread.php?tid=5309226)



Split passwords from a file - l3k0w - Jun 18, 2016

I tried using crunch and hydra for generating passwords and brute-forcing. But hydra's limit is 50,000,000 lines per file and my file was exceeding that limit. So I thought it will be a good idea if I try and create program that splits the file into separate files.

So I came up with this. I know it's not something complicated, but maybe it can help someone 🙂

Code
def main():
    fr_name = input("Enter the name of the file to read from: ")
    num_passwords = int(input("Enter the number of passwords per file: "))

    n = 0
    k = 1

    fw = open("1.lst", "w")

    with open(fr_name) as fr:
        for line in fr:
            if(n == num_passwords):
                n = 0
                k = k + 1

                if(not fw.closed):
                    fw.close()

                fw = open(str(k) + ".lst", "w")
            #end

            fw.write(line)
            n = n + 1
        #end
    #end

    fw.close()
    print("The passwords were split into " + str(k) + " separate files.")
#end

if __name__ == "__main__":
    main()

Q: What are you guys using for splitting a file/generating passwords? Does crunch has a command for splitting?


* - C0ldplay - Jun 18, 2016

Easier way...

dd if=crunch.one of=Crunch.one.partone bs=1M count=50 skip=0 creates a file named Crunch.one.partone with 50 but that can be changed to your liking.


* - l3k0w - Jun 18, 2016

(Jun 18, 2016 09:18 AM)rob_ert Wrote:  Easier way...

dd if=crunch.one of=Crunch.one.partone bs=1M count=50 skip=0 creates a file named Crunch.one.partone with 50 but that can be changed to your liking.

Nice one!

But can you explain what bs, count and skip are for and how we can use them?


* - C0ldplay - Jun 19, 2016

(Jun 18, 2016 10:02 AM)l3k0w Wrote:  Nice one!

But can you explain what bs, count and skip are for and how we can use them?

bs= sets the block size. For example, bs=1 would set it to 1MiB.
count= copies the set amount of blocks.


* - Goodies - Jun 19, 2016

Lol don't use dd. dd uses block sizes, not line counts. This is statistically very likely to end up somewhere in the middle of a word/sentence...

There's an actual split utility in linux:
http://linux.die.net/man/1/split

This will split a line into multiple files with no more than 10,000,000 lines each:
Code
split -l 10000000

The default naming is pretty strange. Use -d for part numbers instead of letters ( and -S if you think there are going to be more than 100 files. In your case, I doubt it.


If you actually want a Python version, though. You can do something like this:

PHP Code

<?php 
import os
.path

count 
= 0
filename 
= "test.txt"
max_lines = 1
fname
, ext = os.path.splitext(os.path.basename(filename))
template = "{}.{:03}{}"
fout = None
with open
(filename) as fin:
    for 
i, line in enumerate(fin):
        if 
not i % max_lines:
            if 
fout:
                
fout.close()
            
fout = open(template.format(fname, count, ext), "w")
            
count += 1
        fout
.write(line)