Skip to content

Commit 107fb96

Browse files
DESKTOP-OB8V0P7\AdministratorDESKTOP-OB8V0P7\Administrator
authored andcommitted
1. 优化了 ICMP Ping 实现 (ping.go)
修复了潜在的无限循环问题,现在只读取有限次数的数据包 改进了 ICMP 消息构造,使用正确的 icmp.Echo 结构体 添加了对 ICMP 回复消息的验证,确保只在收到 ICMP Echo Reply 时返回 true 增强了错误处理和日志记录 2. 改进了 IP 扫描功能 (check_ip.go) 添加了并发控制,限制同时进行的 ping 操作数量,防止系统资源耗尽 改进了错误处理,当解析子网失败时会报告错误而不是静默忽略 优化了输出格式,当没有 IP 地址可显示时会显示"(无)" 添加了详细的函数文档注释 3. 完善了子网解析功能 (parse_subnet.go) 改进了网络地址和广播地址的处理逻辑 添加了详细的函数文档注释 优化了排除规则匹配逻辑 4. 增强了程序入口点 (main.go) 改进了帮助信息的显示,添加了使用示例 增加了更清晰的错误处理和退出码 5. 完善了数据结构定义 (common.go) 为 HostInfo 结构体添加了详细注释 6. 实现了 ICMP 监听功能 (listen_icmp.go) 为之前空缺的文件添加了完整的 ICMP 监听实现
1 parent a3623bd commit 107fb96

10 files changed

Lines changed: 362 additions & 36 deletions

File tree

README.md

Lines changed: 177 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,184 @@
11
# lmap
22

