HFish/utils/setting/setting.go

444 lines
11 KiB
Go
Raw Normal View History

2019-08-07 13:16:23 +08:00
package setting
import (
"HFish/core/exec"
"HFish/utils/color"
"HFish/view"
"fmt"
"github.com/gin-gonic/gin"
"io"
"os"
"net/http"
"time"
2019-12-03 21:28:24 +08:00
"HFish/utils/cache"
2019-08-07 13:16:23 +08:00
"HFish/utils/conf"
"HFish/core/protocol/ssh"
"HFish/core/protocol/redis"
"HFish/core/protocol/mysql"
2019-08-11 20:14:28 +08:00
"HFish/core/protocol/ftp"
"HFish/core/protocol/telnet"
2020-04-12 22:19:13 +08:00
"HFish/core/protocol/custom"
2019-08-10 18:26:43 +08:00
"HFish/core/rpc/server"
"HFish/core/rpc/client"
"HFish/view/api"
"HFish/utils/cors"
"HFish/core/protocol/memcache"
2019-10-28 22:25:23 +08:00
"HFish/core/protocol/tftp"
"HFish/core/protocol/httpx"
"HFish/core/protocol/elasticsearch"
"HFish/core/protocol/vnc"
2019-12-03 21:28:24 +08:00
"HFish/core/dbUtil"
"strconv"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
2020-02-24 19:50:43 +08:00
"syscall"
2020-04-12 22:19:13 +08:00
"HFish/utils/ping"
2019-08-07 13:16:23 +08:00
)
2019-08-10 18:26:43 +08:00
func RunWeb(template string, index string, static string, url string) http.Handler {
2019-08-07 13:16:23 +08:00
r := gin.New()
r.Use(gin.Recovery())
// 引入html资源
r.LoadHTMLGlob("web/" + template + "/*")
// 引入静态资源
r.Static("/static", "./web/"+static)
r.GET(url, func(c *gin.Context) {
2019-08-10 18:26:43 +08:00
c.HTML(http.StatusOK, index, gin.H{})
2019-08-07 13:16:23 +08:00
})
// API 启用状态
apiStatus := conf.Get("api", "status")
// 判断 API 是否启用
if apiStatus == "1" {
// 启动 WEB蜜罐 API
r.Use(cors.Cors())
webUrl := conf.Get("api", "web_url")
r.POST(webUrl, api.ReportWeb)
}
2019-08-07 13:16:23 +08:00
return r
}
2019-08-10 18:26:43 +08:00
func RunDeep(template string, index string, static string, url string) http.Handler {
r := gin.New()
r.Use(gin.Recovery())
// 引入html资源
r.LoadHTMLGlob("web/" + template + "/*")
// 引入静态资源
r.Static("/static", "./web/"+static)
r.GET(url, func(c *gin.Context) {
2019-08-10 18:26:43 +08:00
c.HTML(http.StatusOK, index, gin.H{})
})
// API 启用状态
apiStatus := conf.Get("api", "status")
// 判断 API 是否启用
if apiStatus == "1" {
// 启动 暗网蜜罐 API
r.Use(cors.Cors())
deepUrl := conf.Get("api", "deep_url")
r.POST(deepUrl, api.ReportDeepWeb)
}
return r
}
func RunPlug() http.Handler {
r := gin.New()
r.Use(gin.Recovery())
// API 启用状态
apiStatus := conf.Get("api", "status")
// 判断 API 是否启用
if apiStatus == "1" {
// 启动 蜜罐插件 API
r.Use(cors.Cors())
plugUrl := conf.Get("api", "plug_url")
r.POST(plugUrl, api.ReportPlugWeb)
}
return r
}
2019-08-07 13:16:23 +08:00
func RunAdmin() http.Handler {
gin.DisableConsoleColor()
2019-08-07 13:16:23 +08:00
f, _ := os.Create("./logs/hfish.log")
gin.DefaultWriter = io.MultiWriter(f)
2019-08-07 13:16:23 +08:00
// 引入gin
r := gin.Default()
r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf("[HFish] %s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format("2006-01-02 15:04:05"),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
2019-12-03 21:28:24 +08:00
store := cookie.NewStore([]byte("HFish"))
r.Use(sessions.Sessions("HFish", store))
2019-08-07 13:16:23 +08:00
r.Use(gin.Recovery())
2019-08-07 13:16:23 +08:00
// 引入html资源
r.LoadHTMLGlob("admin/*")
// 引入静态资源
r.Static("/static", "./static")
// 加载路由
view.LoadUrl(r)
return r
}
2019-12-03 21:28:24 +08:00
// 初始化缓存
func initCahe() {
resultMail, _ := dbUtil.DB().Table("hfish_setting").Fields("status", "info").Where("type", "=", "alertMail").First()
resultHook, _ := dbUtil.DB().Table("hfish_setting").Fields("status", "info").Where("type", "=", "webHook").First()
resultIp, _ := dbUtil.DB().Table("hfish_setting").Fields("status", "info").Where("type", "=", "whiteIp").First()
2020-02-24 19:50:43 +08:00
resultPasswd, _ := dbUtil.DB().Table("hfish_setting").Fields("status", "info").Where("type", "=", "passwdTM").First()
2019-12-03 21:28:24 +08:00
cache.Setx("MailConfigStatus", strconv.FormatInt(resultMail["status"].(int64), 10))
cache.Setx("MailConfigInfo", resultMail["info"])
cache.Setx("HookConfigStatus", strconv.FormatInt(resultHook["status"].(int64), 10))
cache.Setx("HookConfigInfo", resultHook["info"])
cache.Setx("IpConfigStatus", strconv.FormatInt(resultIp["status"].(int64), 10))
cache.Setx("IpConfigInfo", resultIp["info"])
2020-02-24 19:50:43 +08:00
cache.Setx("PasswdConfigStatus", strconv.FormatInt(resultPasswd["status"].(int64), 10))
cache.Setx("PasswdConfigInfo", resultPasswd["info"])
2019-12-03 21:28:24 +08:00
}
2019-08-07 13:16:23 +08:00
func Run() {
2020-04-12 22:19:13 +08:00
ping.Ping()
// 启动 自定义 蜜罐
custom.StartCustom()
2019-10-28 22:25:23 +08:00
// 启动 vnc 蜜罐
vncStatus := conf.Get("vnc", "status")
// 判断 vnc 蜜罐 是否开启
if vncStatus == "1" {
vncAddr := conf.Get("vnc", "addr")
go vnc.Start(vncAddr)
2020-02-24 19:50:43 +08:00
2019-10-28 22:25:23 +08:00
}
//=========================//
// 启动 elasticsearch 蜜罐
esStatus := conf.Get("elasticsearch", "status")
// 判断 elasticsearch 蜜罐 是否开启
if esStatus == "1" {
esAddr := conf.Get("elasticsearch", "addr")
go elasticsearch.Start(esAddr)
}
//=========================//
// 启动 TFTP 蜜罐
tftpStatus := conf.Get("tftp", "status")
// 判断 TFTP 蜜罐 是否开启
if tftpStatus == "1" {
tftpAddr := conf.Get("tftp", "addr")
go tftp.Start(tftpAddr)
}
//=========================//
2019-08-16 16:08:00 +08:00
// 启动 MemCache 蜜罐
memCacheStatus := conf.Get("mem_cache", "status")
// 判断 MemCache 蜜罐 是否开启
if memCacheStatus == "1" {
memCacheAddr := conf.Get("mem_cache", "addr")
2019-10-28 22:25:23 +08:00
go memcache.Start(memCacheAddr, "4")
}
2019-08-16 16:08:00 +08:00
//=========================//
2019-08-11 20:14:28 +08:00
// 启动 FTP 蜜罐
ftpStatus := conf.Get("ftp", "status")
2019-08-10 18:26:43 +08:00
2019-08-11 20:14:28 +08:00
// 判断 FTP 蜜罐 是否开启
if ftpStatus != "0" {
2019-08-11 20:14:28 +08:00
ftpAddr := conf.Get("ftp", "addr")
go ftp.Start(ftpAddr)
2019-08-10 18:26:43 +08:00
}
//=========================//
2019-08-11 20:14:28 +08:00
// 启动 Telnet 蜜罐
telnetStatus := conf.Get("telnet", "status")
2019-08-11 20:14:28 +08:00
// 判断 Telnet 蜜罐 是否开启
if telnetStatus != "0" {
2019-08-11 20:14:28 +08:00
telnetAddr := conf.Get("telnet", "addr")
go telnet.Start(telnetAddr)
}
//=========================//
2019-10-28 22:25:23 +08:00
// 启动 HTTP 正向代理
httpStatus := conf.Get("http", "status")
// 判断 HTTP 正向代理 是否开启
if httpStatus == "1" {
httpAddr := conf.Get("http", "addr")
go httpx.Start(httpAddr)
}
2019-08-11 20:14:28 +08:00
//=========================//
// 启动 Mysql 蜜罐
2019-08-07 13:16:23 +08:00
mysqlStatus := conf.Get("mysql", "status")
2019-08-11 20:14:28 +08:00
// 判断 Mysql 蜜罐 是否开启
if mysqlStatus != "0" {
2019-08-07 13:16:23 +08:00
mysqlAddr := conf.Get("mysql", "addr")
// 利用 Mysql 服务端 任意文件读取漏洞
mysqlFiles := conf.Get("mysql", "files")
go mysql.Start(mysqlAddr, mysqlFiles)
}
//=========================//
2019-08-11 20:14:28 +08:00
// 启动 Redis 蜜罐
2019-08-07 13:16:23 +08:00
redisStatus := conf.Get("redis", "status")
2019-08-11 20:14:28 +08:00
// 判断 Redis 蜜罐 是否开启
if redisStatus != "0" {
2019-08-07 13:16:23 +08:00
redisAddr := conf.Get("redis", "addr")
go redis.Start(redisAddr)
}
//=========================//
2019-08-11 20:14:28 +08:00
// 启动 SSH 蜜罐
2019-08-07 13:16:23 +08:00
sshStatus := conf.Get("ssh", "status")
2019-08-11 20:14:28 +08:00
// 判断 SSG 蜜罐 是否开启
if sshStatus != "0" {
2019-08-07 13:16:23 +08:00
sshAddr := conf.Get("ssh", "addr")
go ssh.Start(sshAddr)
}
//=========================//
2019-08-11 20:14:28 +08:00
// 启动 Web 蜜罐
2019-08-07 13:16:23 +08:00
webStatus := conf.Get("web", "status")
2019-08-11 20:14:28 +08:00
// 判断 Web 蜜罐 是否开启
if webStatus != "0" {
2019-08-07 13:16:23 +08:00
webAddr := conf.Get("web", "addr")
webTemplate := conf.Get("web", "template")
webStatic := conf.Get("web", "static")
webUrl := conf.Get("web", "url")
2019-08-10 18:26:43 +08:00
webIndex := conf.Get("web", "index")
2019-08-07 13:16:23 +08:00
serverWeb := &http.Server{
Addr: webAddr,
2019-08-10 18:26:43 +08:00
Handler: RunWeb(webTemplate, webIndex, webStatic, webUrl),
2019-08-07 13:16:23 +08:00
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
go serverWeb.ListenAndServe()
}
//=========================//
2019-08-11 20:14:28 +08:00
// 启动 暗网 蜜罐
2019-08-10 18:26:43 +08:00
deepStatus := conf.Get("deep", "status")
2019-08-11 20:14:28 +08:00
// 判断 暗网 Web 蜜罐 是否开启
if deepStatus != "0" {
2019-08-10 18:26:43 +08:00
deepAddr := conf.Get("deep", "addr")
deepTemplate := conf.Get("deep", "template")
deepStatic := conf.Get("deep", "static")
deepkUrl := conf.Get("deep", "url")
deepIndex := conf.Get("deep", "index")
serverDark := &http.Server{
2019-08-10 18:26:43 +08:00
Addr: deepAddr,
Handler: RunDeep(deepTemplate, deepIndex, deepStatic, deepkUrl),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
go serverDark.ListenAndServe()
}
//=========================//
// 启动 蜜罐插件
plugStatus := conf.Get("plug", "status")
// 判断 蜜罐插件 是否开启
if plugStatus != "0" {
plugAddr := conf.Get("plug", "addr")
serverPlug := &http.Server{
Addr: plugAddr,
Handler: RunPlug(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
go serverPlug.ListenAndServe()
}
//=========================//
2019-08-10 18:26:43 +08:00
// 启动 RPC
rpcStatus := conf.Get("rpc", "status")
// 判断 RPC 是否开启 1 RPC 服务端 2 RPC 客户端
if rpcStatus == "1" {
// 服务端监听地址
rpcAddr := conf.Get("rpc", "addr")
go server.Start(rpcAddr)
} else if rpcStatus == "2" {
// 客户端连接服务端
// 阻止进程,不启动 admin
2020-04-12 22:19:13 +08:00
rpcName := conf.Get("rpc", "name")
2020-02-24 19:50:43 +08:00
client.RpcInit()
2019-08-10 18:26:43 +08:00
for {
2020-04-12 22:19:13 +08:00
// 判断自定义蜜罐是否启动
customStatus := "0"
customNames := conf.GetCustomName()
if len(customNames) > 0 {
customStatus = "1"
}
2019-08-10 18:26:43 +08:00
// 这样写 提高IO读写性能
2020-04-12 22:19:13 +08:00
go client.Start(rpcName, ftpStatus, telnetStatus, httpStatus, mysqlStatus, redisStatus, sshStatus, webStatus, deepStatus, memCacheStatus, plugStatus, esStatus, tftpStatus, vncStatus, customStatus)
2019-08-10 18:26:43 +08:00
time.Sleep(time.Duration(1) * time.Minute)
}
}
//=========================//
2019-12-03 21:28:24 +08:00
// 初始化缓存
initCahe()
2019-08-10 18:26:43 +08:00
2019-08-07 13:16:23 +08:00
// 启动 admin 管理后台
adminAddr := conf.Get("admin", "addr")
2019-08-07 13:16:23 +08:00
serverAdmin := &http.Server{
Addr: adminAddr,
2019-08-07 13:16:23 +08:00
Handler: RunAdmin(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
2020-02-24 19:50:43 +08:00
fmt.Printf("pid is %d", syscall.Getpid())
2019-08-07 13:16:23 +08:00
serverAdmin.ListenAndServe()
}
func Init() {
fmt.Println("test")
}
func Help() {
exec.Execute("clear")
logo := ` o
\_/\o
( Oo) \|/
(_=-) .===O- ~~~b~i~u~~ -O-
/ \_/U' /|\
|| |_/
\\ | ~ By: HackLC Team
{K || __ _______ __
| PP / // / __(_)__ / /
| || / _ / _// (_-</ _ \
2020-04-15 18:09:54 +08:00
(__\\ /_//_/_/ /_/___/_//_/ v0.6.1
2019-08-07 13:16:23 +08:00
`
fmt.Println(color.Yellow(logo))
fmt.Println(color.White(" A Safe and Active Attack Honeypot Fishing Framework System for Enterprises."))
fmt.Println("")
fmt.Println(color.Yellow(" + [ ABOUT ]----------------------------------------------------------- +"))
fmt.Println("")
2019-08-07 15:20:41 +08:00
fmt.Println(color.Green(" - Github:"), color.White("https://github.com/hacklcx/HFish"), color.Green(" - Team:"), color.White("https://hack.lc"))
2019-08-07 13:16:23 +08:00
fmt.Println("")
fmt.Println(color.Yellow(" + [ ARGUMENTS ]------------------------------------------------------- +"))
fmt.Println("")
fmt.Println(color.Cyan(" run,--run"), color.White(" Start up service"))
//fmt.Println(color.Cyan(" init,--init"), color.White(" Initialization, Wipe data"))
fmt.Println(color.Cyan(" version,--version"), color.White(" HFish Version"))
fmt.Println(color.Cyan(" help,--help"), color.White(" Help"))
fmt.Println("")
fmt.Println(color.Yellow(" + -------------------------------------------------------------------- +"))
fmt.Println("")
}