![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
My brute forcer in C
This is a program I wrote a while ago and posted at http://lineman.net/node/520 . This is the updated and final version. Given the character set "set" and the size of the array "gss" (in this case 3) it will print all the possible values. She is pretty fast, just prone to stack overflows because it's recursive. Enjoy.
/*
Copyright 2005(c) Ian Treyball
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This program is provided "as is".
The author takes no responsibility for anything done with it.
_caution_ If the program recurses too deeply you can have a stack overflow.
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
static void incr(const unsigned short int pos);
static const char set[] = {
'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', '$', '!', '%', '*'
};
static char gss[3];
static unsigned long int cnt = 0;
static FILE *rslts;
int main(int argc, char **argv) {
memset(&gss, '\0', sizeof(gss));
rslts = fopen("results.txt", "w");
const unsigned int pssbl = pow(sizeof(set), sizeof(gss));
while(cnt < pssbl)
incr(0);
if(fclose(rslts) != 0) {
fprintf(stderr, "%s", "error closing file");
return -1;
}
printf("%s completed successfully\n", argv[0]);
return 0;
}
static void incr(const unsigned short int pos) {
cnt++;
if(gss[pos] != '\0') {
static unsigned short int n;
for(n = 0; n < sizeof(set); ++n) {
if(gss[pos] == set[n]) {
gss[pos] = set[n + 1];
fputs(gss, rslts);
fputc('\n', rslts);
break;
}
}
static unsigned short int i;
for(i = 0; i < sizeof(gss); ++i) {
if(gss[i] != set[sizeof(set) - 1]) {
incr(i);
break;
} else {
gss[i] = set[0];
}
}
} else {
gss[pos] = set[0];
fputs(gss, rslts);
fputc('\n', rslts);
}
return;
}
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5
![]() |
So this computes every possible combination of letters and $, *, ! in a 3 character word?
EDIT: Oh...You explained it in your first post
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
Quote:
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
I tried to compile and run it, but it had some errors. Sounds cool though.
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
Quote:
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
I'm using Dev-C++ 4, these are the errors
38 main.c parse error before `const' 39 main.c `pssbl' undeclared (first use in this function) 61 main.c parse error before `static' 62 main.c `i' undeclared (first use in this function) |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Yes but which compiler are you using. Dev-C++ is an IDE, it uses another compiler such as GCC.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Out of the box, Dev C++ would use gcc for C and g++ for C++, the mingw variety, I believe. Some errors might be compiler specific, particularly the one about "i".
EDIT: Results with VC++ 6.0 a) Requires the inclusion of string.h or memory.h for memset. b) VC++ 6.0 compiling C doesn't allow the declaration of variables "down in the code", after instructions have been generated. This affects pssbl and also "i", in the incr function. c) assigning the return of "pow" to pssbl will generate a warning unless cast. I didn't do it in Dev C++, but this covers every error Navid mentions, and adds one (memset).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers Last edited by DaWei; Sep 6th, 2005 at 6:23 AM. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
Okay, so I lied, because then that must not be the final version. I'm posted some revised code.
/*
Copyright 2005(c) Ian Treyball
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This program is provided "as is".
The author takes no responsibility for anything done with it.
_caution_ If the program recurses too deeply you can have a stack overflow.
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
static void incr(const unsigned short int pos);
static const char set[] = {
'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'
};
static char gss[4]; // for longer words, change the number here
static unsigned long int cnt = 0;
static unsigned int i = 0, n = 0;
static FILE *rslts;
int main(int argc, char **argv) {
memset(&gss, '\0', sizeof(gss));
rslts = fopen("results.txt", "w");
// this seems reasonable for computing an okay size buffer.
const size_t BUF_SIZE = sizeof(gss)/2 > 1?1024 * (sizeof(gss)/2) : 2048;
char *const buf = (char *)malloc(BUF_SIZE);
setvbuf(rslts, buf, _IOFBF, BUF_SIZE);
// thanks to DaWei for bringing this former uncasted call to my attention.
const unsigned int pssbl = (unsigned int)pow(sizeof(set), sizeof(gss));
while(cnt < pssbl) {
incr(0);
}
// responsible for flushing rslts, too
if(fclose(rslts) != 0) {
fprintf(stderr, "%s", "error closing file");
return -1;
}
free(buf);
printf("%s was successful\n", argv[0]);
return 0;
}
/*
* The heart of this program. Each call to this produces
* the next value assigned to gss[] and writes it out to rslts.
*/
static void incr(const unsigned short int pos) {
cnt++;
if(gss[pos] != '\0') {
for(n = 0; n < sizeof(set); ++n) {
if(gss[pos] == set[n]) {
gss[pos] = set[n + 1];
fputs(gss, rslts);
fputc('\n', rslts);
break;
}
}
for(i = 0; i < sizeof(gss); ++i) {
if(gss[i] != set[sizeof(set) - 1]) {
incr(i);
break;
} else {
gss[i] = set[0];
}
}
} else {
gss[pos] = set[0];
fputs(gss, rslts);
fputc('\n', rslts);
}
return;
}
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|