1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! A configurable and fast implementation of [electron-evil-feature-patcher]'s binary patching capabilites.
//!
//! [electron-evil-feature-patcher]: https://github.com/antelle/electron-evil-feature-patcher

use crate::{BinaryError, ElectronApp, PatcherError};
use regex::bytes::Regex;

#[cfg(test)]
use enum_iterator::IntoEnumIterator;

/// A flag inside an Electron application binary that can be patched to disable it.
pub trait Patchable: private::Sealed {
    #[doc(hidden)]
    /// Disables the option.
    ///
    /// You are probably looking for [patch_option](ElectronApp::patch_option).
    fn disable(&self, binary: &mut [u8]) -> Result<(), PatcherError>;
}

mod private {
    use super::{DevToolsMessage, ElectronOption, NodeJsCommandLineFlag};

    pub trait Sealed {}

    impl Sealed for NodeJsCommandLineFlag {}
    impl Sealed for ElectronOption {}
    impl Sealed for DevToolsMessage {}
}

/// List of known command line debugging flags that can be disabled
///
/// See the [Node.JS documentation] for details on what each flag does.
///
/// [Node.JS documentation]: https://nodejs.org/en/docs/guides/debugging-getting-started/#command-line-options
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(test, derive(IntoEnumIterator))]
#[non_exhaustive]
pub enum NodeJsCommandLineFlag {
    Inspect,
    InspectBrk,
    InspectPort,
    Debug,
    DebugBrk,
    DebugPort,
    InspectBrkNode,
    InspectPublishUid,
}

impl NodeJsCommandLineFlag {
    const fn search_string(&self) -> &'static str {
        match self {
            Self::Inspect => r"(?-u)\xAA--inspect\x00",
            Self::InspectBrk => "\0--inspect-brk\0",
            Self::InspectPort => "\0--inspect-port\0",
            Self::Debug => "\0--debug\0",
            Self::DebugBrk => "\0--debug-brk\0",
            Self::DebugPort => "\0--debug-port\0",
            Self::InspectBrkNode => "\0--inspect-brk-node\0",
            Self::InspectPublishUid => "\0--inspect-publish-uid\0",
        }
    }
}

impl Patchable for NodeJsCommandLineFlag {
    fn disable(&self, binary: &mut [u8]) -> Result<(), PatcherError> {
        let search = Regex::new(self.search_string()).expect("all regex patterns should be valid");
        let found = search
            .find(binary)
            .ok_or(BinaryError::NodeJsFlagNotPresent(*self))?
            .range();

        for b in &mut binary[found] {
            if *b == b'-' {
                *b = b' '
            }
        }

        Ok(())
    }
}

/// List of known Electron command line flags that can be disabled.
///
/// See the [Electron documentation] for details on what each flag does.
///
/// [Electron documentation]: https://www.electronjs.org/docs/api/command-line-switches
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(test, derive(IntoEnumIterator))]
#[non_exhaustive]
pub enum ElectronOption {
    JsFlags,
    RemoteDebuggingPipe,
    RemoteDebuggingPort,
    WaitForDebuggerChildren,
}

impl ElectronOption {
    const fn search_string(&self) -> &'static str {
        match self {
            Self::JsFlags => "\0js-flags\0",
            Self::RemoteDebuggingPipe => "\0remote-debugging-pipe\0",
            Self::RemoteDebuggingPort => "\0remote-debugging-port\0",
            Self::WaitForDebuggerChildren => "\0wait-for-debugger-children\0",
        }
    }
}

impl Patchable for ElectronOption {
    fn disable(&self, binary: &mut [u8]) -> Result<(), PatcherError> {
        let search = Regex::new(self.search_string()).expect("all regex patterns should be valid");
        let found = search
            .find(binary)
            .ok_or(BinaryError::ElectronOptionNotPresent(*self))?
            .range();

        let replacement = b"\0xx\r\n"
            .iter()
            .copied()
            .chain(std::iter::repeat(0))
            .take(found.len());

        for (old, new) in binary[found].iter_mut().zip(replacement) {
            *old = new;
        }

        Ok(())
    }
}

