arnaucube
arnaucube/flock-botnetPublic Self-hosted repository
streaming keywords implemented, replying from the flock (botnet) to tweets with text generated with Markov chains
arnaucode idoctnef@openmailbox.org
Sun, 23 Apr 2017 19:01:28 +0200
commit

938db1f2cf25ebbd890ccbf74cfb75ae3bb8973c

parent

feb90306ebf6793fdc9767656ec60daa9a7a245a

7 files changed, 215 insertions(+), 76 deletions(-)

jump to
M README.md → README.md

@@ -1,1 +1,1 @@

-# twFlock+# projectFlock
A flock.go

@@ -0,0 +1,47 @@

+package main + +import ( + "fmt" + "strconv" + + "github.com/dghubble/go-twitter/twitter" +) + +func replyTweet(client *twitter.Client, text string, inReplyToStatusID int64) { + tweet, httpResp, err := client.Statuses.Update(text, &twitter.StatusUpdateParams{ + InReplyToStatusID: inReplyToStatusID, + }) + if err != nil { + fmt.Println(err) + } + if httpResp.Status != "200 OK" { + c.Red("error: " + httpResp.Status) + c.Purple("maybe twitter has blocked the account, CTRL+C, wait 15 minutes and try again") + } + fmt.Print("tweet posted: ") + c.Green(tweet.Text) +} + +func postTweet(client *twitter.Client, text string) { + tweet, httpResp, err := client.Statuses.Update(text, nil) + if err != nil { + fmt.Println(err) + } + if httpResp.Status != "200 OK" { + c.Red("error: " + httpResp.Status) + c.Purple("maybe twitter has blocked the account, CTRL+C, wait 15 minutes and try again") + } + fmt.Print("tweet posted: ") + c.Green(tweet.Text) +} +func tweetFromFlock(flock Flock, text string) { + fmt.Println("Starting to publish tweet: " + text) + fmt.Println(strconv.Itoa(len(flock.Clients))) + for i := 0; i < len(flock.Clients); i++ { + fmt.Print("tweeting from: ") + c.Cyan("@" + flock.ScreenNames[i]) + postTweet(flock.Clients[i], text) + //waitTime(1) + } + +}
M main.go → main.go

@@ -10,26 +10,22 @@

const version = "0.1-dev" func main() { - c.Yellow("twFlock") + c.Yellow("projectFlock") fmt.Println("---------------") - c.Cyan("twFlock initialized") - c.Purple("https://github.com/arnaucode/twFlock") + c.Cyan("projectFlock initialized") + c.Purple("https://github.com/arnaucode/projectFlock") fmt.Println("version " + version) fmt.Println("Reading flockConfig.json file") flock := readConfigTokensAndConnect() - c.Yellow("generating markov chains (may take some seconds)") - text, _ := readTxt("text.txt") - states := markov.train(text) - c.Green("markov chains generated") - //var flock Flock fmt.Println("---------------") newcommand := bufio.NewReader(os.Stdin) fmt.Print("Please select command number") options := ` - 1 - Tweet from flock of bots - 2 - Markov + 1 - Manual tweet from flock of bots + 2 - Markov manual text generator + 3 - Markov's Flock Botnet 0 - Exit script option to select: ` for {

@@ -40,12 +36,21 @@ option = strings.TrimSpace(option)

switch option { case "1": - fmt.Println("selected 1 - Tweet from flock of bots") - optionTweetFromFlock(flock) + fmt.Println("selected 1 - Manual tweet from flock of bots") + optionManualTweetFromFlock(flock) break case "2": - fmt.Println("selected 2 - Markov") + fmt.Println("selected 2 - Markov manual text generator") + c.Yellow("generating markov chains (may take some seconds)") + text, _ := readTxt("text.txt") + states := markov.train(text) + c.Green("markov chains generated") + optionTweetMarkov(states) + break + case "3": + fmt.Println("selected 3 - Markov Flock Botnet") + optionMarkovFlockBotnet(flock) break case "0": fmt.Println("selected 0 - exit script")
M markov.go → markov.go

@@ -26,7 +26,7 @@ for i := 0; i < tantPerFourty; i++ {

bar = append(bar, "█") } progressBar := strings.Join(bar, "") - fmt.Printf("\r " + progressBar + " - " + strconv.Itoa(tantPerCent) + "%") + fmt.Printf("\r " + progressBar + " - " + strconv.Itoa(tantPerCent) + "") } func addWordToStates(states []State, word string) ([]State, int) {

@@ -70,7 +70,7 @@ for j := 0; j < len(states[i].NextStates); j++ {

states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(len(words)) * 100) } } - fmt.Println("total words computed: " + strconv.Itoa(len(words))) + fmt.Println("\ntotal words computed: " + strconv.Itoa(len(words))) //fmt.Println(states) return states }

@@ -116,9 +116,12 @@ word := initWord

generatedText = append(generatedText, word) for i := 0; i < count; i++ { word = getNextMarkovState(states, word) + if word == "word no exist on the memory" { + return "word no exist on the memory" + } generatedText = append(generatedText, word) } - generatedText = append(generatedText, ".") + //generatedText = append(generatedText, ".") text := strings.Join(generatedText, " ") return text }
A optionManualTweetFromFlock.go

@@ -0,0 +1,32 @@

+package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func optionManualTweetFromFlock(flock Flock) { + + fmt.Print("entry tweet content: ") + newcommand := bufio.NewReader(os.Stdin) + text, _ := newcommand.ReadString('\n') + text = strings.TrimSpace(text) + fmt.Print("tweet content: ") + c.Purple(text) + + c.Red("Are you sure? [y/n]") + newcommand = bufio.NewReader(os.Stdin) + answer, _ := newcommand.ReadString('\n') + answer = strings.TrimSpace(answer) + switch answer { + case "y": + fmt.Println("ok, you are sure") + tweetFromFlock(flock, text) + break + default: + fmt.Println("Operation cancelled") + break + } +}
A optionMarkovFlockBotnet.go

@@ -0,0 +1,111 @@

+package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" + + "github.com/dghubble/go-twitter/twitter" +) + +func isRT(text string) bool { + tweetWords := strings.Split(text, " ") + for i := 0; i < len(tweetWords); i++ { + if tweetWords[i] == "RT" { + c.Red(text) + return true + } + } + return false +} +func generateMarkovResponse(states []State, word string) string { + generatedText := markov.generateText(states, word, 15) + return generatedText +} +func processTweet(states []State, flockUser *twitter.Client, botScreenName string, keywords []string, tweet *twitter.Tweet) { + c.Yellow("bot @" + botScreenName + " - New tweet detected:") + fmt.Println(tweet.Text) + + tweetWords := strings.Split(tweet.Text, " ") + generatedText := "word no exist on the memory" + for i := 0; i < len(tweetWords) && generatedText == "word no exist on the memory"; i++ { + fmt.Println(strconv.Itoa(i) + " - " + tweetWords[i]) + generatedText = generateMarkovResponse(states, tweetWords[i]) + } + c.Yellow("bot @" + botScreenName + " posting response") + fmt.Println(tweet.ID) + replyTweet(flockUser, "@"+tweet.User.ScreenName+" "+generatedText, tweet.ID) + waitTime(1) +} +func startStreaming(states []State, flockUser *twitter.Client, botScreenName string, keywords []string) { + // Convenience Demux demultiplexed stream messages + demux := twitter.NewSwitchDemux() + demux.Tweet = func(tweet *twitter.Tweet) { + if isRT(tweet.Text) == false { + processTweet(states, flockUser, botScreenName, keywords, tweet) + } + } + demux.DM = func(dm *twitter.DirectMessage) { + fmt.Println(dm.SenderID) + } + demux.Event = func(event *twitter.Event) { + fmt.Printf("%#v\n", event) + } + + fmt.Println("Starting Stream...") + // FILTER + filterParams := &twitter.StreamFilterParams{ + Track: keywords, + StallWarnings: twitter.Bool(true), + } + stream, err := flockUser.Streams.Filter(filterParams) + if err != nil { + log.Fatal(err) + } + // Receive messages until stopped or stream quits + demux.HandleChan(stream.Messages) + + // Wait for SIGINT and SIGTERM (HIT CTRL-C) + /*ch := make(chan os.Signal) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + log.Println(<-ch) + + fmt.Println("Stopping Stream...") + stream.Stop()*/ +} +func optionMarkovFlockBotnet(flock Flock) { + c.Green("Starting Markov's Flock botnet") + fmt.Println("generating Markov chains") + inputText, _ := readTxt("text.txt") + states := markov.train(inputText) + + //getting the keywords + c.Purple("entry words to stream tweets (separated by comma): ") + newcommand := bufio.NewReader(os.Stdin) + text, _ := newcommand.ReadString('\n') + text = strings.TrimSpace(text) + text = strings.Replace(text, " ", "", -1) + keywords := strings.Split(text, ",") + c.Purple("total keywords: " + strconv.Itoa(len(keywords))) + fmt.Print("keywords to follow: ") + fmt.Println(keywords) + + c.Red("Are you sure? [y/n]") + newcommand = bufio.NewReader(os.Stdin) + answer, _ := newcommand.ReadString('\n') + answer = strings.TrimSpace(answer) + switch answer { + case "y": + fmt.Println("ok, you are sure") + for i := 0; i < len(flock.Clients); i++ { + go startStreaming(states, flock.Clients[i], flock.ScreenNames[i], keywords) + } + break + default: + fmt.Println("Operation cancelled") + break + } +}
D tweetFromFlock.go

@@ -1,59 +0,0 @@

-package main - -import ( - "bufio" - "fmt" - "os" - "strconv" - "strings" - - "github.com/dghubble/go-twitter/twitter" -) - -func postTweet(client *twitter.Client, text string) { - tweet, httpResp, err := client.Statuses.Update(text, nil) - if err != nil { - fmt.Println(err) - } - if httpResp.Status != "200 OK" { - c.Red("error: " + httpResp.Status) - c.Purple("maybe twitter has blocked the account, CTRL+C, wait 15 minutes and try again") - } - fmt.Print("tweet posted: ") - c.Green(tweet.Text) -} -func tweetFromFlock(flock Flock, text string) { - fmt.Println("Starting to publish tweet: " + text) - fmt.Println(strconv.Itoa(len(flock.Clients))) - for i := 0; i < len(flock.Clients); i++ { - fmt.Print("tweeting from: ") - c.Cyan("@" + flock.ScreenNames[i]) - postTweet(flock.Clients[i], text) - //waitTime(1) - } - -} - -func optionTweetFromFlock(flock Flock) { - - fmt.Print("entry tweet content: ") - newcommand := bufio.NewReader(os.Stdin) - text, _ := newcommand.ReadString('\n') - text = strings.TrimSpace(text) - fmt.Print("tweet content: ") - c.Purple(text) - - c.Red("Are you sure? [y/n]") - newcommand = bufio.NewReader(os.Stdin) - answer, _ := newcommand.ReadString('\n') - answer = strings.TrimSpace(answer) - switch answer { - case "y": - fmt.Println("ok, you are sure") - tweetFromFlock(flock, text) - break - default: - fmt.Println("Operation cancelled") - break - } -}