Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do I generate a sequential string?

by Anonymous Monk
on Apr 22, 2003 at 21:28 UTC ( [id://252366]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find all possible Alphanumeric combinations for 10 characters. (I need to get the password for my DSL Modem.) I think this would be a simple perl script. I've even found a good node for a Random string generator. The following code patterned from the node is nearly what I want. But is there a way of replacing the rand function with a foreach?
# @chars contains characters to get random string from my @chars = (a..z, A..Z, 0..9); my $string = join '', map { @chars[rand @chars] } 1 .. 10; print $string;
Thanks!

Replies are listed 'Best First'.
Re: How do I generate a sequential string?
by Limbic~Region (Chancellor) on Apr 22, 2003 at 21:47 UTC
    Anonymous Monk,
    As others have already pointed you in the right direction. I just want to point out that using lower case, upper case, and digits for all 10 characters , there are

    839,299,365,868,340,224 possibilities.

    If you do not know exactly how many characters out of the 10 are used, it gets even uglier.

  • 1 = 62
  • 2 = 622 = 3,844 + 62 = 3,906
  • 3 = 623 = 238,328 + 3,906 = 242,234
  • 4 = 624 = 14,776,336 + 242,234 = 15,018,570
  • 5 = 625 = 916,132,832 + 15,018,570 = 931,151,402
  • 6 = 626 = 56,800,235,584 + 931,151,402 = 57,731,386,986
  • 7 = 627 = 3,521,614,606,208 + 57,731,386,986 = 3,579,345,993,194
  • 8 = 628 = 218,340,105,584,896 + 3,579,345,993,194 = 221,919,451,578,090
  • 9 = 629 = 13,537,086,546,263,552 + 221,919,451,578,090 = 13,759,005,997,841,642

    And finally, the worst case senario is that it ends up being the very last combination you try out of a 10 character string: 853,058,371,866,181,866

    I doubt this is the best way to go about it. Usually there is a way to reset it to a factory default, a super password, or some such thing - check with the vendor.

    Cheers - L~R

    Update: As I was typing this and doing some calculations dws put my sentiment into far fewer, but more potent words.

    Update 2: I added more number variations to show how complex the problem really is.

      And in case you still don't grasp how many combinations that is, if you have an amazingly fast computer that can try 100,000 combinations per second, it could still take you 270,000 years to find the password.


      We're not surrounded, we're in a target-rich environment!
Re: How do I generate a sequential string?
by dws (Chancellor) on Apr 22, 2003 at 21:41 UTC
    I need to get the password for my DSL Modem.

    I suggest that you determine how many passwords/second you can probe for, then do a little back-of-the-envelope math to see how many days/months/years it will take to probe half the password space. You might find that it's more cost-effective to buy a new modem.

Re: How do I generate a sequential string?
by Corion (Patriarch) on Apr 22, 2003 at 21:38 UTC

    If you are not that interested in efficiency, Perl has nice magic to do what you want :

    perl -le "$a='AAAAAAAAAA'; while ($a lt 'zzzzzzzzzz') { print $a++ }"

    There is only a small problem with my solution - it won't work with numeric strings like starting at 0000000000 and not for strings looking like a number or ending in something that looks like a number - so for these, you will have to come up with another solution. Here is a small hint to that - a string can be interpreted (for example by pack and unpack) as a number in the base of your current alphabet, just like a number in decimal notation is written in an alphabet of 10 letters (0..9). So you can take the number 0, interpret it in your alphabet of digits a..z, A..Z, 0..9, print it, then increment it and so on.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: How do I generate a sequential string?
by dragonchild (Archbishop) on Apr 22, 2003 at 21:30 UTC
    Look here for some ideas.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: How do I generate a sequential string?
by Juerd (Abbot) on Apr 22, 2003 at 21:39 UTC
      I ran this code, and my CPU usage jumped to 80% on my dual processor P3. Perl was using 40% on processor and my anti-virus program kicked in and was using 40% on the other. When I killed the perl script, the anti-virus program stopped. I though that was curious...
Re: How do I generate a sequential string?
by graff (Chancellor) on Apr 23, 2003 at 03:16 UTC
    (I need to get the password for my DSL Modem.)

    A lot of devices with this sort of password protection have a pin-hole reset switch somewhere on the case, which will put the password back to the factory default (and if you lost the manual that tells you what that default is, it's usually something short and easy to guess, like "admin").

Re: How do I generate a sequential string?
by particle (Vicar) on Apr 23, 2003 at 03:09 UTC

    if the math above doesn't scare you off, you might find it faster to write the password combinations to a binary file ahead of time. then you can perform a buffered read on the file, and process the file from memory one record at a time.

    if there are any characters you can eliminate, or other rules you can think of to limit the search space, it may make the problem almost manageable.

    good luck

    ~Particle *accelerates*

Re: How do I generate a sequential string?
by Anonymous Monk on Apr 23, 2003 at 04:01 UTC
    Hi Everyone,

    It looks like your right about trying to brute force the dsl password. I managed to write a small script to generate 600 combinations. Testing the 620 combinations took 5 minutes and 20 seconds.

    I found out that the Puerto Rico telecom company is using passwords of the following form: 30aLPOjbhy

    Unfortunatly, I don't know if the positions are always the same. (i.e. the digits are always there and the upper/lowercase also.) I will probably need to find a super-password for a Zyxel Prestige 600.

    So this is now, just a learning hack. The following code works for a small subset. But I can't think of how to reset the initial values and retry all the additional combinations. Any tips?

    #!/usr/bin/perl -w #my @genString = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); my @genString = ('a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'); my @chars = ("a".."z", "A".."Z", "0".."9"); my $cnt=0; for(my $i; $i <= $#genString; $i++) { foreach my $newChar (@chars) { $genString[$i] = $newChar; print "", @genString, "\n"; # print "$cnt\t\t", @genString, "\n"; $cnt++; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252366]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found