Professional Documents
Culture Documents
using System;
using OpenMcdf;
using System.Text;
using System.Collections.Generic;
using Kavod.Vba.Compression;
using System.Linq;
using NDesk.Options;
using System.Net;
using System.Threading;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Collections;
List<string> extra;
try
{
extra = p.Parse(args);
}
catch (OptionException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try '--help' for more information.");
return;
}
if (extra.Count > 0)
{
filename = string.Join(" ", extra.ToArray());
}
else
{
optionShowHelp = true;
}
if (optionShowHelp)
{
ShowHelp(p);
return;
}
// End parsing command line arguments
commonStorage.GetStorage("VBA").GetStream("_VBA_PROJECT").SetData(vbaProjectStream)
;
}
commonStorage.GetStream("project").SetData(Encoding.UTF8.GetBytes(newProjectStreamS
tring));
}
commonStorage.GetStream("project").SetData(Encoding.UTF8.GetBytes(newProjectStreamS
tring));
}
commonStorage.GetStream("project").SetData(Encoding.UTF8.GetBytes(projectStreamStri
ng));
}
projectStreamString =
projectStreamString.Replace(m.Groups[0].Value, m.Groups[1].Value + moduleString +
m.Groups[3].Value);
commonStorage.GetStream("project").SetData(Encoding.UTF8.GetBytes(projectStreamStri
ng));
}
}
streamBytes =
commonStorage.GetStorage("VBA").GetStream(vbaModule.moduleName).GetData();
streamBytes =
ReplaceVBATextInModuleStream(streamBytes, vbaModule.textOffset, newVBACode);
commonStorage.GetStorage("VBA").GetStream(vbaModule.moduleName).SetData(streamBytes
);
}
}
}
// Reset module names in dir stream so that the ASCII names match the
Unicode names (undo SetRandomNames option)
if (optionResetModuleNames)
{
Console.WriteLine("Resetting module names in dir stream to match
names is _VBA_PROJECT stream (undo SetRandomNames option)");
commonStorage.GetStorage("VBA").GetStream("dir").SetData(Compress(ResetModuleNamesI
nDirStream(dirStream)));
}
return vbaModulesNamesFromProjectwm;
}
CompoundFile cf = null;
try
{
cf = new CompoundFile(outFilename, CFSUpdateMode.Update, 0);
}
catch (Exception e)
{
Console.WriteLine("ERROR: Could not open file " + outFilename);
Console.WriteLine("Please make sure this file exists and is
.docm or .xlsm file or a .doc in the Office 97-2003 format.");
Console.WriteLine();
Console.WriteLine(e.Message);
}
CFStream streamData =
cf.RootStorage.GetStorage("Macros").GetStorage("VBA").GetStream("_VBA_PROJECT");
byte[] streamBytes = streamData.GetData();
string targetOfficeVersion =
UserAgentToOfficeVersion(request.UserAgent);
ReplaceOfficeVersionInVBAProject(streamBytes, targetOfficeVersion);
cf.RootStorage.GetStorage("Macros").GetStorage("VBA").GetStream("_VBA_PROJECT").Set
Data(streamBytes);
// Determine architecture
if (userAgent.Contains("x64") || userAgent.Contains("Win64"))
officeVersion += "x64";
else
officeVersion += "x86";
return officeVersion;
}
switch (officeVersion)
{
case "2010x86":
version[0] = 0x97;
version[1] = 0x00;
break;
case "2013x86":
version[0] = 0xA3;
version[1] = 0x00;
break;
case "2016x86":
version[0] = 0xAF;
version[1] = 0x00;
break;
case "2013x64":
version[0] = 0xA6;
version[1] = 0x00;
break;
case "2016x64":
version[0] = 0xB2;
version[1] = 0x00;
break;
case "2019x64":
version[0] = 0xB2;
version[1] = 0x00;
break;
default:
Console.WriteLine("ERROR: Incorrect MS Office version
specified - skipping this step.");
return moduleStream;
}
moduleStream[2] = version[0];
moduleStream[3] = version[1];
return moduleStream;
}
private static byte[] ReplaceVBATextInModuleStream(byte[] moduleStream,
UInt32 textOffset, string newVBACode)
{
return
moduleStream.Take((int)textOffset).Concat(Compress(Encoding.UTF8.GetBytes(newVBACod
e))).ToArray();
}
return vbaModuleText;
}
int offset = 0;
UInt16 tag;
UInt32 wLength;
switch (tag)
{
case 26: // 2.3.4.2.3.2.3 MODULESTREAMNAME Record
System.Text.UTF8Encoding encoding = new
System.Text.UTF8Encoding();
encoding.GetBytes(Utils.RandomString((int)wLength),
0, (int)wLength, dirStream, (int)offset + 6);
break;
}
offset += 6;
offset += (int)wLength;
}
return dirStream;
}
private static byte[] ResetModuleNamesInDirStream(byte[] dirStream)
{
// 2.3.4.2 dir Stream: Version Independent Project Information
// https://msdn.microsoft.com/en-us/library/dd906362(v=office.12).aspx
// Dir stream is ALWAYS in little endian
int offset = 0;
UInt16 tag;
UInt32 wLength;
switch (tag)
{
case 26: // 2.3.4.2.3.2.3 MODULESTREAMNAME Record
System.Text.UTF8Encoding encoding = new
System.Text.UTF8Encoding();
UInt32 wLengthOrig = wLength;
int offsetOrig = offset;
offset += 6;
offset += (int)wLength;
tag = GetWord(dirStream, offset);
wLength = GetDoubleWord(dirStream, offset + 2);
string moduleNameFromUnicode =
System.Text.Encoding.Unicode.GetString(dirStream.Skip(offset +
6).Take((int)wLength).ToArray());
encoding.GetBytes(moduleNameFromUnicode, 0,
(int)wLengthOrig, dirStream, (int)offsetOrig + 6);
break;
}
offset += 6;
offset += (int)wLength;
}
return dirStream;
}
int offset = 0;
UInt16 tag;
UInt32 wLength;
ModuleInformation currentModule = new ModuleInformation { moduleName =
"", textOffset = 0 };
switch (tag)
{
case 26: // 2.3.4.2.3.2.3 MODULESTREAMNAME Record
currentModule.moduleName =
System.Text.Encoding.UTF8.GetString(dirStream, (int)offset + 6, (int)wLength);
break;
case 49: // 2.3.4.2.3.2.5 MODULEOFFSET Record
currentModule.textOffset = GetDoubleWord(dirStream,
offset + 6);
modules.Add(currentModule);
currentModule = new ModuleInformation { moduleName =
"", textOffset = 0 };
break;
}
offset += 6;
offset += (int)wLength;
}
return modules;
}
_responderMethod = method;
_listener.Start();
}