Exemple #1
0
        public void TestExitCode()
        {
            // Arrange
            Prison prison = new Prison();

            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();

            prisonRules.CellType  = RuleType.None;
            prisonRules.CellType |= RuleType.Filesystem;

            prisonRules.PrisonHomePath = String.Format(@"c:\prison_tests\{0}", prison.ID);

            prison.Lockdown(prisonRules);

            // Act
            Process process = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c exit 667");

            process.WaitForExit();

            prison.Destroy();

            // Assert
            Assert.AreEqual(667, process.ExitCode);
        }
Exemple #2
0
        public void TestMultipleEcho()
        {
            // Arrange
            Prison prison = new Prison();

            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();

            prisonRules.CellType       = RuleType.None;
            prisonRules.PrisonHomePath = String.Format(@"c:\prison_tests\{0}", prison.ID);

            prison.Lockdown(prisonRules);

            // Act
            Process process1 = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c echo test");

            Process process2 = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c echo test");

            // Assert
            Assert.AreNotEqual(0, process1.Id);
            Assert.AreNotEqual(0, process2.Id);

            prison.Destroy();
        }
        public void AssignNewDesktop()
        {
            // Arrange
            Prison prison = new Prison();
            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();
            prisonRules.CellType = RuleType.WindowStation;
            prisonRules.PrisonHomePath = String.Format(@"c:\prison_tests\{0}", prison.ID);

            prison.Lockdown(prisonRules);

       

            // Act
            string exe = Utilities.CreateExeForPrison(
string.Format(@"

byte[] name = new byte[1024];
uint actualLength;
GetUserObjectInformation(GetProcessWindowStation(), UOI_NAME, name, 1024, out actualLength);

string workstationName = ASCIIEncoding.ASCII.GetString(name, 0, (int)actualLength - 1);

if (workstationName != ""{0}"")
{{
return 1;
}}

return 0;   

}}

[DllImport(""user32.dll"", SetLastError = true)]
public static extern bool GetUserObjectInformation(IntPtr hObj, int nIndex,
    [Out] byte[] pvInfo, uint nLength, out uint lpnLengthNeeded);

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport(""user32"", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr GetProcessWindowStation();

public const int UOI_FLAGS = 1;
public const int UOI_NAME = 2;
public const int UOI_TYPE = 3;
public const int UOI_USER_SID = 4;
public const int UOI_HEAPSIZE = 5; //Windows Server 2003 and Windows XP/2000:  This value is not supported.
public const int UOI_IO = 6;

private static int Dummy()
{{
", prison.User.Username), prison);

            Process process = prison.Execute(exe, "", false);

            process.WaitForExit();

            prison.Destroy();
            // Assert
            Assert.AreEqual(0, process.ExitCode);
        }
        public void TestMultipleEcho()
        {
            // Arrange
            Prison prison = new Prison();
            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();
            prisonRules.CellType = RuleType.None;
            prisonRules.PrisonHomePath = String.Format(@"c:\prison_tests\{0}", prison.ID);

            prison.Lockdown(prisonRules);

            // Act
            Process process1 = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c echo test");

            Process process2 = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c echo test");

            // Assert
            Assert.AreNotEqual(0, process1.Id);
            Assert.AreNotEqual(0, process2.Id);

            prison.Destroy();
        }
        public void StopForkBombs()
        {
            Prison prison = new Prison();

            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();

            prisonRules.CellType = RuleType.Memory;
            // prisonRules.CellType = RuleType.WindowStation;
            prisonRules.CPUPercentageLimit           = 2;
            prisonRules.TotalPrivateMemoryLimitBytes = 50 * 1024 * 1024;
            prisonRules.PrisonHomePath       = @"c:\prison_tests\p7";
            prisonRules.ActiveProcessesLimit = 5;

            prison.Lockdown(prisonRules);

            Process process = prison.Execute("", "cmd /c  for /L %n in (1,0,10) do (  start cmd /k echo 32  )");

            // Wait for the bomb to explode
            while (true)
            {
                if (prison.JobObject.ActiveProcesses >= 4)
                {
                    break;
                }
                Thread.Sleep(100);
            }

            Thread.Sleep(500);

            Assert.IsTrue(prison.JobObject.ActiveProcesses < 6);

            prison.Destroy();
        }
 public void PrisonTestCleanup()
 {
     if (prison != null)
     {
         prison.Destroy();
         prison.Dispose();
         prison = null;
     }
 }
Exemple #7
0
        public void TestDestroy()
        {
            using (ShimsContext.Create())
            {
                PrisonTestsHelper.PrisonLockdownFakes();

                Prison prison = new Prison();
                prison.Tag = "uhtst";
                PrisonRules prisonRules = new PrisonRules();
                prisonRules.CellType       = RuleType.None;
                prisonRules.PrisonHomePath = @"c:\prison_tests\p3";

                prison.Lockdown(prisonRules);

                PrisonTestsHelper.PrisonDestroyFakes();

                prison.Destroy();
            }
        }
        public void PrisonDestroyIISGroupTest()
        {
            using (ShimsContext.Create())
            {
                PrisonTestsHelper.PrisonLockdownFakes();
                PrisonTestsHelper.ApplyIISGroupFakes();

                Prison prison = new Prison();
                prison.Tag = "uhtst";
                PrisonRules prisonRules = new PrisonRules();
                prisonRules.CellType = RuleType.None;
                prisonRules.CellType |= RuleType.IISGroup;
                prisonRules.PrisonHomePath = @"c:\prison_tests\p3";

                prison.Lockdown(prisonRules);

                PrisonTestsHelper.PrisonDestroyFakes();
                prison.Destroy();
            }
        }
        public void PrisonDestroyHttpsysTest()
        {
            using (ShimsContext.Create())
            {
                PrisonTestsHelper.PrisonLockdownFakes();
                PrisonTestsHelper.ApplyHttpsysFakes();

                Prison prison = new Prison();
                prison.Tag = "uhtst";
                PrisonRules prisonRules = new PrisonRules();
                prisonRules.CellType = RuleType.None;
                prisonRules.CellType |= RuleType.Httpsys;
                prisonRules.UrlPortAccess = 5400;
                prisonRules.PrisonHomePath = @"c:\prison_tests\p3";

                prison.Lockdown(prisonRules);

                PrisonTestsHelper.PrisonDestroyFakes();
                PrisonTestsHelper.HttpsysRemovePortAccessFakes();
                prison.Destroy();
            }
        }
Exemple #10
0
        public void PrisonDestroyHttpsysTest()
        {
            using (ShimsContext.Create())
            {
                PrisonTestsHelper.PrisonLockdownFakes();
                PrisonTestsHelper.ApplyHttpsysFakes();

                Prison prison = new Prison();
                prison.Tag = "uhtst";
                PrisonRules prisonRules = new PrisonRules();
                prisonRules.CellType       = RuleType.None;
                prisonRules.CellType      |= RuleType.Httpsys;
                prisonRules.UrlPortAccess  = 5400;
                prisonRules.PrisonHomePath = @"c:\prison_tests\p3";

                prison.Lockdown(prisonRules);

                PrisonTestsHelper.PrisonDestroyFakes();
                PrisonTestsHelper.HttpsysRemovePortAccessFakes();
                prison.Destroy();
            }
        }
        public void PrisonDestroyNetworkTest()
        {
            using (ShimsContext.Create())
            {
                PrisonTestsHelper.PrisonLockdownFakes();
                PrisonTestsHelper.ApplyNetworkRuleFakes();

                Prison prison = new Prison();
                prison.Tag = "uhtst";
                PrisonRules prisonRules = new PrisonRules();
                prisonRules.CellType  = RuleType.None;
                prisonRules.CellType |= RuleType.Network;
                prisonRules.NetworkOutboundRateLimitBitsPerSecond = 500;
                prisonRules.PrisonHomePath = @"c:\prison_tests\p3";

                prison.Lockdown(prisonRules);

                PrisonTestsHelper.PrisonDestroyFakes();
                ShimNetwork.RemoveOutboundThrottlePolicyString = (username) => { return; };
                prison.Destroy();
            }
        }
        public void LoadPrison()
        {
            // Arrange


            Prison prison = new Prison();

            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();

            prisonRules.PrisonHomePath = @"c:\prison_tests\p1";
            prisonRules.CellType       = RuleType.WindowStation;

            prison.Lockdown(prisonRules);

            // Act
            var prisonLoaded = Prison.LoadPrisonAndAttach(prison.ID);

            Process process = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c exit 667");

            process.WaitForExit();


            // Assert
            Process process2 = prisonLoaded.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c exit 667");

            process2.WaitForExit();

            // Assert
            Assert.AreEqual(667, process.ExitCode);

            prison.Destroy();
        }
        public void LoadPrison()
        {
            // Arrange

            
            Prison prison = new Prison();
            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();
            prisonRules.PrisonHomePath = @"c:\prison_tests\p1";
            prisonRules.CellType = RuleType.WindowStation;

            prison.Lockdown(prisonRules);

            // Act
            var prisonLoaded = Prison.LoadPrisonAndAttach(prison.ID);

            Process process = prison.Execute(
    @"c:\windows\system32\cmd.exe",
    @"/c exit 667");

            process.WaitForExit();


            // Assert
            Process process2 = prisonLoaded.Execute(
@"c:\windows\system32\cmd.exe",
@"/c exit 667");

            process2.WaitForExit();

            // Assert
            Assert.AreEqual(667, process.ExitCode);

            prison.Destroy();
        }
        public void TestExitCode()
        {
            // Arrange
            Prison prison = new Prison();
            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();
            prisonRules.CellType = RuleType.None;
            prisonRules.CellType |= RuleType.Filesystem;

            prisonRules.PrisonHomePath = String.Format(@"c:\prison_tests\{0}", prison.ID);

            prison.Lockdown(prisonRules);

            // Act
            Process process = prison.Execute(
                @"c:\windows\system32\cmd.exe",
                @"/c exit 667");

            process.WaitForExit();

            prison.Destroy();

            // Assert
            Assert.AreEqual(667, process.ExitCode);
        }
        // Currently not working
        public void LimitPagedPool()
        {
            Prison prison = new Prison();
            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();
            // prisonRules.CellType = RuleType.WindowStation;
            prisonRules.CellType = RuleType.Memory;
            prisonRules.TotalPrivateMemoryLimitBytes = 50 * 1024 * 1024;
            prisonRules.PrisonHomePath = @"c:\prison_tests\p9";
            prisonRules.ActiveProcessesLimit = 5;

            prison.Lockdown(prisonRules);

            // Act
            string exe = Utilities.CreateExeForPrison(
@"
    string MailslotName = @""\\.\mailslot\sterssmailslot"";

    var hMailslotA = CreateMailslot(MailslotName, 0, MAILSLOT_WAIT_FOREVER, IntPtr.Zero);

    var hMailslot = CreateFile(MailslotName, FileDesiredAccess.GENERIC_WRITE, FileShareMode.FILE_SHARE_READ, IntPtr.Zero, FileCreationDisposition.OPEN_EXISTING, 0, IntPtr.Zero);

    int cbBytesWritten;
    byte[] bMessage = Encoding.Unicode.GetBytes(""Hello mailslot! Still alive?"");

    while (true)
    {
        WriteFile(hMailslot, bMessage, bMessage.Length, out cbBytesWritten, IntPtr.Zero);
    }

return 0;

}

        [Flags]
        enum FileDesiredAccess : uint
        {
            GENERIC_READ = 0x80000000,
            GENERIC_WRITE = 0x40000000,
            GENERIC_EXECUTE = 0x20000000,
            GENERIC_ALL = 0x10000000
        }

        [Flags]
        enum FileShareMode : uint
        {
            Zero = 0x00000000,  // No sharing
            FILE_SHARE_DELETE = 0x00000004,
            FILE_SHARE_READ = 0x00000001,
            FILE_SHARE_WRITE = 0x00000002
        }

        enum FileCreationDisposition : uint
        {
            CREATE_NEW = 1,
            CREATE_ALWAYS = 2,
            OPEN_EXISTING = 3,
            OPEN_ALWAYS = 4,
            TRUNCATE_EXISTING = 5
        }

        const int MAILSLOT_WAIT_FOREVER = -1;
        const int MAILSLOT_NO_MESSAGE = -1;

        [DllImport(""kernel32.dll"", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr CreateMailslot(string mailslotName,
            uint nMaxMessageSize, int lReadTimeout,
            IntPtr securityAttributes);

        [DllImport(""kernel32.dll"", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr CreateFile(string fileName,
            FileDesiredAccess desiredAccess, FileShareMode shareMode,
            IntPtr securityAttributes,
            FileCreationDisposition creationDisposition,
            int flagsAndAttributes, IntPtr hTemplateFile);

        [DllImport(""kernel32.dll"", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool WriteFile(IntPtr handle,
            byte[] bytes, int numBytesToWrite, out int numBytesWritten,
            IntPtr overlapped);

private static int Dummy()
{

", prison);

            Process process = prison.Execute(exe);

            long lastVal = 0;
            // Wait for the bomb to explode
            while (prison.JobObject.PagedSystemMemory > lastVal)
            {
                lastVal = prison.JobObject.PagedSystemMemory;
                Assert.IsTrue(prison.JobObject.PagedSystemMemory < prisonRules.TotalPrivateMemoryLimitBytes);
                Thread.Sleep(300);
            }

            prison.Destroy();
        }
        public void PrisonDestroyNetworkTest()
        {
            
            using (ShimsContext.Create())
            {
                PrisonTestsHelper.PrisonLockdownFakes();
                PrisonTestsHelper.ApplyNetworkRuleFakes();

                Prison prison = new Prison();
                prison.Tag = "uhtst";
                PrisonRules prisonRules = new PrisonRules();
                prisonRules.CellType = RuleType.None;
                prisonRules.CellType |= RuleType.Network;
                prisonRules.NetworkOutboundRateLimitBitsPerSecond = 500;
                prisonRules.PrisonHomePath = @"c:\prison_tests\p3";

                prison.Lockdown(prisonRules);

                PrisonTestsHelper.PrisonDestroyFakes();
                ShimNetwork.RemoveOutboundThrottlePolicyString = (username) => { return; };
                prison.Destroy();
            }
        }
        public void StopForkBombs()
        {
            Prison prison = new Prison();
            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();
            prisonRules.CellType = RuleType.Memory;
            // prisonRules.CellType = RuleType.WindowStation;
            prisonRules.CPUPercentageLimit = 2;
            prisonRules.TotalPrivateMemoryLimitBytes = 50 * 1024 * 1024;
            prisonRules.PrisonHomePath = @"c:\prison_tests\p7";
            prisonRules.ActiveProcessesLimit = 5;

            prison.Lockdown(prisonRules);

            Process process = prison.Execute("", "cmd /c  for /L %n in (1,0,10) do (  start cmd /k echo 32  )");

            // Wait for the bomb to explode
            while (true)
            {
                if (prison.JobObject.ActiveProcesses >= 4) break;
                Thread.Sleep(100);
            }

            Thread.Sleep(500);

            Assert.IsTrue(prison.JobObject.ActiveProcesses < 6);

            prison.Destroy();
        }
        // Currently not working
        public void LimitPagedPool()
        {
            Prison prison = new Prison();

            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();

            // prisonRules.CellType = RuleType.WindowStation;
            prisonRules.CellType = RuleType.Memory;
            prisonRules.TotalPrivateMemoryLimitBytes = 50 * 1024 * 1024;
            prisonRules.PrisonHomePath       = @"c:\prison_tests\p9";
            prisonRules.ActiveProcessesLimit = 5;

            prison.Lockdown(prisonRules);

            // Act
            string exe = Utilities.CreateExeForPrison(
                @"
    string MailslotName = @""\\.\mailslot\sterssmailslot"";

    var hMailslotA = CreateMailslot(MailslotName, 0, MAILSLOT_WAIT_FOREVER, IntPtr.Zero);

    var hMailslot = CreateFile(MailslotName, FileDesiredAccess.GENERIC_WRITE, FileShareMode.FILE_SHARE_READ, IntPtr.Zero, FileCreationDisposition.OPEN_EXISTING, 0, IntPtr.Zero);

    int cbBytesWritten;
    byte[] bMessage = Encoding.Unicode.GetBytes(""Hello mailslot! Still alive?"");

    while (true)
    {
        WriteFile(hMailslot, bMessage, bMessage.Length, out cbBytesWritten, IntPtr.Zero);
    }

return 0;

}

        [Flags]
        enum FileDesiredAccess : uint
        {
            GENERIC_READ = 0x80000000,
            GENERIC_WRITE = 0x40000000,
            GENERIC_EXECUTE = 0x20000000,
            GENERIC_ALL = 0x10000000
        }

        [Flags]
        enum FileShareMode : uint
        {
            Zero = 0x00000000,  // No sharing
            FILE_SHARE_DELETE = 0x00000004,
            FILE_SHARE_READ = 0x00000001,
            FILE_SHARE_WRITE = 0x00000002
        }

        enum FileCreationDisposition : uint
        {
            CREATE_NEW = 1,
            CREATE_ALWAYS = 2,
            OPEN_EXISTING = 3,
            OPEN_ALWAYS = 4,
            TRUNCATE_EXISTING = 5
        }

        const int MAILSLOT_WAIT_FOREVER = -1;
        const int MAILSLOT_NO_MESSAGE = -1;

        [DllImport(""kernel32.dll"", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr CreateMailslot(string mailslotName,
            uint nMaxMessageSize, int lReadTimeout,
            IntPtr securityAttributes);

        [DllImport(""kernel32.dll"", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr CreateFile(string fileName,
            FileDesiredAccess desiredAccess, FileShareMode shareMode,
            IntPtr securityAttributes,
            FileCreationDisposition creationDisposition,
            int flagsAndAttributes, IntPtr hTemplateFile);

        [DllImport(""kernel32.dll"", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool WriteFile(IntPtr handle,
            byte[] bytes, int numBytesToWrite, out int numBytesWritten,
            IntPtr overlapped);

private static int Dummy()
{

", prison);

            Process process = prison.Execute(exe);

            long lastVal = 0;

            // Wait for the bomb to explode
            while (prison.JobObject.PagedSystemMemory > lastVal)
            {
                lastVal = prison.JobObject.PagedSystemMemory;
                Assert.IsTrue(prison.JobObject.PagedSystemMemory < prisonRules.TotalPrivateMemoryLimitBytes);
                Thread.Sleep(300);
            }

            prison.Destroy();
        }
        public void AssignNewDesktop()
        {
            // Arrange
            Prison prison = new Prison();

            prison.Tag = "uhtst";

            PrisonRules prisonRules = new PrisonRules();

            prisonRules.CellType       = RuleType.WindowStation;
            prisonRules.PrisonHomePath = String.Format(@"c:\prison_tests\{0}", prison.ID);

            prison.Lockdown(prisonRules);



            // Act
            string exe = Utilities.CreateExeForPrison(
                string.Format(@"

byte[] name = new byte[1024];
uint actualLength;
GetUserObjectInformation(GetProcessWindowStation(), UOI_NAME, name, 1024, out actualLength);

string workstationName = ASCIIEncoding.ASCII.GetString(name, 0, (int)actualLength - 1);

if (workstationName != ""{0}"")
{{
return 1;
}}

return 0;   

}}

[DllImport(""user32.dll"", SetLastError = true)]
public static extern bool GetUserObjectInformation(IntPtr hObj, int nIndex,
    [Out] byte[] pvInfo, uint nLength, out uint lpnLengthNeeded);

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport(""user32"", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr GetProcessWindowStation();

public const int UOI_FLAGS = 1;
public const int UOI_NAME = 2;
public const int UOI_TYPE = 3;
public const int UOI_USER_SID = 4;
public const int UOI_HEAPSIZE = 5; //Windows Server 2003 and Windows XP/2000:  This value is not supported.
public const int UOI_IO = 6;

private static int Dummy()
{{
", prison.User.Username), prison);

            Process process = prison.Execute(exe, "", false);

            process.WaitForExit();

            prison.Destroy();
            // Assert
            Assert.AreEqual(0, process.ExitCode);
        }