Skip to content

Instantly share code, notes, and snippets.

@SamuelTulach
Created March 16, 2019 12:46
Show Gist options
  • Save SamuelTulach/9a90e8c525e1ab0b676e68d82fa64a42 to your computer and use it in GitHub Desktop.
Save SamuelTulach/9a90e8c525e1ab0b676e68d82fa64a42 to your computer and use it in GitHub Desktop.
Automatic junk code adder
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace JunkCodeAdder
{
class Program
{
private static string _dir = "/home/samueltulach/Downloads/ObfuscateTest/Fuzion/src";
private static int _varcount = 100;
private static int _varlength = 50;
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertzuioplkjhgfdsayxcvbnm";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
static string Generate()
{
List<string> variables = new List<string>();
string code = "";
// Generate random variable names
for (int i = 0; i < _varcount; i++)
{
variables.Add(RandomString(_varlength));
}
// Generate varibale initialization and printf
// Printf because some compilers might without it delete our junk (optimalization)
foreach (string varname in variables)
{
code += "int " + varname + " = " + random.Next(0, 379) + ";";
// printf ("JUNK: %d", varname);
code += "printf(\"JUNK: %d\", " + varname + ");";
}
return code;
}
static void Main(string[] args)
{
Console.WriteLine("DIR: " + _dir);
// Get all .cpp files in directory
string[] cppfiles = Directory.GetFiles(_dir, "*.cpp", SearchOption.AllDirectories);
// Loop all files
foreach (string file in cppfiles)
{
// Print it because why not
Console.WriteLine("FILE: " + file);
// Read all lines and loop it
string[] lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i++)
{
// Check if there is a function on line
if (lines[i].Contains("void"))
{
// Line contains a function definition
// Now we need to check if the function bracket is on same line or not
if (lines[i].Contains("{"))
{
// Bracket is on same line
Console.WriteLine("Function found on " + i);
lines[i] += Generate();
} else if (!lines[i].Contains("{") && lines[i + 1].Contains("{"))
{
// Bracket is under the function
Console.WriteLine("Function found on " + i);
lines[i + 1] += Generate();
}
else
{
// Ignoring
// No function found
}
}
}
// Write it back to file
File.WriteAllLines(file, lines);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment