Advertisement
e4ch

Program.cs

Jan 1st, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Text;
  6. using System.Web.Script.Serialization;
  7.  
  8. namespace mapping
  9. {
  10.  
  11.     class Program
  12.     {
  13.         const string cPath = "C:\\TEMP\\CTF_Hackvent\\day09_json\\stages\\";
  14.         public class JsonFile
  15.         {
  16.             public List<JsonFileContent> data { get; set; }
  17.         }
  18.         public class JsonFileContent
  19.         {
  20.             public string op { get; set; }
  21.             public string mapTo { get; set; }
  22.             public string content { get; set; }
  23.             public string mapFrom { get; set; }
  24.             public string mask { get; set; }
  25.         }
  26.         static void Main(string[] args)
  27.         {
  28.             //string filename = cPath+"jsonion.json";
  29.             //int stage = 1;
  30.             string filename = cPath+"stage74b.json";
  31.             int stage = 74;
  32.             FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
  33.             string fileContent;
  34.             using (StreamReader sr = new StreamReader(fs, Encoding.ASCII)) {
  35.                 fileContent = sr.ReadToEnd();
  36.             }
  37.             bool fContinueLoop = true;
  38.             string nextStage = "";
  39.             while (fContinueLoop) {
  40.                 fileContent = "{\"data\":" + fileContent + "}";
  41.                 JavaScriptSerializer jss = new JavaScriptSerializer();
  42.                 jss.MaxJsonLength = 5000000;
  43.                 JsonFile jf = jss.Deserialize<JsonFile>(fileContent);
  44.                 JsonFileContent jfc = jf.data[0];
  45.                 switch (jfc.op) {
  46.                     case "map":
  47.                         nextStage = ExecuteMap(jfc.mapFrom, jfc.mapTo, jfc.content);
  48.                         break;
  49.                     case "gzip":
  50.                         nextStage = ExecuteGzip(jfc.content);
  51.                         break;
  52.                     case "b64":
  53.                         nextStage = ExecuteBase64(jfc.content);
  54.                         break;
  55.                     case "nul":
  56.                         nextStage = jfc.content;
  57.                         break;
  58.                     case "xor":
  59.                         nextStage = ExecuteXor(jfc.mask, jfc.content);
  60.                         break;
  61.                     case "rev":
  62.                         nextStage = ExecuteReverse(jfc.content);
  63.                         break;
  64.                     case "flag":
  65.                         nextStage = jfc.content;
  66.                         fContinueLoop = false;
  67.                         break;
  68.                     default:
  69.                         throw new Exception("unknown operation!");
  70.                 }
  71.                 stage++;
  72.                 if(fContinueLoop)
  73.                     System.IO.File.WriteAllText(cPath + "stage" + stage.ToString() + ".json", nextStage, Encoding.ASCII);
  74.                 Console.WriteLine("Stage " + stage.ToString() + " done.");
  75.                 fileContent = nextStage;
  76.             }
  77.             Console.WriteLine("FLAG: "+nextStage);
  78.             int a = 5;
  79.         }
  80.  
  81.         static string ExecuteMap(string mapFrom, string mapTo, string content)
  82.         {
  83.             string mapTo2 = new string((char)0, 256);
  84.             for (int i = 0; i < mapFrom.Length; i++) {
  85.                 char c1 = mapFrom[i];
  86.                 char c2 = mapTo[i];
  87.                 mapTo2 = mapTo2.Substring(0, c1) + (char)c2 + mapTo2.Substring(c1 + 1);
  88.             }
  89.             StringBuilder sB = new StringBuilder(content.Length);
  90.             for (int i = 0; i < content.Length; i++)
  91.                 sB.Append((char)mapTo2[content[i]]);
  92.             return sB.ToString();
  93.         }
  94.  
  95.         static string ExecuteBase64(string content)
  96.         {
  97.             byte[] bytes = Convert.FromBase64String(content);
  98.             return System.Text.Encoding.ASCII.GetString(bytes);
  99.         }
  100.  
  101.         static string ExecuteGzip(string content)
  102.         {
  103.             const int cMaxBufSize=5000000;
  104.             byte[] bytes = Convert.FromBase64String(content);
  105.             MemoryStream ms = new MemoryStream(bytes);
  106.             GZipStream sr = new GZipStream(ms, CompressionMode.Decompress);
  107.             bytes = new byte[cMaxBufSize];
  108.             int rByte = sr.Read(bytes, 0, bytes.Length);
  109.             if (rByte >= cMaxBufSize)
  110.                 throw new Exception("max size limit reached!");
  111.             StringBuilder sB = new StringBuilder(rByte);
  112.             for (int i = 0; i < rByte; i++)
  113.                 sB.Append((char)bytes[i]);
  114.             sr.Close();
  115.             ms.Close();
  116.             sr.Dispose();
  117.             ms.Dispose();
  118.             return sB.ToString();
  119.         }
  120.  
  121.         static string ExecuteXor(string mask, string content)
  122.         {
  123.             byte[] bytesMask = Convert.FromBase64String(mask);
  124.             byte[] bytesContent = Convert.FromBase64String(content);
  125.             if (bytesMask.Length != 1)
  126.                 throw new Exception("XOR mask length not 1!");
  127.             byte maskByte = bytesMask[0];
  128.             StringBuilder sB = new StringBuilder(bytesContent.Length);
  129.             for (int i = 0; i < bytesContent.Length; i++)
  130.                 sB.Append((char)((byte)bytesContent[i] ^ maskByte));
  131.             return sB.ToString();
  132.         }
  133.  
  134.         static string ExecuteReverse(string content)
  135.         {
  136.             char[] charArray = content.ToCharArray();
  137.             Array.Reverse(charArray);
  138.             return new string(charArray);
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement