0

我正在编写一个客户端-服务器应用程序,用于计算机实验室并充当服务(不作为服务运行)。我有一个控制台应用程序使用控制台的 HWND 对象调用本机函数“ShowWindow”/SW_HIDE——这给了它我想要的东西。服务器/客户端正在工作,我已发送消息“Hello world!” 从客户端到服务器多次,我很高兴。(我将 UDP 用于套接字协议,因为 IT 部门想要一种无连接的方法。)

我的问题在于客户端-服务器之间通信的“协议”。

服务器背后的目标包括以下内容:

  • 以编程方式授予我们 IT 部门出于安全考虑已阻止的某些功能的访问权限(例如“net.exe”)
  • 允许访问我的程序以监控学生在计算机实验室中查看的内容。

我想包括的一些事情是来回发送的简单问题:

  • “REQUSER”命令将返回用户名和全名(之前“net user”允许)
  • “REQPROCS”命令将返回当前在当前用户的用户名下运行的进程列表。

我毫不怀疑我能够做到这一点。我目前最担心的是数据安全。在我的大学里,我们确实有一些“黑客”,他们可能知道如何嗅探数据包并能够将数据包重新发送到特定服务器,以进行恶意操作或获取有关敌人的信息或诸如此类的信息。

我的想法是为所有发送的数据提供加密方案,并在接收时对其进行解码。

一位与我交谈过的朋友说我应该使用 bit packer,我开始将他的 BitPacker 类从 C++ 移植到 C#,我对此感到困惑并来到这里看看 Stackoverflow 的想法。

namespace Atlantis.Net.Sockets
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Windows.Forms;

    public class UdpServer : System.Net.Sockets.UdpClient
    {

        #region Constructor(s)

        public UdpServer(Int32 port)
            : base(port)
        {
        }

        public UdpServer(IPEndPoint endPoint)
            : base(endPoint)
        {
        }

        #endregion

        #region Properties

        private Int32 m_Backlog = 32;
        /// <summary>
        ///     Sets how many active connections the socket can support
        /// </summary>
        public Int32 Backlog
        {
            private get
            {
                return m_Backlog;
            }
            set
            {
                m_Backlog = value;
            }
        }

        private Boolean m_IsInitialized = false;
        /// <summary>
        ///     Gets a value indicating whether the server has been initialized
        /// </summary>
        public Boolean IsInitialized
        {
            get
            {
                return m_IsInitialized;
            }
            private set
            {
                m_IsInitialized = value;
            }
        }

        private Int32 m_Port = 1337;
        /// <summary>
        ///     Sets the port number for listening for incoming connections
        /// </summary>
        public Int32 Port
        {
            private get
            {
                return m_Port;
            }
            set
            {
                m_Port = value;
            }
        }

        private Encoding m_Encoding = Encoding.ASCII;
        /// <summary>
        ///     Gets or sets the text encoding for data being transferred to/from the server
        /// </summary>
        public Encoding Encoding
        {
            get
            {
                return m_Encoding;
            }
            set
            {
                m_Encoding = value;
            }
        }

        #endregion

        #region Events

        public event EventHandler<UdpReceiveEventArgs> DataReceive;

        #endregion

        #region Methods

        protected virtual void OnDataRecieve(String data, object state)
        {
            if (DataReceive != null)
            {
                DataReceive(this, new UdpReceiveEventArgs(data, ((UdpState)state)));
            }
        }

        private void DataReceiveCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)((UdpState)ar.AsyncState).host;
            IPEndPoint e = (IPEndPoint)((UdpState)ar.AsyncState).endPoint;

            Byte[] data = u.EndReceive(ar, ref e);

            OnDataRecieve(Encoding.GetString(data), ((UdpState)ar.AsyncState));

            UdpState state = new UdpState();
            state.endPoint = new IPEndPoint(IPAddress.Any, Port);
            state.host = u;
            u.BeginReceive(new AsyncCallback(DataReceiveCallback), ((UdpState)ar.AsyncState));
        }

        /// <summary>
        ///     .
        /// </summary>
        public void Initialize()
        {
            if (IsInitialized)
            {
                return;
            }
            //Debug.WriteLine(String.Format("Local address and port : {0}", Client.RemoteEndPoint.ToString()));

            UdpState state = new UdpState();
            state.endPoint = new IPEndPoint(IPAddress.Any, Port);
            state.host = this;
            BeginReceive(new AsyncCallback(DataReceiveCallback), state);

            IsInitialized = true;
        }

        #endregion

    }
}

PS我希望问题很清楚?我注意到我写的大部分问题都不清楚。:/

4

1 回答 1

2

System.Net.Security 中的 SslStream 类可能会满足您的需要

(SslStream) 提供用于客户端-服务器通信的流,该流使用安全套接字层 (SSL) 安全协议来验证服务器和可选的客户端。

...

SSL 协议有助于为使用 SslStream 传输的消息提供机密性和完整性检查。在客户端和服务器之间传递敏感信息时,应使用 SSL 连接,例如 SslStream 提供的连接。使用 SslStream 有助于防止任何人在信息在网络上传输时读取和篡改信息

更多细节和例子在这里:SslStream 类

于 2009-11-13T21:47:19.113 回答