/// List of known developer tool command line messages that can be
/// written to stdout by Node.JS during debugging.
///
/// ### Warning
///
/// Disabling these is a worst-case fallback protection against internal changes to the way
/// that Chromium/Electron/Node.JS handle parsing command line arguments. If something is changed
/// and a debugging flag slips through, modifying one of these will cause the application to trigger a segemntation fault
/// and be terminated by the OS, exiting immediately.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(test, derive(IntoEnumIterator))]
#[non_exhaustive]
pub enum DevToolsMessage {
    /// The message printed to standard out when Node.JS listens on TCP port.
    ///
    /// Ex: `Debugger listening on 127.0.0.1:9229/uuid`
    Listening,
    /// The message printed to standard out when Node.JS listens on a websocket.
    ///
    /// Ex: `Debugger listening on ws://127.0.0.1:9229/uuid`
    ListeningWs,
}

impl DevToolsMessage {
    const fn search_string(&self) -> &'static str {
        match self {
            Self::Listening => "\0Debugger listening on %s\n\0",
            Self::ListeningWs => "\0\nDevTools listening on ws://%s%s\n\0",
        }
    }
}

impl Patchable for DevToolsMessage {
    fn disable(&self, binary: &mut [u8]) -> Result<(), PatcherError> {
        let search = Regex::new(self.search_string()).expect("all regex patterns should be valid");
        let found = search
            .find(binary)
            .ok_or(BinaryError::MessageNotPresent(*self))?
            .range();

        let mut replacement = Vec::with_capacity(found.len());
        replacement.push(b'\0');
        let str_len = found.len() - 3;
        for _ in (0..str_len).step_by(2) {
            replacement.push(b'%');
            replacement.push(b's');
        }
        replacement.extend_from_slice(b"\n\0");

        for (old, new) in binary[found].iter_mut().zip(replacement) {
            *old = new;
        }

        Ok(())
    }
}

impl ElectronApp<'_> {
    /// Disables the ability to use this command line flag in the application.
    ///
    /// After being disabled, the flag will no longer be processed by the application. The removal
    /// is a best-effort attempt. See the [crate documentation on effectiveness](crate).
    pub fn patch_option<P: Patchable>(&mut self, to_disable: P) -> Result<(), PatcherError> {
        to_disable.disable(self.contents)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_DATA: &[u8] = include_bytes!("../examples/fake_electron_flags.bin");

    #[test]
    fn disabling_nodejs_flags_works() {
        let mut data = TEST_DATA.to_vec();

        // Remove all the flags supported.
        for flag in NodeJsCommandLineFlag::into_enum_iter() {
            flag.disable(&mut data).unwrap();
        }

        // Ensure they no longer exist
        for flag in NodeJsCommandLineFlag::into_enum_iter() {
            assert_eq!(
                flag.disable(&mut data),
                Err(PatcherError::Binary(BinaryError::NodeJsFlagNotPresent(
                    flag
                )))
            );
        }
    }

    #[test]
    fn disabling_electron_options_works() {
        let mut data = TEST_DATA.to_vec();

        // Remove all the options supported.
        for opt in ElectronOption::into_enum_iter() {
            opt.disable(&mut data).unwrap();
        }

        // Ensure they no longer exist
        for opt in ElectronOption::into_enum_iter() {
            assert_eq!(
                opt.disable(&mut data),
                Err(PatcherError::Binary(BinaryError::ElectronOptionNotPresent(
                    opt
                )))
            );
        }
    }

    #[test]
    fn disabling_debugging_messages_works() {
        let mut data = TEST_DATA.to_vec();

        // Remove all the options supported.
        for msg in DevToolsMessage::into_enum_iter() {
            msg.disable(&mut data).unwrap();
        }

        // Ensure they no longer exist
        for msg in DevToolsMessage::into_enum_iter() {
            assert_eq!(
                msg.disable(&mut data),
                Err(PatcherError::Binary(BinaryError::MessageNotPresent(msg)))
            );
        }
    }
}