Skip to content

Commit

Permalink
Security tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sardelka9515 committed Aug 18, 2022
1 parent be13e0d commit 7566423
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 21 deletions.
10 changes: 4 additions & 6 deletions RageCoop.Client/Menus/CoopMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion RageCoop.Client/Menus/Sub/ServersMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 19 additions & 6 deletions RageCoop.Client/Networking/Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion RageCoop.Client/Networking/Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static void ProcessMessage(NetIncomingMessage message)
}
case PacketType.PublicKeyResponse:
{

if(Security.ServerRSA!=null){break;}
var packet = data.GetPacket<Packets.PublicKeyResponse>();
Security.SetServerPublicKey(packet.Modulus, packet.Exponent);
_publicKeyReceived.Set();
Expand Down
4 changes: 2 additions & 2 deletions RageCoop.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" )]

2 changes: 2 additions & 0 deletions RageCoop.Client/Util/WeaponUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ public static WeaponGroup GetWeaponGroup(this WeaponHash hash)
return Function.Call<WeaponGroup>(Hash.GET_WEAPONTYPE_GROUP, hash);
}
}
/*
class WeaponInfo
{
public string Name;
Expand All @@ -545,4 +546,5 @@ public class AimingInfo
public float SweepPitchMin;
public float SweepPitchMax;
}
*/
}
22 changes: 22 additions & 0 deletions RageCoop.Core/Networking/PublicKey.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions RageCoop.Core/Networking/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

}
}
4 changes: 2 additions & 2 deletions RageCoop.Server/Networking/Server.Connections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion RageCoop.Server/Networking/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions RageCoop.Server/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" )]

0 comments on commit 7566423

Please sign in to comment.