Exemplo n.º 1
0
        public static bool ExtractResourceToFile(ConsoleContent console, string resourceName, string filename)
        {
            using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                if (stream == null)
                {
                    console.Error("cannot find resource " + resourceName);
                    return(false);
                }

                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    byte[] buf = new byte[stream.Length];
                    stream.Read(buf, 0, buf.Length);

                    try
                    {
                        fs.Write(buf, 0, buf.Length);
                    }
                    catch (IOException err)
                    {
                        console.Error("cannot write to " + filename + ": " + err);
                        return(false);
                    }
                }
            }

            console.Info("OpenGameCamera extracted to " + filename);
            return(true);
        }
Exemplo n.º 2
0
        private void InjectButton_Click(object sender, RoutedEventArgs e)
        {
            string rootDir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "OpenGameCamera", buildVersion ?? "default");

            if (!Directory.Exists(rootDir))
            {
                try
                {
                    Directory.CreateDirectory(rootDir);
                    console.Info("Creating directory for OGC: path=" + rootDir);
                }
                catch (IOException)
                {
                    console.Error("Path already exists as file: path=" + rootDir);
                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    console.Error("Permission error trying to create: path=" + rootDir);
                    return;
                }
            }
            else
            {
                console.Info("Directory already exists at " + rootDir);
            }

            string dllPath = System.IO.Path.Combine(rootDir, "OpenGameCamera.dll");

            if (!File.Exists(dllPath))
            {
                if (!ResourceExtractor.ExtractResourceToFile(console, "OpenGameCamera.OpenGameCamera.dll", dllPath))
                {
                    console.Error("Failed to extract DLL -- if this happens consistently, paste the Log Output in #log-outputs in the OpenGameCamera Discord (https://discord.gg/HZ676Ff)");
                    return;
                }

                console.Info("Extracted OGC to " + dllPath);
            }

            if (!DllInjector.Inject(console, dllPath))
            {
                console.Error("Failed to inject -- if this happens consistently, paste the Log Output in #log-outputs in the OpenGameCamera Discord (https://discord.gg/HZ676Ff)");
                return;
            }

            console.Info("");
        }
Exemplo n.º 3
0
        public static Nullable <DateTime> GetBuildUTCTimestamp(ConsoleContent console)
        {
            using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenGameCamera.BuildTimestamp.txt"))
            {
                if (stream == null)
                {
                    console.Error("Cannot find UTC timestamp for current build");
                    return(null);
                }

                byte[] buf = new byte[stream.Length];
                stream.Read(buf, 0, buf.Length);

                string rawData = Encoding.Unicode.GetString(buf);
                if (rawData.Length <= 50)
                {
                    console.Error("Invalid timestamp format (too short): " + rawData.Replace("\n", "\\n"));
                    return(null);
                }

                int rawTimestampStart = rawData.LastIndexOf("\n", rawData.Length - 2);
                if (rawTimestampStart < 0)
                {
                    console.Error("Invalid timestamp format: " + rawData.Replace("\n", "\\n"));
                    return(null);
                }

                string rawTimestamp = rawData.Substring(rawTimestampStart + 1);
                if (rawTimestamp.Length < 24)
                {
                    console.Error("Bad build timestamp format: " + rawTimestamp);
                    return(null);
                }

                string rawTimezone = rawTimestamp.Substring(22);

                int minOffset;

                try
                {
                    minOffset = Int32.Parse(rawTimezone, NumberStyles.AllowTrailingWhite);
                }
                catch (FormatException)
                {
                    console.Error("Timezone modifier not a number: " + rawTimezone);
                    return(null);
                }
                catch (OverflowException)
                {
                    console.Error("Timezone modifier too big: " + rawTimezone);
                    return(null);
                }

                int offsetHours = minOffset / 60;
                int offsetMins  = minOffset % 60;

                string utcStamp = String.Format("{0}{1:00}:{2:00}", rawTimestamp.Substring(0, 22), offsetHours, offsetMins);
                return(DateTime.ParseExact(utcStamp, "yyyyMMddHHmmss.ffffffzzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal));
            }
        }
Exemplo n.º 4
0
        public static bool Inject(ConsoleContent console, string dll_path)
        {
            IntPtr wnd = FindWindow(null, "STAR WARS Battlefront II");

            if (wnd == null)
            {
                console.Error("failed to find an active Star Wars Battlefront II game: code=" + Marshal.GetLastWin32Error());
                return(false);
            }

            uint processId = 0;

            GetWindowThreadProcessId(wnd, out processId);

            if (processId == 0)
            {
                console.Error("failed to obtain process ID for Star Wars Battlefront II game: code=" + Marshal.GetLastWin32Error());
                return(false);
            }

            console.Info("found SWBF2 at pid " + processId);

            IntPtr procHandle = OpenProcess(
                PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ,
                false,
                (int)processId
                );

            if (procHandle == null)
            {
                console.Error("failed to open pid=" + processId + ": code=" + Marshal.GetLastWin32Error());
                return(false);
            }

            try
            {
                IntPtr loadLibraryPtr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryW");

                console.Info("LoadLibraryW found at " + loadLibraryPtr.ToInt64().ToString("X16"));

                byte[] pathBuf = Encoding.Unicode.GetBytes(dll_path);

                IntPtr targetAddr = VirtualAllocEx(
                    procHandle,
                    IntPtr.Zero,
                    (uint)(pathBuf.Length + Marshal.SizeOf(typeof(ushort))),
                    MEM_COMMIT | MEM_RESERVE,
                    PAGE_READWRITE
                    );
                if (targetAddr == null)
                {
                    console.Error("failed to allocate memory in target process: code=" + Marshal.GetLastWin32Error());
                    return(false);
                }

                console.Info("memory allocated in SWBF2: 0x" + targetAddr.ToInt64().ToString("X16"));

                UIntPtr bytesWritten;

                if (!WriteProcessMemory(procHandle, targetAddr, pathBuf, (uint)pathBuf.Length, out bytesWritten))
                {
                    console.Error("failed to write memory in target process: code=" + Marshal.GetLastWin32Error());
                    return(false);
                }

                console.Info("wrote " + bytesWritten.ToUInt64() + " bytes to allocated memory");

                IntPtr threadId;

                if (CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryPtr, targetAddr, 0, out threadId) == null)
                {
                    console.Error("failed to launch remote thread: code=" + Marshal.GetLastWin32Error());
                    return(false);
                }

                console.Info("OGC injected into SWBF2: threadId=" + threadId.ToInt64());
            }
            finally
            {
                CloseHandle(procHandle);
            }

            return(true);
        }