Skip to content

Commit

Permalink
fix: CC unable to detect existing instance
Browse files Browse the repository at this point in the history
this commit also refactors `cc.main`, improves its readability and structure
  • Loading branch information
jm33-m0 committed Jan 19, 2024
1 parent bbd57f9 commit 15e2940
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
54 changes: 34 additions & 20 deletions core/cmd/cc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,13 @@ func init_magic_str() {
}

func main() {
// abort if CC is already running
if cc.IsCCRunning() {
cc.CliFatalError("CC is already running")
}

// set up dirs
err := cc.DirSetup()
// set up dirs and default varaibles
// including config file location
err := cc.InitConfig()
if err != nil {
cc.CliFatalError("DirSetup: %v", err)
}

// set up magic string
init_magic_str()

cdnproxy := flag.String("cdn2proxy", "", "Start cdn2proxy server on this port")
config := flag.String("config", cc.EmpConfigFile, "Use this config file to update hardcoded variables")
names := flag.String("gencert", "", "Generate C2 server cert with these host names")
Expand All @@ -75,6 +68,16 @@ func main() {
relayed_port := flag.Int("relayed_port", 0, "Relayed port, use with -connect_relay")
flag.Parse()

// read config file
err = readJSONConfig(*config)
if err != nil {
cc.CliFatalError("Failed to read config from '%s': %v", *config, err)
}

// set up magic string
init_magic_str()

// generate C2 TLS cert for given host names
if *names != "" {
hosts := strings.Fields(*names)
err := cc.GenC2Certs(hosts)
Expand All @@ -88,11 +91,14 @@ func main() {
os.Exit(0)
}

// read config file
err = readJSONConfig(*config)
if err != nil {
cc.CliFatalError("Read %s: %v", *config, err)
} else if *ssh_relay_port != "" {
// abort if CC is already running
if cc.IsCCRunning() {
cc.CliFatalError("CC is already running")
}

// run as relay server
// no need to start CC services
if *ssh_relay_port != "" {
cc.CliMsg("Copy ~/.emp3r0r to client host, "+
"then run `emp3r0r -connect_relay relay_ip:%s -relayed_port %s` "+
"(C2 port, or Shadowsocks port %s if you are using it)",
Expand All @@ -104,15 +110,13 @@ func main() {
cc.CliFatalError("SSHRemoteFwdServer: %v", err)
}
} else {
// unlock downloads
err = cc.UnlockDownloads()
if err != nil {
cc.CliPrintWarning("UnlockDownloads: %v", err)
}
// run as CC
go cc.TLSServer()
go cc.ShadowsocksServer()
go cc.InitModules()
}

// run as relay client
if *connect_relay_addr != "" {
if *relayed_port == 0 {
cc.CliFatalError("Please specify -relayed_port")
Expand Down Expand Up @@ -140,6 +144,7 @@ func main() {
}()
}

// start cdn2proxy server
if *cdnproxy != "" {
go func() {
logFile, err := os.OpenFile("/tmp/ws.log", os.O_CREATE|os.O_RDWR, 0600)
Expand All @@ -153,14 +158,23 @@ func main() {
}()
}

// print banner
err = cc.CliBanner()
if err != nil {
cc.CliFatalError("Banner: %v", err)
}

// unlock incomplete downloads
err = cc.UnlockDownloads()
if err != nil {
cc.CliPrintWarning("UnlockDownloads: %v", err)
}

// use emp3r0r in terminal or from other frontend
if *apiserver {
go cc.APIMain()
}

// run CLI
cc.CliMain()
}
4 changes: 2 additions & 2 deletions core/lib/cc/cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ func Send2Agent(data *emp3r0r_data.MsgTunData, agent *emp3r0r_data.AgentSystemIn
return
}

// DirSetup set workspace, module directories, etc
func DirSetup() (err error) {
// InitConfig set workspace, module directories, etc
func InitConfig() (err error) {
// prefix
Prefix = os.Getenv("EMP3R0R_PREFIX")
if Prefix == "" {
Expand Down
6 changes: 2 additions & 4 deletions core/lib/cc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,7 @@ func GetDateTime() (datetime string) {
// IsCCRunning check if CC is already running
func IsCCRunning() bool {
// it is running if we can connect to it
if tun.IsPortOpen("127.0.0.1", RuntimeConfig.CCPort) {
return true
}
return false
return tun.IsPortOpen("127.0.0.1", RuntimeConfig.CCPort)
}

// UnlockDownloads if there are incomplete file downloads that are "locked", unlock them
Expand All @@ -285,6 +282,7 @@ func UnlockDownloads() error {
for _, f := range files {
if strings.HasSuffix(f.Name(), ".lock") {
err = os.Remove(FileGetDir + f.Name())
CliPrintDebug("Unlocking download: %s", f.Name())
if err != nil {
return fmt.Errorf("Remove %s: %v", f.Name(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/tun/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

// IsPortOpen is this TCP port open?
func IsPortOpen(host string, port string) bool {
timeout := time.Second
timeout := 3 * time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err != nil {
return false
Expand Down

0 comments on commit 15e2940

Please sign in to comment.