This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
at-brute.njs
192 lines (164 loc) · 6.32 KB
/
at-brute.njs
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
//
// at-brute.js
// at-brute
//
// Created by Rolandas Razma on 02/02/2014.
//
// Copyright (c) 2013 Rolandas Razma <[email protected]>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// arguments
var packageJSON = require('./package.json');
var optimist = require('optimist')
.usage('at-brute v'+packageJSON.version+'\nRolandas Razma <[email protected]>\n\nUsage: $0 [OPTIONS] --start')
.options({
p: {
alias: 'port',
default: '/dev/tty.usbserial',
describe: 'Specify the serial port to use'
},
b: {
alias: 'baud',
default: 115200,
describe: 'Specify the baud rate'
},
o: {
alias : 'offset',
default : 0,
describe: 'Specify offset where to start'
},
s: {
alias : 'separator',
default : '+',
describe: 'Specify separator of AT and command.'
},
t: {
alias : 'timeout',
default : 0,
describe: 'Specify timeout in seconds for command response before retrying. 0 to disable.'
},
f: {
alias : 'file',
describe: 'Log to CSV file'
},
ESLP: {
describe: 'Start with AT+ESLP=0 - no sleep on mobile devices',
default : false
}
});
var argv = optimist.argv;
// check for --start
if( !argv.start ) {
optimist.showHelp();
return;
}
// require
require('colors');
var StringGenerator = require('./modules/stringgenerator/stringgenerator.js').StringGenerator;
var strftime = require('strftime');
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var csv;
var timeoutTimer;
// do we need logging?
if( argv.file ){
csv = require('csv');
csv = csv().to(argv.file, {delimiter: ',', escape: '"'});
csv.write( ["Command No", "AT", "Response"] );
}
// Connect to port
var serialPort = new SerialPort(argv.port, { baudrate: argv.baud, parser: serialport.parsers.readline("\n") }, false);
serialPort.open(function() {
var dataBuffer = [];
// SerialPort data
serialPort.on('data', function( data ) {
data = data.trim();
if( data === "OK" ){
if( dataBuffer.length === 2 ){
dataBuffer.push("OK");
}
if( csv ){
csv.write( dataBuffer );
}
console.log("\t"+dataBuffer[2].green);
tryNextCommand();
}else if ( data === "ERROR" ){
tryNextCommand();
}else if( data.length ){
dataBuffer.push(data);
}
});
// SerialPort close
serialPort.on('close', function () {
console.log("\n* "+argv.port+" closed".red);
});
// SerialPort error
serialPort.on('error', function( error ) {
console.log("\n* ERROR - "+error.toString().red);
});
var stringGenerator = new StringGenerator(["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"], argv.offset);
var stepNo = 0;
console.log("* open "+argv.port+" - "+"[OK]".green);
console.log("* start brute force AT commands with offset " +stringGenerator.offset +" (AT" +argv.separator +stringGenerator.current()+"?)");
if( argv.file ){
console.log("* logging to "+argv.file);
}
function tryNextCommand(){
dataBuffer = [String(stringGenerator.offset)];
// generate command
var command = "AT" +argv.separator +stringGenerator.current() +((stepNo%2)?"=?":"?");
tryCommand( command );
if( ++stepNo%2 ){
stringGenerator.offset++;
}
}
function tryCommand( command ){
// time out timer
if( argv.timeout ){
if( timeoutTimer ) {
clearTimeout( timeoutTimer );
}
timeoutTimer = setTimeout(function(){
console.log(" - timed out.".red);
timeoutTimer = null;
tryCommand( command );
}, argv.timeout *1000);
}
// Show on screen
process.stdout.write("\r["+strftime('%F %T', new Date())+" - "+stringGenerator.offset+"] "+command+((stepNo%2)?"":" "));
// write
serialPort.write(command+"\n", function(err, results) {
if( err ) {
console.log("\n* ERROR - "+err.toString().red);
}
});
}
if( argv.ESLP ){
// write
dataBuffer = ["*"];
process.stdout.write("\r["+strftime('%F %T', new Date())+" - *] AT+ESLP=0");
serialPort.write("AT+ESLP=0\n", function(err, results) {
if( err ) {
console.log("\n* ERROR - "+err.toString().red);
}
});
}else{
tryNextCommand();
}
});