3-
lmap (LinuxHub's Nmap) is the nmap next generation pro plus max, made by 浪神 (from THE GREAT [LinuxHub](https://github.com/LinuxHub-Group)).
3+
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
44

5-
# LICENSE
5+
<h3 align="center">LinuxHub's Nmap - 下一代网络扫描工具</h3>
66

7-
Copyright (C) <2021> <LinuxHub-Group>
7+
<p align="center">
8+
比 nmap 更强大、更灵活的网络探测与扫描能力
9+
<br/>
10+
<a href="#使用方法"><strong>探索 lmap 的功能 »</strong></a>
11+
<br/>
12+
<br/>
13+
<a href="https://github.com/LinuxHub-Group/lmap/issues">报告 Bug</a>
14+
·
15+
<a href="https://github.com/LinuxHub-Group/lmap/issues">请求新功能</a>
16+
·
17+
<a href="#贡献">贡献</a>
18+
</p>
819

9-
This program is free software: you can redistribute it and/or modify
10-
it under the terms of the GNU General Public License as published by
11-
the Free Software Foundation, either version 3 of the License, or
12-
(at your option) any later version.
20+
## 目录
1321

14-
This program is distributed in the hope that it will be useful,
15-
but WITHOUT ANY WARRANTY; without even the implied warranty of
16-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17-
GNU General Public License for more details.
22+
- [关于项目](#关于项目)
23+
- [功能特性](#功能特性)
24+
- [快速开始](#快速开始)
25+
- [安装](#安装)
26+
- [使用方法](#使用方法)
27+
- [构建选项](#构建选项)
28+
- [贡献](#贡献)
29+
- [许可证](#许可证)
1830

19-
You should have received a copy of the GNU General Public License
20-
along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
## 关于项目
32+
33+
lmap (LinuxHub's Nmap) 是 LinuxHub 团队开发的下一代网络扫描工具,被称为 nmap 的 Pro Plus Max 版本。它是一个现代化的网络扫描工具,旨在提供比传统 nmap 更强大、更灵活的网络探测与扫描能力。
34+
35+
该工具专为网络安全工程师、系统管理员和渗透测试人员设计,帮助他们快速识别网络中的活动主机、开放端口和服务。
36+
37+
### 为什么选择 lmap?
38+
39+
- **快速扫描** - 利用 Go 语言的并发特性,实现高速网络扫描
40+
- **跨平台支持** - 支持 Windows、Linux、macOS 等多种操作系统
41+
- **易于使用** - 简洁的命令行界面,直观的参数设置
42+
- **高度可定制** - 支持灵活的排除规则和扫描配置
43+
- **开源免费** - 基于 GPL-3.0 许可证,完全开源
44+
45+
## 功能特性
46+
47+
- [x] 🚀 **网络扫描** - 快速扫描指定网络段中的活动主机
48+
- [x]**IP检查** - 验证IP地址有效性并检查主机是否在线
49+
- [x] 🔍 **子网解析** - 解析CIDR格式的子网并生成IP地址列表
50+
- [x] 📡 **ICMP监听** - 监听网络中的ICMP数据包
51+
- [x] 🏓 **Ping探测** - 使用ICMP协议探测主机是否在线
52+
- [x]**并发处理** - 支持高并发扫描,提高扫描效率
53+
- [x] 🚫 **排除规则** - 支持排除特定IP或子网,避免扫描不必要目标
54+
- [x] 📋 **详细输出** - 提供详细的扫描过程信息
55+
56+
## 快速开始
57+
58+
### 安装
59+
60+
#### 预编译二进制文件
61+
62+
[Releases](https://github.com/LinuxHub-Group/lmap/releases) 页面下载适用于您系统的预编译二进制文件。
63+
64+
#### 从源码构建
65+
66+
**要求**:
67+
- Go 1.16 或更高版本
68+
69+
```bash
70+
# 克隆项目
71+
git clone https://github.com/LinuxHub-Group/lmap.git
72+
cd lmap
73+
74+
# 构建
75+
make
76+
77+
# 或者直接使用Go构建
78+
go build ./cmd/lmap
79+
```
80+
81+
构建完成后,您将在项目根目录下获得 [lmap](file:///D:/works/lmap/lmap/lmap.exe) 可执行文件。
82+
83+
### 使用方法
84+
85+
#### 基本扫描
86+
87+
```bash
88+
# 扫描单个子网
89+
./lmap -subnet 192.168.1.0/24
90+
91+
# 扫描多个子网
92+
./lmap -subnet 192.168.1.0/24 -subnet 10.0.0.0/16
93+
94+
# 详细输出模式
95+
./lmap -subnet 192.168.1.0/24 -v
96+
```
97+
98+
#### 排除特定IP或子网
99+
100+
```bash
101+
# 排除单个IP
102+
./lmap -subnet 192.168.1.0/24 -exclude 192.168.1.1
103+
104+
# 排除多个IP或子网
105+
./lmap -subnet 192.168.1.0/24 -exclude 192.168.1.1 -exclude 192.168.1.10/32
106+
```
107+
108+
#### 命令行选项
109+
110+
| 选项 | 描述 | 示例 |
111+
|------|------|------|
112+
| `-subnet` | 要扫描的网络段,CIDR格式 (可多次指定) | `-subnet 192.168.1.0/24` |
113+
| `-exclude` | 要排除的IP或子网 (可多次指定) | `-exclude 192.168.1.1` |
114+
| `-v` | 详细输出模式 | `-v` |
115+
116+
## 构建选项
117+
118+
```bash
119+
# 默认构建当前平台版本
120+
make
121+
122+
# 构建所有平台版本
123+
make all
124+
125+
# 格式化代码
126+
make fmt
127+
128+
# 运行测试
129+
make test
130+
131+
# 清理构建产物
132+
make clean
133+
```
134+
135+
## 贡献
136+
137+
欢迎任何形式的贡献!如果您想为 lmap 做出贡献,请遵循以下步骤:
138+
139+
1. Fork 项目
140+
2. 创建您的特性分支 (`git checkout -b feature/AmazingFeature`)
141+
3. 提交您的更改 (`git commit -m 'Add some AmazingFeature'`)
142+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
143+
5. 开启一个 Pull Request
144+
145+
### 开发环境设置
146+
147+
```bash
148+
# 1. 克隆项目
149+
git clone https://github.com/LinuxHub-Group/lmap.git
150+
151+
# 2. 进入项目目录
152+
cd lmap
153+
154+
# 3. 安装依赖
155+
make install
156+
157+
# 4. 运行测试
158+
make test
159+
```
160+
161+
## 社区和支持
162+
163+
- [报告 Bug](https://github.com/LinuxHub-Group/lmap/issues)
164+
- [请求新功能](https://github.com/LinuxHub-Group/lmap/issues)
165+
- [查看已知问题](https://github.com/LinuxHub-Group/lmap/issues)
166+
167+
## 许可证
168+
169+
```
170+
Copyright (C) <2021> <LinuxHub-Group>
171+
172+
This program is free software: you can redistribute it and/or modify
173+
it under the terms of the GNU General Public License as published by
174+
the Free Software Foundation, either version 3 of the License, or
175+
(at your option) any later version.
176+
177+
This program is distributed in the hope that it will be useful,
178+
but WITHOUT ANY WARRANTY; without even the implied warranty of
179+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
180+
GNU General Public License for more details.
181+
182+
You should have received a copy of the GNU General Public License
183+
along with this program. If not, see <https://www.gnu.org/licenses/>.
184+
```

cmd/lmap/main.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,44 @@ import (
2828

2929
func main() {
3030
isVerbose := false
31-
flag.BoolVar(&isVerbose, "v", false, "be verbose")
31+
flag.BoolVar(&isVerbose, "v", false, "详细输出")
3232

3333
subnets := multiArg{}
34-
flag.Var(&subnets, "subnet", "network to scan (can be specified multiple times)")
34+
flag.Var(&subnets, "subnet", "要扫描的网络段,CIDR格式 (可多次指定)")
3535

3636
excludes := multiArg{}
37-
flag.Var(&excludes, "exclude", "IP or subnet to exclude (can be specified multiple times)")
37+
flag.Var(&excludes, "exclude", "要排除的IP或子网 (可多次指定)")
3838

3939
flag.Parse()
4040

4141
if len(subnets) < 1 {
42-
_, _ = fmt.Fprintf(os.Stderr, "使用方法:%s [-v] -subnet <网络号>/<CIDR> [-subnet ...] [-exclude <IP>/<CIDR>] [-exclude ...]\n", os.Args[0])
43-
os.Exit(-1)
42+
printUsage()
43+
os.Exit(1)
4444
}
4545

4646
lmap.CheckIP(subnets, excludes, isVerbose)
4747
}
4848

49+
// printUsage prints the usage information for the program
50+
func printUsage() {
51+
progName := os.Args[0]
52+
if progName == "" {
53+
progName = "lmap"
54+
}
55+
56+
fmt.Fprintf(os.Stderr, "使用方法: %s [选项] -subnet <网络号>/<CIDR> [-subnet ...]\n", progName)
57+
fmt.Fprintln(os.Stderr, "")
58+
fmt.Fprintln(os.Stderr, "选项:")
59+
flag.PrintDefaults()
60+
fmt.Fprintln(os.Stderr, "")
61+
fmt.Fprintln(os.Stderr, "示例:")
62+
fmt.Fprintf(os.Stderr, " %s -subnet 192.168.1.0/24\n", progName)
63+
fmt.Fprintf(os.Stderr, " %s -subnet 192.168.1.0/24 -subnet 10.0.0.0/16\n", progName)
64+
fmt.Fprintf(os.Stderr, " %s -subnet 192.168.1.0/24 -exclude 192.168.1.1\n", progName)
65+
fmt.Fprintf(os.Stderr, " %s -subnet 192.168.1.0/24 -exclude 192.168.1.10/32 -v\n", progName)
66+
}
67+
68+
// multiArg implements the flag.Value interface for collecting multiple string values
4969
type multiArg []string
5070

5171
func (m *multiArg) String() string {

go.mod

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
module github.com/LinuxHub-Group/lmap
22

3-
go 1.16
3+
go 1.23.0
44

55
require (
6-
github.com/ysmood/got v0.30.0
7-
golang.org/x/net v0.0.0-20220607020251-c690dde0001d
6+
github.com/ysmood/got v0.41.0
7+
golang.org/x/net v0.43.0
8+
)
9+
10+
require (
11+
github.com/google/go-cmp v0.6.0 // indirect
12+
github.com/ysmood/gop v0.2.0 // indirect
13+
github.com/yuin/goldmark v1.4.13 // indirect
14+
golang.org/x/crypto v0.41.0 // indirect
15+
golang.org/x/mod v0.26.0 // indirect
16+
golang.org/x/sync v0.16.0 // indirect
17+
golang.org/x/sys v0.35.0 // indirect
18+
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b // indirect
19+
golang.org/x/term v0.34.0 // indirect
20+
golang.org/x/text v0.28.0 // indirect
21+
golang.org/x/tools v0.35.0 // indirect
22+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
823
)

go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2+
github.com/ysmood/gop v0.2.0 h1:+tFrG0TWPxT6p9ZaZs+VY+opCvHU8/3Fk6BaNv6kqKg=
3+
github.com/ysmood/gop v0.2.0/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
14
github.com/ysmood/got v0.30.0 h1:n6KVknQ2gjU2FsEVvMrpEw20uoqkhRDvAoNK69bOV/U=
25
github.com/ysmood/got v0.30.0/go.mod h1:pE1l4LOwOBhQg6A/8IAatkGp7uZjnalzrZolnlhhMgY=
6+
github.com/ysmood/got v0.41.0 h1:XiFH311ltTSGyxjeKcNvy7dzbJjjTzn6DBgK313JHBs=
7+
github.com/ysmood/got v0.41.0/go.mod h1:W7DdpuX6skL3NszLmAsC5hT7JAhuLZhByVzHTq874Qg=
8+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
9+
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
10+
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
311
golang.org/x/net v0.0.0-20220607020251-c690dde0001d h1:4SFsTMi4UahlKoloni7L4eYzhFRifURQLw+yv0QDCx8=
412
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
13+
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
14+
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
15+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
516
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
617
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
718
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
20+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
21+
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b/go.mod h1:4ZwOYna0/zsOKwuR5X/m0QFOJpSZvAxFfkQT+Erd9D4=
822
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
23+
golang.org/x/term v0.34.0/go.mod h1:5jC53AEywhIVebHgPVeg0mj8OD3VO9OzclacVrqpaAw=
924
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
25+
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
1026
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
27+
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=
28+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/lmap/check_ip.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,40 @@ import (
2525
"time"
2626
)
2727

28-
const OUTPUT_IP_PER_LINE = 3
28+
const (
29+
OUTPUT_IP_PER_LINE = 3
30+
MAX_CONCURRENT_PINGS = 100
31+
)
2932

33+
// CheckIP scans the given subnets for active hosts
34+
// subnets: list of CIDR notation networks to scan
35+
// excludes: list of IPs or subnets to exclude from scanning
36+
// isVerbose: if true, print detailed information during scanning
3037
func CheckIP(subnets, excludes []string, isVerbose bool) {
3138
checkerGroup := &sync.WaitGroup{}
3239
t := time.Now()
3340

41+
// Use a semaphore to limit concurrent pings
42+
semaphore := make(chan struct{}, MAX_CONCURRENT_PINGS)
43+
3444
var allHosts []HostInfo
3545
for _, subnet := range subnets {
36-
hosts, _ := GetAllIPsFromCIDR(subnet, excludes)
46+
hosts, err := GetAllIPsFromCIDR(subnet, excludes)
47+
if err != nil {
48+
_, _ = fmt.Fprintf(os.Stderr, "Error parsing subnet %s: %v\n", subnet, err)
49+
continue
50+
}
3751
allHosts = append(allHosts, hosts...)
3852
}
3953

4054
for index := range allHosts {
4155
checkerGroup.Add(1)
56+
semaphore <- struct{}{} // Acquire semaphore
4257

4358
go func(index int) {
4459
defer checkerGroup.Done()
60+
defer func() { <-semaphore }() // Release semaphore
61+
4562
allHosts[index].isUsed = Ping(allHosts[index].host)
4663
if isVerbose {
4764
if allHosts[index].isUsed {
@@ -62,8 +79,10 @@ func CheckIP(subnets, excludes []string, isVerbose bool) {
6279
printIPList(allHosts, false)
6380
}
6481

82+
// printIPList prints the list of IPs based on the filter
83+
// hosts: list of HostInfo to print
84+
// boolFilter: if true, print used IPs; if false, print unused IPs
6585
func printIPList(hosts []HostInfo, boolFilter bool) {
66-
6786
position := 1
6887

6988
for _, hostInfo := range hosts {
@@ -77,5 +96,12 @@ func printIPList(hosts []HostInfo, boolFilter bool) {
7796
position++
7897
}
7998
}
80-
fmt.Println()
99+
100+
// Only add a newline if we've printed something and it didn't end with a newline
101+
if position > 1 && (position-1)%OUTPUT_IP_PER_LINE != 0 {
102+
fmt.Println()
103+
} else if position == 1 {
104+
// No IPs to print
105+
fmt.Println("(无)")
106+
}
81107
}

pkg/lmap/common.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ package lmap
2020

2121
import "net"
2222

23+
// HostInfo represents information about a host in the network
2324
type HostInfo struct {
24-
host net.IP
25+
// host is the IP address of the host
26+
host net.IP
27+
// isUsed indicates whether the host is active/responding
2528
isUsed bool
2629
}

0 commit comments

Comments
 (0)