.NET Wrapper
Posted: Tue Sep 28, 2010 11:22 pm
Hello all,
I have enjoyed using Sandboxie, and wished I had found it sooner. My usage is as an RDP replacement for running browser sessions. The US$50 it costs is simply amazing when you compare to the US$35k Microsoft wanted and US$20k Graphon GoGlobal costs to do much of our used functionality.
Anyway, here is source for a C# wrapper of the SBIE API, free to use, distribute, and modify as you see fit. I would prefer to know if you improve upon it (not hard to do), because I'll be posting this same code on CodeProject.com to sing the praises of Sandboxie soon.
I have enjoyed using Sandboxie, and wished I had found it sooner. My usage is as an RDP replacement for running browser sessions. The US$50 it costs is simply amazing when you compare to the US$35k Microsoft wanted and US$20k Graphon GoGlobal costs to do much of our used functionality.
Anyway, here is source for a C# wrapper of the SBIE API, free to use, distribute, and modify as you see fit. I would prefer to know if you improve upon it (not hard to do), because I'll be posting this same code on CodeProject.com to sing the praises of Sandboxie soon.
Code: Select all
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace SandboxieWrapper
{
public class SandboxieNative
{
private static string ConvertFromWChar(byte[] incoming)
{
string outgoing = Encoding.Unicode.GetString(incoming);
outgoing = outgoing.Remove(outgoing.IndexOf("\0"));
return outgoing;
}
[DllImport("SbieDll.dll")]
private static extern int SbieApi_EnumBoxes(
int index,
byte[] boxname);
/// <summary>
/// Retrieves the names of the sandboxes
/// </summary>
/// <returns>list of sandboxes as string</returns>
public List<string> EnumerateSandboxes()
{
var sandboxes = new List<string>();
int index = -1;
while (true)
{
var name = new byte[34];
index = SbieApi_EnumBoxes(index, name);
if (index == -1) break;
string sandboxname = ConvertFromWChar(name);
sandboxes.Add(sandboxname);
}
return sandboxes;
}
public struct SandboxPaths
{
public string FilePath;
public string KeyPath;
public string IpcPath;
}
[DllImport("SbieDll.dll")]
private static extern int SbieApi_QueryBoxPath(byte[] boxname,
byte[] filepath,
byte[] keypath,
byte[] ipcpath,
ref ulong filepathlen,
ref ulong keypathlen,
ref ulong ipcpathlen);
/// <summary>
/// Retrieves path information for a sandbox
/// </summary>
/// <param name="boxname">name of the sandbox</param>
/// <returns>SandboxPaths struct</returns>
public SandboxPaths QueryPaths(string boxname)
{
ulong fileLen = 0;
ulong keyLen = 0;
ulong ipcLen = 0;
byte[] byteBoxName = Encoding.Unicode.GetBytes(boxname);
SbieApi_QueryBoxPath(byteBoxName, null, null, null, ref fileLen, ref keyLen, ref ipcLen);
var fileBuf = new byte[fileLen];
var keyBuf = new byte[keyLen];
var ipcBuf = new byte[ipcLen];
SbieApi_QueryBoxPath(byteBoxName, fileBuf, keyBuf, ipcBuf, ref fileLen, ref keyLen, ref ipcLen);
var paths = new SandboxPaths
{
FilePath = ConvertFromWChar(fileBuf),
KeyPath = ConvertFromWChar(keyBuf),
IpcPath = ConvertFromWChar(ipcBuf)
};
return paths;
}
[DllImport("SbieDll.dll")]
private static extern int SbieApi_QueryProcessPath(uint processid,
byte[] filepath,
byte[] keypath,
byte[] ipcpath,
ref ulong filepathlen,
ref ulong keypathlen,
ref ulong ipcpathlen);
/// <summary>
/// Retrieves path information for a sandbox
/// </summary>
/// <param name="processid">process id within a sandbox</param>
/// <returns>SandboxPaths struct</returns>
public SandboxPaths QueryPaths(uint processid)
{
ulong fileLen = 0;
ulong keyLen = 0;
ulong ipcLen = 0;
SbieApi_QueryProcessPath(processid, null, null, null, ref fileLen, ref keyLen, ref ipcLen);
var fileBuf = new byte[fileLen];
var keyBuf = new byte[keyLen];
var ipcBuf = new byte[ipcLen];
SbieApi_QueryProcessPath(processid, fileBuf, keyBuf, ipcBuf, ref fileLen, ref keyLen, ref ipcLen);
var paths = new SandboxPaths
{
FilePath = ConvertFromWChar(fileBuf),
KeyPath = ConvertFromWChar(keyBuf),
IpcPath = ConvertFromWChar(ipcBuf)
};
return paths;
}
[DllImport("SbieDll.dll")]
private static extern int SbieApi_EnumProcessEx(
byte[] boxname,
bool allsessions,
Int32 whichsession,
[MarshalAs(UnmanagedType.LPArray)]
UInt32[] boxedpids);
/// <summary>
/// Retrieve a list of processes for a sandbox
/// </summary>
/// <param name="boxname">name of the sandbox</param>
/// <param name="allsessions">flag indicating all sessions (true) or a specific session (false)</param>
/// <param name="whichsession">if allessions is false, this indicates the specific session</param>
/// <returns>list of process ids for the sandbox</returns>
public List<UInt32> EnumerateProcesses(string boxname, bool allsessions, Int32 whichsession)
{
var pids = new UInt32[512];
byte[] byteBoxName = Encoding.Unicode.GetBytes(boxname);
SbieApi_EnumProcessEx(byteBoxName, allsessions, whichsession, pids);
// first item contains count
var pidlist = new List<UInt32>();
for (int i = 1; i <= pids[0]; i++)
{
if (pids[i] > 0) pidlist.Add(pids[i]);
}
return pidlist;
}
/// <summary>
/// Retrieves process information for all processes in a sandbox
/// </summary>
/// <param name="boxname">name of the sandbox</param>
/// <returns>list of ProcessInformation struct</returns>
public List<ProcessInformation> EnumerateProcesses(string boxname)
{
List<UInt32> pidlist = EnumerateProcesses(boxname,true,-1);
var proclist = new List<ProcessInformation>();
foreach (uint pid in pidlist)
{
proclist.Add(GetProcessInfo(pid));
}
return proclist;
}
[DllImport("SbieDll.dll")]
private static extern int SbieApi_QueryProcess(
uint processid,
byte[] boxname, // pointer to WCHAR [34]
byte[] imagename, // pointer to WCHAR [96]
byte[] sidstring, // pointer to WCHAR [96]
UIntPtr sessionid);
public struct ProcessInformation
{
public UInt32 ProcessId;
public string BoxName;
public string Imagename;
public string SidString;
public UInt32 SessionId;
}
/// <summary>
/// retrieve information about a single process
/// </summary>
/// <param name="processid">process id to retrieve</param>
/// <returns>ProcessInformation struct</returns>
public ProcessInformation GetProcessInfo(uint processid)
{
var proc = new ProcessInformation();
var boxname = new byte[34];
var imagename = new byte[96];
var sidstring = new byte[96];
var sessionid = new UIntPtr();
SbieApi_QueryProcess(processid, boxname, imagename, sidstring, sessionid);
proc.ProcessId = processid;
proc.BoxName = ConvertFromWChar(boxname);
proc.Imagename = ConvertFromWChar(imagename);
proc.SidString = ConvertFromWChar(sidstring);
proc.SessionId = sessionid.ToUInt32();
return proc;
}
[DllImport("SbieDll.dll")]
private static extern bool SbieDll_KillOne(ulong processid);
/// <summary>
/// terminates a single process
/// </summary>
/// <param name="processid">process id to terminate</param>
/// <returns>true on success</returns>
public bool TerminateProcess(UInt32 processid)
{
return SbieDll_KillOne(processid);
}
[DllImport("SbieDll.dll")]
private static extern bool SbieDll_KillAll(Int32 sessionid, byte[] boxname);
/// <summary>
/// terminates all processes in a sandbox
/// </summary>
/// <param name="boxname">name of the sandbox</param>
/// <param name="sessionid">session id (default -1)</param>
/// <returns>true on success</returns>
public bool TerminateAllProcesses(string boxname, int sessionid=-1)
{
byte[] byteBoxName = Encoding.Unicode.GetBytes(boxname);
return SbieDll_KillAll(sessionid, byteBoxName);
}
[DllImport("SbieDll.dll")]
private static extern Int32 SbieApi_QueryConf(
byte[] sectionname, // pointer to WCHAR [34]
byte[] settingname, // pointer to WCHAR [66]
UInt32 settingindex,
byte[] value,
UInt32 valuelen);
/// <summary>
/// Requests configuration information
/// </summary>
/// <param name="sectionName">section to retrieve</param>
/// <param name="settingName">setting to retrieve</param>
/// <param name="settingindex">if more than 1, the 0-based index to retrieve (default is zero)</param>
/// <returns>value as string</returns>
public string QueryConfiguration(string sectionName, string settingName, int settingindex=0)
{
byte[] byteSectionName = Encoding.Unicode.GetBytes(sectionName);
byte[] byteSettingName = Encoding.Unicode.GetBytes(settingName);
const uint valuelen = 8000;
var valueBuf = new byte[valuelen];
SbieApi_QueryConf(byteSectionName, byteSettingName, (UInt32)settingindex, valueBuf, valuelen);
return ConvertFromWChar(valueBuf);
}
[DllImport("SbieDll.dll")]
private static extern UInt32 SbieApi_ReloadConf(UInt32 sessionid);
/// <summary>
/// Causes the configuration file to be reloaded
/// </summary>
/// <param name="sessionid">session to reload it for or -1 for all</param>
/// <returns>zero on success, a non-zero value on error</returns>
public UInt32 ReloadConfiguration(int sessionid=-1)
{
return SbieApi_ReloadConf((UInt32)sessionid);
}
[DllImport("SbieDll.dll")]
private static extern void SbieDll_Hook(char[] name, Delegate sourcefunc, Delegate detourfunc);
/// <summary>
/// UNTESTED METHOD to set a hook
/// </summary>
/// <param name="hookName">name of the hook to set</param>
/// <param name="sourcefunction">delegate to override</param>
/// <param name="detourfunction">delegate to detour toward</param>
public void SetHook(string hookName, Delegate sourcefunction, Delegate detourfunction)
{
SbieDll_Hook(hookName.ToCharArray(), sourcefunction, detourfunction);
}
}
}