public byte[] ReturnBlackList() { List <Restriction> list = Restriction.ReadBlackList(); string s = Restriction.BlackListToString(list); return(AesAlg.Encrypt(s, sessionKeys[Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name)])); }
public PovratnaVrijednost CloseApp(byte[] encrypted) { OpenAppData decryted = (OpenAppData)AesAlg.Decrypt(encrypted, sessionKeys[Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name)]); IIdentity identity = Thread.CurrentPrincipal.Identity; WindowsIdentity winIdentity = identity as WindowsIdentity; string user = Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name); List <string> groups = GetUsergroups(winIdentity.Groups); blackList = Restriction.ReadBlackList(); if (Restriction.IsRestricted(blackList, decryted, user, groups)) { string pov = WCFServiceAudit.ReturnFactory().ConnectS(string.Format("{0}|{1}|{2}", user, decryted.Protokol, decryted.Port)); if (pov == "DOS") { return(PovratnaVrijednost.DOS); } return(PovratnaVrijednost.NEMADOZ);; } string key = string.Format("{0}", decryted.Port); if (servisi.ContainsKey(key)) { servisi[key].Close(); servisi.Remove(key); return(PovratnaVrijednost.USPJEH); } return(PovratnaVrijednost.NIJEOTV); }
public bool EditBlackList(byte[] crypted) { try { List <Restriction> newBlackList = (List <Restriction>)AesAlg.Decrypt(crypted, sessionKeys[Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name)]); Restriction.WriteBlackList(newBlackList); string checksum = checkMD5("Blacklist.xml"); byte[] ba = Encoding.Default.GetBytes(checksum); string hexString = BitConverter.ToString(ba); writeInTxt(hexString); return(true); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); return(false); } }
private void ChoseAppToOpen() { int port = 0; string protokol; while (true) { Console.WriteLine("Please chose one of the following actions"); Console.WriteLine("\t1.Open service"); Console.WriteLine("\t2.Close service"); Console.WriteLine("\t3.Check blacklist cache"); Console.WriteLine("\t4.Edit blacklist"); Console.WriteLine("\t5.Exit"); Console.WriteLine("Press any other key to exit"); char key = Console.ReadKey().KeyChar; switch (key) { case '1': try { Console.WriteLine("Enter port number:"); if (Int32.TryParse(Console.ReadLine(), out port)) { protokol = ChoseProto(); OpenAppData openAppData = new OpenAppData(machineName, port, protokol); byte[] encrypted = AesAlg.Encrypt(openAppData.ToString(), secretKey); PovratnaVrijednost pov = factory.OpenApp(encrypted); if (pov == PovratnaVrijednost.USPJEH) { Console.WriteLine("Uspjesno ste otvorili servis!"); } else if (pov == PovratnaVrijednost.VECOTV) { Console.WriteLine("Servis je vec otvoren!"); } else if (pov == PovratnaVrijednost.NEMADOZ) { Console.WriteLine("Nemate dozvolu da otvorite aplikaciju!"); } else if (pov == PovratnaVrijednost.DOS) { Console.WriteLine("Previse puta ste pokusali da pokrenete nedozvoljeni proces!"); Thread.Sleep(1000); return; } } } catch (Exception e) { Console.WriteLine("Request failed! Error message: " + e.Message); } break; case '2': try { Console.WriteLine("Enter port number:"); if (Int32.TryParse(Console.ReadLine(), out port)) { protokol = ChoseProto(); OpenAppData openAppData = new OpenAppData(machineName, port, protokol); PovratnaVrijednost pov = factory.CloseApp(AesAlg.Encrypt(openAppData.ToString(), secretKey)); if (pov == PovratnaVrijednost.USPJEH) { Console.WriteLine("Uspjesno ste zatvorili servis!"); } else if (pov == PovratnaVrijednost.NIJEOTV) { Console.WriteLine("Servis ne postoji!"); } else if (pov == PovratnaVrijednost.NEMADOZ) { Console.WriteLine("Nemate dozvolu da zatvorite aplikaciju!"); } else if (pov == PovratnaVrijednost.DOS) { Console.WriteLine("Previse puta ste pokusali da pokrenete nedozvoljeni proces!"); Thread.Sleep(1000); return; } } } catch (Exception e) { Console.WriteLine("Request failed! Error message: " + e.Message); } break; case '3': Console.WriteLine("Checking blacklist cache...."); try { if (factory.IsBlackListValid()) { Console.WriteLine("Black list is valid!"); } else { Console.WriteLine("Black list is not valid!"); Thread.Sleep(1000); return; } } catch (Exception e) { Console.WriteLine("Request failed! Error message: " + e.Message); } break; case '4': try { Console.Clear(); char key1 = '0'; List <Restriction> blacklist = (List <Restriction>)AesAlg.Decrypt(ReturnBlackList(), secretKey); while (key1 != '3') { int br = 1; Console.WriteLine("Blacklist:"); foreach (Restriction r in blacklist) { Console.WriteLine("{0}. {1}\t{2}\t{3}", br++, r.UserOrGroup, r.Port, r.Protocol); } Console.WriteLine("Chose action:"); Console.WriteLine("\t1.Add new restriction"); Console.WriteLine("\t2.Delete existing restriction"); Console.WriteLine("\t3.Finish"); key1 = Console.ReadKey().KeyChar; if (key1 == '1') { Console.WriteLine("Which user/user group:"); string userG = Console.ReadLine(); Console.WriteLine("Which port"); int portBr = 0; string port1 = Console.ReadLine(); if (port1 != "") { if (!Int32.TryParse(port1, out portBr)) { Console.WriteLine("For port, please enter a number"); continue; } } string proto = ChoseProto(); Restriction r = new Restriction(); r.UserOrGroup = userG; r.Port = portBr; r.Protocol = proto; blacklist.Add(r); Console.WriteLine("Restriction successfully added to blacklist!"); } else if (key1 == '2') { int redni = 0; Console.WriteLine("Wich restriction do you want to delete? Enter the number next to the restriction"); string brisanje = Console.ReadLine(); if (brisanje == "") { Console.WriteLine("Please enter a number!"); continue; } if (!Int32.TryParse(brisanje, out redni)) { Console.WriteLine("Please enter a number!"); continue; } blacklist.RemoveAt(redni + 1); Console.WriteLine("Restriction successfully deleted from blackList!"); } } if (EditBlackList(AesAlg.Encrypt(Restriction.BlackListToString(blacklist), secretKey))) { Console.WriteLine("Succesfully edited blacklist!"); } else { Console.WriteLine("Failed to edit blacklist!"); } } catch (Exception e) { Console.WriteLine("Request failed! Error message: " + e.Message); } break; case '5': return; default: Console.WriteLine("Choose one of given options!"); break; } } }
public object DecryptFile(string s, string t) { string encrypted = new AesAlg().Decrypt(s, t); return(encrypted); }
public object EncryptFile(string s, string t) { object encrypted = new AesAlg().Encrypt(s, t); return(encrypted); }
public PovratnaVrijednost OpenApp(byte[] encrypted) { OpenAppData decryted = (OpenAppData)AesAlg.Decrypt(encrypted, sessionKeys[Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name)]); Console.WriteLine("------------------ OTVARANJE SERVISA ------------------"); Console.WriteLine("Korisnik {0} je zatrazio otvaranje novog servisa na portu {1} sa protokolom {2}", Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name) , decryted.Port, decryted.Protokol); IIdentity identity = Thread.CurrentPrincipal.Identity; WindowsIdentity winIdentity = identity as WindowsIdentity; string user = Formatter.ParseName(Thread.CurrentPrincipal.Identity.Name); List <string> groups = GetUsergroups(winIdentity.Groups); blackList = Restriction.ReadBlackList(); if (Restriction.IsRestricted(blackList, decryted, user, groups)) { Console.WriteLine("Korisnik nema dozvolu za otvaranje servisa na datom portu ili sa datim protokolom."); string pov = WCFServiceAudit.ReturnFactory().ConnectS(string.Format("{0}|{1}|{2}", user, decryted.Protokol, decryted.Port)); Console.WriteLine("------------------ OTVARANJE NEUSPESNO ------------------"); if (pov == "DoS") { return(PovratnaVrijednost.DOS); } return(PovratnaVrijednost.NEMADOZ);; } if (servisi.ContainsKey(string.Format("{0}", decryted.Port))) { Console.WriteLine("Servis je vec otvoren na datom portu"); Console.WriteLine("------------------ OTVARANJE NEUSPESNO ------------------"); return(PovratnaVrijednost.VECOTV); } ServiceHost host = new ServiceHost(typeof(WCFService)); if (decryted.Protokol == "UDP") { Console.WriteLine("Otvaranje UDP konekcije"); UdpBinding binding = new UdpBinding(); string addr = String.Format("soap.udp://localhost:{0}/{1}", decryted.Port, decryted.ImeMasine); host.AddServiceEndpoint(typeof(IWCFContract), binding, addr); } else if (decryted.Protokol == "HTTP") { Console.WriteLine("Otvaranje HTTP konekcije"); NetHttpBinding binding = new NetHttpBinding(); string addr = String.Format("http://localhost:{0}/{1}", decryted.Port, decryted.ImeMasine); host.AddServiceEndpoint(typeof(IWCFContract), binding, addr); } else { Console.WriteLine("Otvaranje TCP konekcije"); NetTcpBinding binding = new NetTcpBinding(); string addr = String.Format("net.tcp://localhost:{0}/{1}", decryted.Port, decryted.ImeMasine); host.AddServiceEndpoint(typeof(IWCFContract), binding, addr); } string key = String.Format("{0}", decryted.Port); servisi.Add(key, host); servisi[key].Open(); Console.WriteLine("------------------ OTVARANJE USPESNO ------------------"); return(PovratnaVrijednost.USPJEH); }