Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Text;
- using System.Web.Script.Serialization;
- namespace mapping
- {
- class Program
- {
- const string cPath = "C:\\TEMP\\CTF_Hackvent\\day09_json\\stages\\";
- public class JsonFile
- {
- public List<JsonFileContent> data { get; set; }
- }
- public class JsonFileContent
- {
- public string op { get; set; }
- public string mapTo { get; set; }
- public string content { get; set; }
- public string mapFrom { get; set; }
- public string mask { get; set; }
- }
- static void Main(string[] args)
- {
- //string filename = cPath+"jsonion.json";
- //int stage = 1;
- string filename = cPath+"stage74b.json";
- int stage = 74;
- FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
- string fileContent;
- using (StreamReader sr = new StreamReader(fs, Encoding.ASCII)) {
- fileContent = sr.ReadToEnd();
- }
- bool fContinueLoop = true;
- string nextStage = "";
- while (fContinueLoop) {
- fileContent = "{\"data\":" + fileContent + "}";
- JavaScriptSerializer jss = new JavaScriptSerializer();
- jss.MaxJsonLength = 5000000;
- JsonFile jf = jss.Deserialize<JsonFile>(fileContent);
- JsonFileContent jfc = jf.data[0];
- switch (jfc.op) {
- case "map":
- nextStage = ExecuteMap(jfc.mapFrom, jfc.mapTo, jfc.content);
- break;
- case "gzip":
- nextStage = ExecuteGzip(jfc.content);
- break;
- case "b64":
- nextStage = ExecuteBase64(jfc.content);
- break;
- case "nul":
- nextStage = jfc.content;
- break;
- case "xor":
- nextStage = ExecuteXor(jfc.mask, jfc.content);
- break;
- case "rev":
- nextStage = ExecuteReverse(jfc.content);
- break;
- case "flag":
- nextStage = jfc.content;
- fContinueLoop = false;
- break;
- default:
- throw new Exception("unknown operation!");
- }
- stage++;
- if(fContinueLoop)
- System.IO.File.WriteAllText(cPath + "stage" + stage.ToString() + ".json", nextStage, Encoding.ASCII);
- Console.WriteLine("Stage " + stage.ToString() + " done.");
- fileContent = nextStage;
- }
- Console.WriteLine("FLAG: "+nextStage);
- int a = 5;
- }
- static string ExecuteMap(string mapFrom, string mapTo, string content)
- {
- string mapTo2 = new string((char)0, 256);
- for (int i = 0; i < mapFrom.Length; i++) {
- char c1 = mapFrom[i];
- char c2 = mapTo[i];
- mapTo2 = mapTo2.Substring(0, c1) + (char)c2 + mapTo2.Substring(c1 + 1);
- }
- StringBuilder sB = new StringBuilder(content.Length);
- for (int i = 0; i < content.Length; i++)
- sB.Append((char)mapTo2[content[i]]);
- return sB.ToString();
- }
- static string ExecuteBase64(string content)
- {
- byte[] bytes = Convert.FromBase64String(content);
- return System.Text.Encoding.ASCII.GetString(bytes);
- }
- static string ExecuteGzip(string content)
- {
- const int cMaxBufSize=5000000;
- byte[] bytes = Convert.FromBase64String(content);
- MemoryStream ms = new MemoryStream(bytes);
- GZipStream sr = new GZipStream(ms, CompressionMode.Decompress);
- bytes = new byte[cMaxBufSize];
- int rByte = sr.Read(bytes, 0, bytes.Length);
- if (rByte >= cMaxBufSize)
- throw new Exception("max size limit reached!");
- StringBuilder sB = new StringBuilder(rByte);
- for (int i = 0; i < rByte; i++)
- sB.Append((char)bytes[i]);
- sr.Close();
- ms.Close();
- sr.Dispose();
- ms.Dispose();
- return sB.ToString();
- }
- static string ExecuteXor(string mask, string content)
- {
- byte[] bytesMask = Convert.FromBase64String(mask);
- byte[] bytesContent = Convert.FromBase64String(content);
- if (bytesMask.Length != 1)
- throw new Exception("XOR mask length not 1!");
- byte maskByte = bytesMask[0];
- StringBuilder sB = new StringBuilder(bytesContent.Length);
- for (int i = 0; i < bytesContent.Length; i++)
- sB.Append((char)((byte)bytesContent[i] ^ maskByte));
- return sB.ToString();
- }
- static string ExecuteReverse(string content)
- {
- char[] charArray = content.ToCharArray();
- Array.Reverse(charArray);
- return new string(charArray);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement