Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class StartUp
- {
- public static void Main()
- {
- var spy = new Spy();
- //var result = spy.StealFieldInfo("Hacker", "username", "password");
- //var result = spy.AnalyzeAcessModifiers("Hacker");
- //var result = spy.RevealPrivateMethods("Hacker");
- var result = spy.CollectGettersAndSetters("Hacker");
- Console.WriteLine(result);
- }
- }
- using System;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- public class Spy
- {
- private StringBuilder result;
- private Type classType;
- public Spy()
- {
- this.result = new StringBuilder();
- this.classType = Type.GetType("Hacker");
- }
- public string StealFieldInfo(string investigatedClass, params string[] requestedFields)
- {
- this.result.AppendLine($"Class under investigation: {this.classType}");
- Object classInstance = Activator.CreateInstance(this.classType);
- foreach (var fieldName in requestedFields)
- {
- FieldInfo fieldInfo = this.classType.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- if (fieldInfo != null)
- {
- this.result.AppendLine($"{fieldInfo.Name} = {fieldInfo.GetValue(classInstance)}");
- }
- }
- return result.ToString().Trim();
- }
- public string AnalyzeAcessModifiers(string className)
- {
- var fieldsInfo = this.classType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
- foreach (FieldInfo fieldInfo in fieldsInfo)
- {
- if (!fieldInfo.IsPrivate)
- {
- this.result.AppendLine($"{fieldInfo.Name} must be private!");
- }
- }
- var nonPublicMethodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
- foreach (MethodInfo nonPublicMethodInfo in nonPublicMethodsInfo.Where(m => m.Name.StartsWith("get")))
- {
- this.result.AppendLine($"{nonPublicMethodInfo.Name} have to be public!");
- }
- var publicMethodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.Public);
- foreach (MethodInfo publicMethodInfo in publicMethodsInfo.Where(m => m.Name.StartsWith("set")))
- {
- this.result.AppendLine($"{publicMethodInfo.Name} have to be private!");
- }
- return this.result.ToString().Trim();
- }
- public string RevealPrivateMethods(string className)
- {
- this.result.AppendLine($"All Private Methods of Class: {className}");
- this.result.AppendLine($"Base Class: {this.classType.BaseType.Name}");
- var nonPublicMethodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
- foreach (MethodInfo methodInfo in nonPublicMethodsInfo)
- {
- this.result.AppendLine(methodInfo.Name);
- }
- return this.result.ToString().Trim();
- }
- public string CollectGettersAndSetters(string className)
- {
- var methodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- foreach (MethodInfo methodInfo in methodsInfo.Where(m => m.Name.StartsWith("get")))
- {
- this.result.AppendLine($"{methodInfo.Name} will return {methodInfo.ReturnType}");
- }
- foreach (MethodInfo methodInfo in methodsInfo.Where(m => m.Name.StartsWith("set")))
- {
- this.result.AppendLine($"{methodInfo.Name} will set field of {methodInfo.GetParameters().First().ParameterType}");
- }
- return this.result.ToString().Trim();
- }
- }
- public class Hacker
- {
- public string username = "securityGod82";
- private string password = "mySuperSecretPassw0rd";
- public string Password
- {
- get => this.password;
- set => this.password = value;
- }
- private int Id { get; set; }
- public double BankAccountBalance { get; private set; }
- public void DownloadAllBankAccountsInTheWorld()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement