From 7566423889b1dd5769fd7b6dc8d42fa3aaed8640 Mon Sep 17 00:00:00 2001 From: Sardelka9515 Date: Thu, 18 Aug 2022 17:45:08 +0800 Subject: [PATCH] Security tweaks --- RageCoop.Client/Menus/CoopMenu.cs | 10 +++----- RageCoop.Client/Menus/Sub/ServersMenu.cs | 2 +- RageCoop.Client/Networking/Networking.cs | 25 ++++++++++++++----- RageCoop.Client/Networking/Receive.cs | 2 +- RageCoop.Client/Properties/AssemblyInfo.cs | 4 +-- RageCoop.Client/Util/WeaponUtil.cs | 2 ++ RageCoop.Core/Networking/PublicKey.cs | 22 ++++++++++++++++ RageCoop.Core/Networking/ServerInfo.cs | 3 +++ .../Networking/Server.Connections.cs | 4 +-- RageCoop.Server/Networking/Server.cs | 5 +++- RageCoop.Server/Properties/AssemblyInfo.cs | 4 +-- 11 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 RageCoop.Core/Networking/PublicKey.cs diff --git a/RageCoop.Client/Menus/CoopMenu.cs b/RageCoop.Client/Menus/CoopMenu.cs index 71e5220e..cc7569d9 100644 --- a/RageCoop.Client/Menus/CoopMenu.cs +++ b/RageCoop.Client/Menus/CoopMenu.cs @@ -91,6 +91,7 @@ public static bool ShowPopUp(string prompt, string title, string subtitle, strin PopUp.Error=error; PopUp.ShowBackground=showbackground; PopUp.Visible=true; + Script.Yield(); while (true) { Game.DisableAllControlsThisFrame(); @@ -123,12 +124,9 @@ public static void UsernameActivated(object a, System.EventArgs b) private static void _passwordActivated(object sender, System.EventArgs e) { string newPass = Game.GetUserInput(WindowTitle.EnterMessage20, "", 20); - if (!string.IsNullOrWhiteSpace(newPass)) - { - Main.Settings.Password = newPass; - Util.SaveSettings(); - _passwordItem.AltTitle = new string('*', newPass.Length); - } + Main.Settings.Password = newPass; + Util.SaveSettings(); + _passwordItem.AltTitle = new string('*', newPass.Length); } public static void ServerIpActivated(object a, System.EventArgs b) { diff --git a/RageCoop.Client/Menus/Sub/ServersMenu.cs b/RageCoop.Client/Menus/Sub/ServersMenu.cs index b7cab4e5..e6197465 100644 --- a/RageCoop.Client/Menus/Sub/ServersMenu.cs +++ b/RageCoop.Client/Menus/Sub/ServersMenu.cs @@ -91,7 +91,7 @@ private static void GetAllServers() throw new Exception("Failed to obtain ZeroTier network IP"); } } - Networking.ToggleConnection(address); + Networking.ToggleConnection(address,null,null,PublicKey.FromServerInfo(server)); #if !NON_INTERACTIVE CoopMenu.ServerIpItem.AltTitle = address; diff --git a/RageCoop.Client/Networking/Networking.cs b/RageCoop.Client/Networking/Networking.cs index 60bc98eb..7201ac47 100644 --- a/RageCoop.Client/Networking/Networking.cs +++ b/RageCoop.Client/Networking/Networking.cs @@ -6,6 +6,8 @@ using System.Threading; using System.Threading.Tasks; using GTA.UI; +using System.Net; + namespace RageCoop.Client { internal static partial class Networking @@ -24,8 +26,9 @@ static Networking() Security=new Security(Main.Logger); } - public static void ToggleConnection(string address, string username = null, string password = null) + public static void ToggleConnection(string address, string username = null, string password = null,PublicKey publicKey=null) { + Menus.CoopMenu.Menu.Visible=false; Peer?.Shutdown("Bye"); if (IsOnServer) { @@ -74,6 +77,11 @@ public static void ToggleConnection(string address, string username = null, stri PlayerList.Cleanup(); EntityPool.AddPlayer(); + if (publicKey==null && !string.IsNullOrEmpty(password) && !Menus.CoopMenu.ShowPopUp("WARNING", "WARNING", "Server's IP can be spoofed when using direct connection, do you wish to continue?", "", true)) + { + IsConnecting=false; + return; + } Task.Run(() => { try @@ -88,13 +96,18 @@ public static void ToggleConnection(string address, string username = null, stri Main.QueueAction(() => { Notification.Show($"~y~Trying to connect..."); }); Menus.CoopMenu._serverConnectItem.Enabled=false; Security.Regen(); - if (!GetServerPublicKey(ip[0],int.Parse(ip[1]))) - { - Menus.CoopMenu._serverConnectItem.Enabled=true; - throw new TimeoutException("Failed to retrive server's public key"); + if(publicKey==null){ + if (!GetServerPublicKey(ip[0],int.Parse(ip[1]))) + { + Menus.CoopMenu._serverConnectItem.Enabled=true; + throw new TimeoutException("Failed to retrive server's public key"); + } + } + else{ + Security.SetServerPublicKey(publicKey.Modulus,publicKey.Exponent); } - // Send HandshakePacket + // Send handshake packet NetOutgoingMessage outgoingMessage = Peer.CreateMessage(); var handshake = new Packets.Handshake() { diff --git a/RageCoop.Client/Networking/Receive.cs b/RageCoop.Client/Networking/Receive.cs index 11c88d98..6c4efdf9 100644 --- a/RageCoop.Client/Networking/Receive.cs +++ b/RageCoop.Client/Networking/Receive.cs @@ -197,7 +197,7 @@ public static void ProcessMessage(NetIncomingMessage message) } case PacketType.PublicKeyResponse: { - + if(Security.ServerRSA!=null){break;} var packet = data.GetPacket(); Security.SetServerPublicKey(packet.Modulus, packet.Exponent); _publicKeyReceived.Set(); diff --git a/RageCoop.Client/Properties/AssemblyInfo.cs b/RageCoop.Client/Properties/AssemblyInfo.cs index 39941440..b52bc2c7 100644 --- a/RageCoop.Client/Properties/AssemblyInfo.cs +++ b/RageCoop.Client/Properties/AssemblyInfo.cs @@ -16,7 +16,7 @@ // Version informationr( -[assembly: AssemblyVersion("1.5.0.13")] -[assembly: AssemblyFileVersion("1.5.0.13")] +[assembly: AssemblyVersion("1.5.1.13")] +[assembly: AssemblyFileVersion("1.5.1.13")] [assembly: NeutralResourcesLanguageAttribute( "en-US" )] diff --git a/RageCoop.Client/Util/WeaponUtil.cs b/RageCoop.Client/Util/WeaponUtil.cs index 974f119a..af9e1cc7 100644 --- a/RageCoop.Client/Util/WeaponUtil.cs +++ b/RageCoop.Client/Util/WeaponUtil.cs @@ -533,6 +533,7 @@ public static WeaponGroup GetWeaponGroup(this WeaponHash hash) return Function.Call(Hash.GET_WEAPONTYPE_GROUP, hash); } } + /* class WeaponInfo { public string Name; @@ -545,4 +546,5 @@ public class AimingInfo public float SweepPitchMin; public float SweepPitchMax; } + */ } diff --git a/RageCoop.Core/Networking/PublicKey.cs b/RageCoop.Core/Networking/PublicKey.cs new file mode 100644 index 00000000..449c60d7 --- /dev/null +++ b/RageCoop.Core/Networking/PublicKey.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Lidgren.Network; +using System.Threading; + +namespace RageCoop.Core +{ + internal class PublicKey{ + public PublicKey(){ + + } + public static PublicKey FromServerInfo(ServerInfo info){ + return new PublicKey{ + Modulus=Convert.FromBase64String(info.publicKeyModulus), + Exponent=Convert.FromBase64String(info.publicKeyExponent) + }; + } + public byte[] Modulus; + public byte[] Exponent; + } +} diff --git a/RageCoop.Core/Networking/ServerInfo.cs b/RageCoop.Core/Networking/ServerInfo.cs index 03d4514f..9a2ba42c 100644 --- a/RageCoop.Core/Networking/ServerInfo.cs +++ b/RageCoop.Core/Networking/ServerInfo.cs @@ -27,5 +27,8 @@ internal class ServerInfo public string ztID { get; set; } public string ztAddress { get; set; } + public string publicKeyModulus{get;set;} + public string publicKeyExponent{get;set;} + } } diff --git a/RageCoop.Server/Networking/Server.Connections.cs b/RageCoop.Server/Networking/Server.Connections.cs index 7ed8e5f3..4cc82b4a 100644 --- a/RageCoop.Server/Networking/Server.Connections.cs +++ b/RageCoop.Server/Networking/Server.Connections.cs @@ -23,9 +23,9 @@ private void DisconnectAndLog(NetConnection senderConnection, PacketType type, E private void GetHandshake(NetConnection connection, Packets.Handshake packet) { Logger?.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + connection.RemoteEndPoint.Address.ToString() + "]"); - if (!packet.ModVersion.StartsWith(Version.ToString(2))) + if (!packet.ModVersion.StartsWith(Version.ToString(3))) { - connection.Deny($"RAGECOOP version {Version.ToString(2)} required!"); + connection.Deny($"RAGECOOP version {Version.ToString(3)} required!"); return; } if (string.IsNullOrWhiteSpace(packet.Username)) diff --git a/RageCoop.Server/Networking/Server.cs b/RageCoop.Server/Networking/Server.cs index d310c305..818a450a 100644 --- a/RageCoop.Server/Networking/Server.cs +++ b/RageCoop.Server/Networking/Server.cs @@ -132,6 +132,7 @@ public Server(Settings settings,Logger logger=null) HttpResponseMessage response = null; try { + Security.GetPublicKey(out var pModulus,out var pExpoenet); var serverInfo = new ServerInfo { address = info.Address, @@ -149,6 +150,8 @@ public Server(Settings settings,Logger logger=null) useZT=Settings.UseZeroTier, ztID=Settings.UseZeroTier ? Settings.ZeroTierNetworkID : "", ztAddress=Settings.UseZeroTier ? ZeroTierHelper.Networks[Settings.ZeroTierNetworkID].Addresses.Where(x => !x.Contains(":")).First() : "0.0.0.0", + publicKeyModulus=Convert.ToBase64String(pModulus), + publicKeyExponent=Convert.ToBase64String(pExpoenet) }; string msg = JsonConvert.SerializeObject(serverInfo); @@ -216,7 +219,7 @@ public void Start() Logger?.Info("================"); Logger?.Info($"Server bound to: 0.0.0.0:{Settings.Port}"); Logger?.Info($"Server version: {Version}"); - Logger?.Info($"Compatible RAGECOOP versions: {Version.ToString(2)}"); + Logger?.Info($"Compatible RAGECOOP versions: {Version.ToString(3)}"); Logger?.Info("================"); if (Settings.UseZeroTier) diff --git a/RageCoop.Server/Properties/AssemblyInfo.cs b/RageCoop.Server/Properties/AssemblyInfo.cs index 3bdd3263..0508d162 100644 --- a/RageCoop.Server/Properties/AssemblyInfo.cs +++ b/RageCoop.Server/Properties/AssemblyInfo.cs @@ -15,7 +15,7 @@ [assembly: AssemblyCulture("")] // Version informationr( -[assembly: AssemblyVersion("1.5.0.13")] -[assembly: AssemblyFileVersion("1.5.0.13")] +[assembly: AssemblyVersion("1.5.1.13")] +[assembly: AssemblyFileVersion("1.5.1.13")] [assembly: NeutralResourcesLanguageAttribute( "en-US" )]