private RequestUserDataResponsePackage(SerializationInfo info, StreamingContext context) { UserData = info.GetValue("User", typeof(User)) as User; Success = info.GetBoolean("Success"); }
public bool SetUser(string username, User user) { if (UserExists(username)) { UserDictionary[username] = user; return true; } return false; }
public static void Main(string[] args) { // Create a client session manager and allow sessions to remain valid for up to 5 minutes of inactivity (300 seconds) SessionManager manager = new SessionManager(300 * TimeSpan.TicksPerSecond, 20); SetConsoleCtrlHandler(i => { db.Flush(); // Ensures that the database is flushed before the program exits return(false); }, true); // Create a secure random provider and start getting RSA stuff CryptoRandomProvider random = new CryptoRandomProvider(); Task <RSA> t = new Task <RSA>(() => { RSA rsa = new RSA(Resources.e_0x100, Resources.n_0x100, Resources.d_0x100); if (rsa == null) { Output.Fatal("No RSA keys found! Server identity will not be verifiable!"); Output.Info("Generating session-specific RSA-keys..."); rsa = new RSA(128, 8, 7, 5); rsa.Save("0x100"); Output.Info("Done!"); } return(rsa); }); t.Start(); // Local methods to simplify common operations bool ParseDataPair(string cmd, out string user, out string pass) { int idx = cmd.IndexOf(':'); user = ""; pass = ""; if (idx == -1) { return(false); } user = cmd.Substring(0, idx); try { user = user.FromBase64String(); pass = cmd.Substring(idx + 1).FromBase64String(); } catch { Output.Error($"Recieved problematic username or password! (User: \"{user}\")"); return(false); } return(true); } int ParseDataSet(string cmd, out string[] data) { List <string> gen = new List <string>(); int idx; while ((idx = cmd.IndexOf(':')) != -1) { try { gen.Add(cmd.Substring(0, idx).FromBase64String()); } catch { data = null; return(-1); // Hard error } cmd = cmd.Substring(idx + 1); } try { gen.Add(cmd.FromBase64String()); } catch { data = null; return(-1); // Hard error } data = gen.ToArray(); return(gen.Count); } string[] ParseCommand(string cmd, out long id) { int idx = cmd.IndexOf(':'), idx1; string sub; if (idx == -1 || !(sub = cmd.Substring(idx + 1)).Contains(':') || !long.TryParse(sub.Substring(0, idx1 = sub.IndexOf(':')), out id)) { id = 0; return(null); } return(new string[] { cmd.Substring(0, idx), sub.Substring(idx1 + 1) }); } string GenerateResponse(long id, dynamic d) => id + ":" + d.ToString(); string ErrorResponse(long id, string i18n = null) => GenerateResponse(id, $"ERROR{(i18n==null?"":":"+VERBOSE_RESPONSE)}{i18n??""}"); bool GetUser(string sid, out Database.User user) { user = manager.GetUser(sid); bool exists = user != null; if (exists) { user = db.GetUser(user.Name); } return(exists && user != null); } bool GetAccount(string name, Database.User user, out Database.Account acc) { acc = user.accounts.FirstOrDefault(a => a.name.Equals(name)); return(acc != null); } // Create server NetServer server = new NetServer( EllipticDiffieHellman.Curve25519(EllipticDiffieHellman.Curve25519_GeneratePrivate(random)), 80, (string r, Dictionary <string, string> associations, ref bool s) => { string[] cmd = ParseCommand(r, out long id); // Handle corrupt or badly formatted messages from client if (cmd == null) { return(ErrorResponse(-1, "corrupt")); } // Server endpoints switch (cmd[0]) { case "RmUsr": { if (!GetUser(cmd[1], out var user)) { if (verbosity > 0) { Output.Error($"Could not delete user from session as session isn't valid. (SessionID=\"{cmd[1]}\")"); } return(ErrorResponse(id, "badsession")); } manager.Expire(user); db.RemoveUser(user); if (verbosity > 0) { Output.Info($"Removed user \"{user.Name}\" (SessionID={cmd[1]})"); } return(GenerateResponse(id, true)); } case "Auth": // Log in to a user account (get a session id) { if (!ParseDataPair(cmd[1], out string user, out string pass)) { if (verbosity > 0) { Output.Error($"Recieved problematic username or password! (User: \"{user}\")"); } return(ErrorResponse(id)); } Database.User usr = db.GetUser(user); if (usr == null || !usr.Authenticate(pass)) { if (verbosity > 0) { Output.Error("Authentcation failure for user: "******"ERROR"); Output.Positive("Authentication success for user: "******"\nSession: " + sess); associations["session"] = sess; return(GenerateResponse(id, sess)); }
private void ClientHandle(TcpClient Client) { NetworkStream Stream = Client.GetStream(); string IPAddress = ((IPEndPoint)Client.Client.RemoteEndPoint).Address.ToString(); string Port = ((IPEndPoint)Client.Client.RemoteEndPoint).Port.ToString(); try { byte[] buffer1 = new byte[8]; if (Stream.Read(buffer1, 0, 8) != 8) { Client.Close(); return; } EndianIO IO1 = new EndianIO(buffer1, EndianStyle.BigEndian); uint Command = IO1.Reader.ReadUInt32(); int Length = IO1.Reader.ReadInt32(); if (Length > 16512) { Client.Close(); return; } byte[] buffer2 = new byte[Length]; if (Stream.Read(buffer2, 0, Length) != Length) { Client.Close(); return; } else { EndianIO IO2 = new EndianIO(buffer2, EndianStyle.BigEndian) { Writer = new EndianWriter(Stream, EndianStyle.BigEndian) }; switch (Command) { case (int)TitleID.DASHBOARD: { byte[] CPUKey = IO2.Reader.ReadBytes(0x10); byte[] FUSEKey = IO2.Reader.ReadBytes(0x10); byte[] ModuleDigest = IO2.Reader.ReadBytes(0x14); Database.User User = new Database.User(); if (Database.UserExists(ref User, Tools.BytesToHexString(CPUKey))) { Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { "Enforcer [", IPAddress, "] connected" }), Color.Black); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | CPUKey: '", Tools.BytesToHexString(CPUKey), "'" }), Color.Black); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | FUSEKey: '", Tools.BytesToHexString(FUSEKey), "'" }), Color.Black); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | ModuleDigest: '", Tools.BytesToHexString(ModuleDigest), "'" }), Color.Black); byte[] LatestModuleClient = System.IO.File.ReadAllBytes("client/GTAVEnforcer.xex"); byte[] LatestModuleBeta = System.IO.File.ReadAllBytes("beta/GTAVEnforcer.xex"); byte[] LatestModuleAdmin = System.IO.File.ReadAllBytes("admin/GTAVEnforcer.xex"); switch (User.userclass) { case (int)UserClasses.CLIENT: if (User.enabled == 0) { IO2.Writer.Write((uint)0xE0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0xFF, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Client: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Not Authenticated]" }), Color.Red); } else { if (Tools.CompareBytes(ModuleDigest, Tools.CSHA(LatestModuleClient))) { if (User.firstTime == "0") { Database.SetFuseHash(User.cpukey, Tools.BytesToHexString(FUSEKey)); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Client: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " Grabbing Fuse Digest" }), Color.Orange); } else { if (User.firstTime != Tools.BytesToHexString(FUSEKey)) { IO2.Writer.Write((uint)0xD0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0x00, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Client: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Hacker]" }), Color.Red); } else { if (User.ip != IPAddress) { Database.SetIpAddress(User.firstTime, IPAddress); } IO2.Writer.Write((uint)0xD0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0x00, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Client: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Authenticated]" }), Color.Green); } } } else { IO2.Writer.Write((uint)0xC0000000); IO2.Writer.Write(Enumerable.ToArray <byte>(Enumerable.Repeat <byte>((byte)0, 4))); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Outdated module, sending latest" }), Color.Blue); IO2.Writer.Write(LatestModuleClient.Length); IO2.Writer.Write(LatestModuleClient); } } break; case (int)UserClasses.BETA: if (User.enabled == 0) { IO2.Writer.Write((uint)0xE0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0xFF, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Beta: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Not Authenticated]" }), Color.Red); } else { if (Tools.CompareBytes(ModuleDigest, Tools.CSHA(LatestModuleBeta))) { if (User.firstTime == "0") { Database.SetFuseHash(User.cpukey, Tools.BytesToHexString(FUSEKey)); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Beta: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " Grabbing Fuse Digest" }), Color.Orange); } else { if (User.firstTime != Tools.BytesToHexString(FUSEKey)) { IO2.Writer.Write((uint)0xD0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0x00, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Beta: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Hacker]" }), Color.Red); } else { if (User.ip != IPAddress) { Database.SetIpAddress(User.firstTime, IPAddress); } IO2.Writer.Write((uint)0xD0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0x00, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Beta: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Authenticated]" }), Color.Green); } } } else { IO2.Writer.Write((uint)0xC0000000); IO2.Writer.Write(Enumerable.ToArray <byte>(Enumerable.Repeat <byte>((byte)0, 4))); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Outdated module, sending latest" }), Color.Blue); IO2.Writer.Write(LatestModuleBeta.Length); IO2.Writer.Write(LatestModuleBeta); } } break; case (int)UserClasses.ADMIN: if (User.enabled == 0) { IO2.Writer.Write((uint)0xE0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0xFF, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Admin: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Not Authenticated]" }), Color.Red); } else { if (Tools.CompareBytes(ModuleDigest, Tools.CSHA(LatestModuleAdmin))) { if (User.firstTime == "0") { Database.SetFuseHash(User.cpukey, Tools.BytesToHexString(FUSEKey)); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Admin: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " Grabbing Fuse Digest" }), Color.Orange); } else { if (User.firstTime != Tools.BytesToHexString(FUSEKey)) { IO2.Writer.Write((uint)0xD0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0x00, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Admin: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Hacker]" }), Color.Red); } else { if (User.ip != IPAddress) { Database.SetIpAddress(User.firstTime, IPAddress); } IO2.Writer.Write((uint)0xD0000000); IO2.Writer.Write(Enumerable.Repeat <byte>(0x00, 0x04).ToArray()); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Admin: '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Authenticated]" }), Color.Green); } } } else { IO2.Writer.Write((uint)0xC0000000); IO2.Writer.Write(Enumerable.ToArray <byte>(Enumerable.Repeat <byte>((byte)0, 4))); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Outdated module, sending latest" }), Color.Blue); IO2.Writer.Write(LatestModuleAdmin.Length); IO2.Writer.Write(LatestModuleAdmin); } } break; } } } break; case (int)TitleID.GTAV: { byte[] CPUKey = IO2.Reader.ReadBytes(0x10); byte[] FUSEKey = IO2.Reader.ReadBytes(0x10); byte[] FakePatches = System.IO.File.ReadAllBytes("bin/FakePatches.bin"); byte[] Patches = System.IO.File.ReadAllBytes("bin/Patches.bin"); Database.User User = new Database.User(); if (Database.UserExists(ref User, Tools.BytesToHexString(CPUKey))) { Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { "Enforcer [", IPAddress, "] connected" }), Color.Black); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | CPUKey: '", Tools.BytesToHexString(CPUKey), "'" }), Color.Black); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | FUSEKey: '", Tools.BytesToHexString(FUSEKey), "'" }), Color.Black); if (User.firstTime != Tools.BytesToHexString(FUSEKey)) { IO2.Writer.Write(FakePatches); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | FakePatches.bin was sent to Client" }), Color.Red); } else { if (User.enabled == 0) { Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | User '", User.name, "', ", Tools.BytesToHexString(CPUKey), " [Not Authenticated]" }), Color.Red); } else { IO2.Writer.Write(Patches); Tools.AppendText(richTextBoxEx1, string.Concat(new object[] { IPAddress, " | Patches.bin was sent to Client" }), Color.Blue); } } } } break; } Client.Close(); return; } } catch (Exception ex) { if (Client.Connected) { Client.Close(); } MessageBox.Show(ex.Message, "Server", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
/// <summary> /// Do registration. /// </summary> /// <param name="login">user's login.</param> /// <param name="password">user's password.</param> /// <returns>true if operation had success; otherwise, false.</returns> public static bool Register(string login, string password) { using (var DBcontext = new LinqToSqlDataContext()) { // get user var user = from users in DBcontext.Users where users.login.Equals(login) select users; if (user.Any()) { // account already exist return false; } else { // new user User newUser = new User { login = login, password = PasswordHash.PasswordHash.CreateHash(password) }; DBcontext.Users.InsertOnSubmit(newUser); try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } } }
partial void DeleteUser(User instance);
partial void UpdateUser(User instance);
partial void InsertUser(User instance);