首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以编程方式设置SeCreateGlobalPrivilege?

如何以编程方式设置SeCreateGlobalPrivilege?
EN

Stack Overflow用户
提问于 2014-02-25 14:27:57
回答 1查看 1.7K关注 0票数 0

我想知道如何以编程方式将SeCreateGlobalPrivilege设置为一个非session0进程,该进程试图打开一个带有Global\前缀的MemoryMappedFile,该进程将用于在两个进程之间共享一些数据,一个进程与session0 (Windows )一起运行,另一个进程是在用户登录系统时调用CreateProcessAsUser启动的。

传递给CreateProcessAsUser函数的访问令牌是从“资源管理器”进程中复制的,该进程的sessionIdWTSGetActiveConsoleSessionId()的结果匹配。

我有这个Owin中间件,它在session0下运行,它返回由用户进程分配的MemoryMappedFile内容,该进程持续监视特定的本地文件,该文件映射到一个url,该url用于使用HTTP从外部访问数据。

我不能让Windows进程打开MemoryMappedFile,因为它会创建一些未知的sesison0进程。

下面是我试图让它在这种特定情况下工作的代码片段。只有当上述两个进程运行在相同的sessionId中时,它才能工作,而不管是否存在全局前缀。

代码语言:javascript
运行
复制
public class FileView
{
    private readonly AutoResetEvent _run = new AutoResetEvent(false);
    private readonly AutoResetEvent _update = new AutoResetEvent(false);

    private ILog logger = LogManager.GetLogger("weldR");
    private bool IsDisposed { get; set; }

    private byte[] Data { get; set; }

    private string MapName { get; set; }

    private string MutexName { get; set; }

    private MemoryMappedFileSecurity Security { get; set; }

    public FileView(string url)
    {
        if (url.StartsWith("/"))
        {
            url = url.Substring(1);
        }
        MapName = string.Format("Global\\{0}",String.Concat("pipe://", url));
        MutexName = string.Format("Global\\{0}", url.GetHashCode().ToString("x"));
        Security = new MemoryMappedFileSecurity();
        Security.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>("everyone", 
            MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

        Task.Factory.StartNew(Run);
    }

    void Run()
    {
        while (!IsDisposed)
        {
            if(Data != null)
            {
                Process process = Process.GetCurrentProcess();
                using (new PrivilegeEnabler(process, Privilege.CreateGlobal))
                {
                    var mutex = new Mutex(true, MutexName);

                    using (var mmf = MemoryMappedFile.CreateNew(MapName, Data.Length + 8,
                        MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileOptions.None, Security, HandleInheritability.Inheritable))
                    {
                        using (var viewAccessor = mmf.CreateViewAccessor())
                        {
                            try
                            {
                                var size = Data.Length.Bytes();
                                viewAccessor.WriteArray(0, size, 0, size.Length);
                                viewAccessor.WriteArray(size.Length + 1, Data, 0, Data.Length);
                            }
                            catch (Exception e)
                            {
                                logger.ErrorFormat(e.ToString());
                            }
                        }
                        mutex.ReleaseMutex();

                        _update.Set();

                        _run.WaitOne();

                        mutex.WaitOne();
                    }     
                }
            }
            else
            {
                _run.WaitOne();
            }
        }
    }

    public void Update(byte[] data)
    { 
        Data = data;
        _run.Set();
        _update.WaitOne();
    }

    public void Dispose()
    { 
        IsDisposed = true;
        _run.Set();
        _update.Set();
    }
}
private static Action<OwinRequest, OwinResponse> Serve(string path)
    {
        return (request, response) =>
            {

                //var pipename = String.Concat("pipe://", path);
               // var pipemutex = path.GetHashCode().ToString("x");

                var pipename = string.Format("Global\\{0}", String.Concat("pipe://", path));
                var pipemutex = string.Format("Global\\{0}", path.GetHashCode().ToString("x"));

                using (var mmf = MemoryMappedFile.OpenExisting(pipename))
                {
                    using (var mutex = Mutex.OpenExisting(pipemutex))
                    {
                        try
                        {
                            mutex.WaitOne();
                            using (var accessor = mmf.CreateViewAccessor())
                            {
                                var offset = 0;
                                var fileLength = new byte[4];
                                accessor.ReadArray(offset, fileLength, 0, fileLength.Length);
                                offset += fileLength.Length;
                                var size = fileLength.Int32();
                                var buff = new byte[size];
                                accessor.ReadArray(offset, buff, 0, buff.Length);

                                var lastWriteTimeUtc = new DateTime();
                                var etag = string.Concat("\"", lastWriteTimeUtc.Ticks.ToString("x"), "\"");
                                var lastModified = lastWriteTimeUtc.ToString("R");
                                if ("HEAD".Equals(request.Method, StringComparison.OrdinalIgnoreCase))
                                    return;

                                //if (CacheHelpers.ReturnNotModified(etag, lastWriteTimeUtc, context))
                                //{
                                //    mutex.ReleaseMutex();
                                //    return TaskHelpers.Completed();
                                //}
                                response.AddHeader("ETag", etag);
                                response.AddHeader("Last-Modified", lastModified);
                                response.AddHeader("Content-Type", Mime.MimeTypeFromExtension(path, "text/plain"));
                                response.AddHeader("Content-Length", size.ToString(CultureInfo.InvariantCulture));

                                response.Body.Write(buff, 0, buff.Length);
                            }
                        }
                        finally
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                }
            };
    }

当窗口服务进程试图打开用户会话进程创建的MemoryMappedFile时,我会得到以下异常。

2014-02-27 11:21:18,677,1704双倍调试: System.IO.FileNotFoundException:指定されたファイルが見つかりません.System.IO.MemoryMappedFiles.MemoryMappedFile.OpenCore(String System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(String System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(String System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath) desiredAccessRights mapName,HandleInheritability遗传力,Int32 desiredAccessRights,Boolean ) mapName,MemoryMappedFileRights desiredAccessRights,HandleInheritability遗传力(System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(String mapName)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-03 14:25:39

这似乎不可能。不过,我只是猜测,我看到其他博客文章和论坛条目表明,只有具有管理权限的进程才允许打开带有Global\前缀的MemoryMappedFile。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22017109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档