Skip to content

Commit

Permalink
add web static moudle
Browse files Browse the repository at this point in the history
Signed-off-by: jiguang <[email protected]>
  • Loading branch information
jiguangsdf committed Aug 4, 2022
1 parent 32efe0f commit 30e16d8
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 19 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ whitespace += $(whitespace)
comma := ,
build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags))

NAME=netcat
UNIX=netcat
WINDOWS=netcat.exe
PACKAGENAME=main

### -X package.name
ldflags = -X $(PACKAGENAME).Name=$(NAME) \
ldflags = -X $(PACKAGENAME).Name=$(UNIX) \
-X $(PACKAGENAME).Version=$(VERSION) \
-X $(PACKAGENAME).Commit=$(COMMIT) \
-X $(PACKAGENAME).BuildTags=$(build_tags_comma_sep) \
Expand All @@ -40,7 +41,7 @@ BUILD_FLAGS += -gcflags='all=-trimpath=$(GOMODCACHE)' -asmflags='all=-trimpath=$
# darwin amd64
build: gofmt clean go.sum
mkdir -p $(BUILDDIR)
go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/$(NAME)
go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/$(UNIX)

# linux amd64
build-linux:
Expand All @@ -53,7 +54,7 @@ build-linux-arm64:
# windows
build-windows: gofmt clean go.sum
mkdir -p $(BUILDDIR)
GOOS=windows GOARCH=amd64 go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/$(NAME)
GOOS=windows GOARCH=amd64 go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/$(WINDOWS)

install: go.sum
go install -mod=readonly -v $(BUILD_FLAGS)
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,26 @@ netcat最重要的一个功能就是提供命令执行功能,这在渗透测

- 正向命令执行

![](images/WX20200317-011030@2x.png)
![](images/p1@2x.png)

- 反向命令执行

![](images/WX20200317-011240@2x.png)
![](images/p2@2x.png)

- 文件传输

![](images/WX20200317-011427@2x.png)
![](images/p3@2x.png)

- 标准输入输出[在线聊天功能]
- 标准输入输出

![](images/WX20200317-011629@2x.png)
![](images/p4@2x.png)

#### web静态服务器

使用netacat轻松实现index目录索引,在实际环境用可以用作下载和上传资源使用

![](images/p5.png)

### 总结

本文使用`Golang`语言实现了简单的`Netcat`功能,文章提及了接口的实现,如:自定义结构体方法用于命令执行结果编码实时转换,以及`io.Copy`等方法的参数查看与具体使用。完整的代码:[netcat - github.com](https://github.com/jiguangin/netcat)
本文使用`Golang`语言实现了简单的`Netcat`功能,文章提及了接口的实现,如:自定义结构体方法用于命令执行结果编码实时转换,以及`io.Copy`等方法的参数查看与具体使用。完整的代码:[netcat - github.com](https://github.com/jiguangsdf/netcat)
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module netcat
module github.com/jiguangsdf/netcat

go 1.15
go 1.18

require github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added images/p5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 36 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"bytes"
"flag"
"fmt"
"github.com/axgle/mahonia"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"

"github.com/axgle/mahonia"
)

var (
Expand All @@ -30,6 +31,7 @@ var (
BuildTags = ""
// application variable
udpNetwork = "udp"
tcpNetwork = "tcp"
udpBufSize = 64 * 1024
)

Expand Down Expand Up @@ -91,16 +93,20 @@ var config struct {
Listen bool
Port int
Network string
Web bool
Path string
Command bool
Host string
}

func init() {
flag.IntVar(&config.Port, "p", 4000, "host port to connect or listen")
flag.BoolVar(&config.Help, "help", false, "print this help")
flag.BoolVar(&config.Verbose, "v", false, "verbose mode")
flag.BoolVar(&config.Verbose, "v", true, "verbose mode")
flag.BoolVar(&config.Listen, "l", false, "listen mode")
flag.BoolVar(&config.Command, "e", false, "shell mode")
flag.BoolVar(&config.Web, "web", false, "web static server")
flag.StringVar(&config.Path, "path", "public", "web static path")
flag.StringVar(&config.Network, "n", "tcp", "network protocol")
flag.StringVar(&config.Host, "h", "0.0.0.0", "host addr to connect or listen")
flag.Usage = usage
Expand Down Expand Up @@ -256,6 +262,16 @@ func dial(network, host string, port int, command bool) {
}
}

func listenWeb(host string, port int, path string) {
listenAddr := net.JoinHostPort(host, strconv.Itoa(port))
logf("Listening web on: %s, path: %s", listenAddr, path)
err := http.ListenAndServe(listenAddr,
http.FileServer(http.Dir(path)))
if err != nil {
logf("Listen web failed: %v", err)
}
}

func main() {
if config.Help {
flag.Usage()
Expand All @@ -268,12 +284,25 @@ func main() {
logf("Exited")
os.Exit(0)
}()

// Web
if config.Web {
listenWeb(config.Host, config.Port, config.Path)
return
}

// Listen
if config.Listen {
if strings.HasPrefix(config.Network, udpNetwork) {
switch config.Network {
case udpNetwork:
listenPacket(config.Network, config.Host, config.Port, config.Command)
case tcpNetwork:
listen(config.Network, config.Host, config.Port, config.Command)
default:
panic("no target network protocol")
}
listen(config.Network, config.Host, config.Port, config.Command)
return
// Dial
} else {
dial(config.Network, config.Host, config.Port, config.Command)
}
dial(config.Network, config.Host, config.Port, config.Command)
}

0 comments on commit 30e16d8

Please sign in to comment.