Skip to content

Commit 4cb17c2

Browse files
listscan (nmap -sL)
1 parent bfd1d5d commit 4cb17c2

File tree

7 files changed

+108
-13
lines changed

7 files changed

+108
-13
lines changed

src/main-conf.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ masscan_echo_nic(struct Masscan *masscan, FILE *fp, unsigned i)
109109
(masscan->nic[i].adapter_ip>> 8)&0xFF,
110110
(masscan->nic[i].adapter_ip>> 0)&0xFF
111111
);
112-
fprintf(fp, "adapter-mac = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
112+
fprintf(fp, "adapter-mac%s = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
113113
masscan->nic[i].adapter_mac[0],
114114
masscan->nic[i].adapter_mac[1],
115115
masscan->nic[i].adapter_mac[2],
116116
masscan->nic[i].adapter_mac[3],
117117
masscan->nic[i].adapter_mac[4],
118118
masscan->nic[i].adapter_mac[5]);
119-
fprintf(fp, "router-mac = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
119+
fprintf(fp, "router-mac%s = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
120120
masscan->nic[i].router_mac[0],
121121
masscan->nic[i].router_mac[1],
122122
masscan->nic[i].router_mac[2],
@@ -617,7 +617,8 @@ masscan_set_parameter(struct Masscan *masscan,
617617
}
618618
else if (EQUALS("ports", name) || EQUALS("port", name)) {
619619
rangelist_parse_ports(&masscan->ports, value);
620-
masscan->op = Operation_Scan;
620+
if (masscan->op == 0)
621+
masscan->op = Operation_Scan;
621622
}
622623
else if (EQUALS("exclude-ports", name) || EQUALS("exclude-port", name)) {
623624
rangelist_parse_ports(&masscan->exclude_port, value);
@@ -650,7 +651,8 @@ masscan_set_parameter(struct Masscan *masscan,
650651
else
651652
offset++; /* skip comma */
652653
}
653-
masscan->op = Operation_Scan;
654+
if (masscan->op == 0)
655+
masscan->op = Operation_Scan;
654656
}
655657
else if (
656658
EQUALS("exclude", name) ||
@@ -679,7 +681,8 @@ masscan_set_parameter(struct Masscan *masscan,
679681
else
680682
offset++; /* skip comma */
681683
}
682-
masscan->op = Operation_Scan;
684+
if (masscan->op == 0)
685+
masscan->op = Operation_Scan;
683686
} else if (EQUALS("append-output", name) || EQUALS("output-append", name)) {
684687
if (EQUALS("overwrite", name))
685688
masscan->nmap.append = 0;
@@ -821,6 +824,8 @@ masscan_set_parameter(struct Masscan *masscan,
821824
masscan->resume.seed = parseInt(value);
822825
} else if (EQUALS("resume-index", name)) {
823826
masscan->resume.index = parseInt(value);
827+
} else if (EQUALS("resume-count", name)) {
828+
masscan->resume.count = parseInt(value);
824829
} else if (EQUALS("retries", name) || EQUALS("retry", name)) {
825830
unsigned x = strtoul(value, 0, 0);
826831
if (x >= 1000) {
@@ -1230,8 +1235,8 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
12301235
fprintf(stderr, "nmap(%s): Zombie scans will never be supported\n", argv[i]);
12311236
exit(1);
12321237
case 'L': /* List Scan - simply list targets to scan */
1233-
fprintf(stderr, "nmap(%s): list scan unsupported\n", argv[i]);
1234-
exit(1);
1238+
masscan->op = Operation_ListScan;
1239+
break;
12351240
case 'M':
12361241
fprintf(stderr, "nmap(%s): Maimon scan not yet supported\n", argv[i]);
12371242
exit(1);

src/main-listscan.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "masscan.h"
2+
#include "logger.h"
3+
#include "rand-blackrock.h"
4+
5+
void
6+
main_listscan(struct Masscan *masscan)
7+
{
8+
uint64_t count_ips;
9+
uint64_t count_ports;
10+
uint64_t i;
11+
uint64_t range;
12+
uint64_t start;
13+
uint64_t end;
14+
struct BlackRock blackrock;
15+
unsigned r = masscan->retries + 1;
16+
unsigned increment = masscan->shard.of;
17+
18+
count_ports = rangelist_count(&masscan->ports);
19+
if (count_ports == 0)
20+
rangelist_add_range(&masscan->ports, 80, 80);
21+
count_ports = rangelist_count(&masscan->ports);
22+
23+
count_ips = rangelist_count(&masscan->targets);
24+
if (count_ips == 0) {
25+
LOG(0, "FAIL: target IP address list empty\n");
26+
LOG(0, " [hint] try something like \"--range 10.0.0.0/8\"\n");
27+
LOG(0, " [hint] try something like \"--range 192.168.0.100-192.168.0.200\"\n");
28+
return;
29+
}
30+
31+
range = count_ips * count_ports;
32+
33+
34+
blackrock_init(&blackrock, range, masscan->seed);
35+
36+
start = masscan->resume.index + (masscan->shard.one-1);
37+
end = range;
38+
if (masscan->resume.count && end > start + masscan->resume.count)
39+
end = start + masscan->resume.count;
40+
end += masscan->retries * masscan->max_rate;
41+
42+
43+
for (i=start; i<end; ) {
44+
uint64_t xXx;
45+
unsigned ip;
46+
unsigned port;
47+
48+
49+
xXx = (i + (r--) * masscan->max_rate);
50+
while (xXx >= range)
51+
xXx -= range;
52+
xXx = blackrock_shuffle(&blackrock, xXx);
53+
ip = rangelist_pick(&masscan->targets, xXx % count_ips);
54+
port = rangelist_pick(&masscan->ports, xXx / count_ips);
55+
56+
if (count_ports == 1)
57+
printf("%u.%u.%u.%u\n",
58+
(ip>>24)&0xFF, (ip>>16)&0xFF, (ip>>8)&0xFF, (ip>>0)&0xFF
59+
);
60+
else
61+
printf("%u.%u.%u.%u:%u\n",
62+
(ip>>24)&0xFF, (ip>>16)&0xFF, (ip>>8)&0xFF, (ip>>0)&0xFF,
63+
port
64+
);
65+
66+
if (r == 0) {
67+
i += increment; /* <------ increment by 1 normally, more with shards/nics */
68+
r = masscan->retries + 1;
69+
}
70+
}
71+
}

src/main.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
unsigned control_c_pressed = 0;
6565
static unsigned control_c_pressed_again = 0;
6666
time_t global_now;
67-
static unsigned wait = 10;
67+
static unsigned global_wait = 10;
6868

6969
uint64_t foo_timestamp = 0;
7070
uint64_t foo_count = 0;
@@ -238,6 +238,8 @@ transmit_thread(void *v) /*aka. scanning_thread() */
238238
* is essentially the same logic as shards. */
239239
start = masscan->resume.index + (masscan->shard.one-1) + parms->nic_index;
240240
end = range;
241+
if (masscan->resume.count && end > start + masscan->resume.count)
242+
end = start + masscan->resume.count;
241243
end += retries * rate;
242244

243245

@@ -702,7 +704,9 @@ static void control_c_handler(int x)
702704
{
703705
if (control_c_pressed == 0) {
704706
fprintf(stderr,
705-
"waiting %u seconds to exit... \n", wait);
707+
"waiting %u seconds to exit..."
708+
" \n",
709+
global_wait);
706710
fflush(stderr);
707711
control_c_pressed = 1+x;
708712
} else
@@ -907,8 +911,7 @@ main_scan(struct Masscan *masscan)
907911
unsigned i;
908912
double rate = 0;
909913

910-
min_index = UINT64_MAX;
911-
914+
912915
/* Find the minimum index of all the threads */
913916
min_index = UINT64_MAX;
914917
for (i=0; i<masscan->nic_count; i++) {
@@ -1081,6 +1084,11 @@ int main(int argc, char *argv[])
10811084
* THIS IS THE NORMAL THING
10821085
*/
10831086
return main_scan(masscan);
1087+
1088+
case Operation_ListScan:
1089+
/* Create a randomized list of IP addresses */
1090+
main_listscan(masscan);
1091+
return 0;
10841092

10851093
case Operation_List_Adapters:
10861094
/* List the network adapters we might want to use for scanning */

src/masscan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct Masscan
107107
struct {
108108
uint64_t seed;
109109
uint64_t index;
110+
uint64_t count;
110111
} resume;
111112

112113
struct {
@@ -172,6 +173,7 @@ void masscan_read_config_file(struct Masscan *masscan, const char *filename);
172173
void masscan_command_line(struct Masscan *masscan, int argc, char *argv[]);
173174
void masscan_usage();
174175
void masscan_save_state(struct Masscan *masscan);
176+
void main_listscan(struct Masscan *masscan);
175177

176178
int
177179
masscan_initialize_adapter(

src/output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ output_report_banner(struct Output *out, unsigned ip, unsigned port,
530530
length, px
531531
);
532532
if (count < 80)
533-
fprintf(stdout, "%.*s\n", (size_t)(79-count),
533+
fprintf(stdout, "%.*s\n", (int)(79-count),
534534
" ");
535535
else
536536
fprintf(stdout, "\n");

src/pixie-timer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,19 @@ pixie_nanotime()
206206

207207
return tv.tv_sec * 1000000000 + tv.tv_nsec;
208208
}
209-
#elif defined(__MACH__) /* works for Apple */
209+
#elif defined(__MACH__) || defined(__FreeBSD__) /* works for Apple */
210210
#include <unistd.h>
211211
#include <mach/mach_time.h>
212212

213213
void pixie_usleep(uint64_t microseconds)
214214
{
215215
usleep(microseconds);
216216
}
217+
void
218+
pixie_mssleep(unsigned milliseconds)
219+
{
220+
pixie_usleep(milliseconds * 1000ULL);
221+
}
217222
uint64_t
218223
pixie_gettime()
219224
{

xcode4/masscan.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
11AC80EE17E0DAD4001BCE3A /* proto-icmp.c in Sources */ = {isa = PBXBuildFile; fileRef = 11AC80E917E0DAD4001BCE3A /* proto-icmp.c */; };
5050
11AC80EF17E0DAD4001BCE3A /* proto-ssh.c in Sources */ = {isa = PBXBuildFile; fileRef = 11AC80EB17E0DAD4001BCE3A /* proto-ssh.c */; };
5151
11AC80F617E0ED47001BCE3A /* main-ptrace.c in Sources */ = {isa = PBXBuildFile; fileRef = 11AC80F517E0ED47001BCE3A /* main-ptrace.c */; };
52+
11B039C117E506B400925E7E /* main-listscan.c in Sources */ = {isa = PBXBuildFile; fileRef = 11B039C017E506B400925E7E /* main-listscan.c */; };
5253
11B2DD9E17DE4DD8007FC363 /* templ-payloads.c in Sources */ = {isa = PBXBuildFile; fileRef = 11B2DD9C17DE4DD8007FC363 /* templ-payloads.c */; };
5354
/* End PBXBuildFile section */
5455

@@ -141,6 +142,7 @@
141142
11AC80EC17E0DAD4001BCE3A /* proto-ssh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "proto-ssh.h"; sourceTree = "<group>"; };
142143
11AC80F517E0ED47001BCE3A /* main-ptrace.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "main-ptrace.c"; sourceTree = "<group>"; };
143144
11AC80F817E0EDA7001BCE3A /* main-ptrace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "main-ptrace.h"; sourceTree = "<group>"; };
145+
11B039C017E506B400925E7E /* main-listscan.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "main-listscan.c"; sourceTree = "<group>"; };
144146
11B2DD9C17DE4DD8007FC363 /* templ-payloads.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "templ-payloads.c"; sourceTree = "<group>"; };
145147
11B2DD9D17DE4DD8007FC363 /* templ-payloads.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "templ-payloads.h"; sourceTree = "<group>"; };
146148
/* End PBXFileReference section */
@@ -196,6 +198,7 @@
196198
11A9219C17DBCC7E00DDFD32 /* main-status.h */,
197199
11A9219D17DBCC7E00DDFD32 /* main-throttle.c */,
198200
11A9219E17DBCC7E00DDFD32 /* main-throttle.h */,
201+
11B039C017E506B400925E7E /* main-listscan.c */,
199202
11A9219F17DBCC7E00DDFD32 /* main.c */,
200203
11A921A017DBCC7E00DDFD32 /* masscan.h */,
201204
11A921A117DBCC7E00DDFD32 /* out-binary.c */,
@@ -350,6 +353,7 @@
350353
11AC80EE17E0DAD4001BCE3A /* proto-icmp.c in Sources */,
351354
11AC80EF17E0DAD4001BCE3A /* proto-ssh.c in Sources */,
352355
11AC80F617E0ED47001BCE3A /* main-ptrace.c in Sources */,
356+
11B039C117E506B400925E7E /* main-listscan.c in Sources */,
353357
);
354358
runOnlyForDeploymentPostprocessing = 0;
355359
};

0 commit comments

Comments
 (0)