autoit-rs 0.1.1

Extract and inspect compiled AutoIt2Exe and A3X payloads
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Raw string extraction over recovered payload bytes.

use crate::Record;

/// String extraction limits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limits {
    /// Minimum string length in Unicode scalar values.
    pub min_chars: usize,
    /// Maximum number of string findings to retain.
    pub max_findings: usize,
}

impl Default for Limits {
    /// Returns the default extraction limits.
    ///
    /// # Returns
    ///
    /// A [`Limits`] requiring at least 4 characters per string and retaining up to
    /// 4096 findings.
    fn default() -> Self {
        Self {
            min_chars: 4,
            max_findings: 4096,
        }
    }
}

/// A raw string found in recovered bytes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StringFinding {
    record_index: usize,
    offset: usize,
    encoding: StringEncoding,
    value: String,
}

impl StringFinding {
    /// Returns the source record index.
    ///
    /// # Returns
    ///
    /// The index of the [`Record`] this string was extracted from.
    #[must_use]
    pub const fn record_index(&self) -> usize {
        self.record_index
    }

    /// Returns byte offset inside the record payload.
    ///
    /// # Returns
    ///
    /// The byte offset within the record payload where the string begins.
    #[must_use]
    pub const fn offset(&self) -> usize {
        self.offset
    }

    /// Returns string encoding used for extraction.
    ///
    /// # Returns
    ///
    /// The [`StringEncoding`] under which this string was extracted.
    #[must_use]
    pub const fn encoding(&self) -> StringEncoding {
        self.encoding
    }

    /// Returns extracted string value.
    ///
    /// # Returns
    ///
    /// The extracted string value.
    #[must_use]
    pub fn value(&self) -> &str {
        self.value.as_str()
    }
}

/// String encoding used for a finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StringEncoding {
    /// Printable ASCII byte string.
    Ascii,
    /// UTF-16 little-endian string with printable ASCII-range code units.
    Utf16Le,
}

/// Extracts raw strings from every recovered record payload.
///
/// Scans each record payload for both printable ASCII and UTF-16LE strings,
/// stopping early once `limits.max_findings` results are collected.
///
/// # Arguments
///
/// * `records` - Parsed AU3 records whose payloads are scanned.
/// * `limits` - Minimum string length and maximum finding count.
///
/// # Returns
///
/// A vector of [`StringFinding`] values, capped at `limits.max_findings`.
#[must_use]
pub fn extract_from_records(records: &[Record], limits: Limits) -> Vec<StringFinding> {
    let mut findings = Vec::new();
    for record in records {
        extract_ascii(record.index(), record.payload_data(), limits, &mut findings);
        if findings.len() >= limits.max_findings {
            return findings;
        }
        extract_utf16le(record.index(), record.payload_data(), limits, &mut findings);
        if findings.len() >= limits.max_findings {
            return findings;
        }
    }
    findings
}

/// Scans payload bytes for printable ASCII runs and appends findings.
///
/// Walks the bytes tracking the start of each printable run and flushes a finding
/// when a non-printable byte ends the run or the data is exhausted.
///
/// # Arguments
///
/// * `record_index` - Index of the source record, stored on each finding.
/// * `data` - Payload bytes to scan.
/// * `limits` - Minimum string length and maximum finding count.
/// * `findings` - Output vector that findings are appended to.
fn extract_ascii(
    record_index: usize,
    data: &[u8],
    limits: Limits,
    findings: &mut Vec<StringFinding>,
) {
    let mut start = None;
    for (index, byte) in data.iter().enumerate() {
        if is_ascii_string_byte(*byte) {
            if start.is_none() {
                start = Some(index);
            }
        } else if let Some(offset) = start {
            push_ascii(record_index, data, offset, index, limits, findings);
            start = None;
        }
        if findings.len() >= limits.max_findings {
            return;
        }
    }
    if let Some(offset) = start {
        push_ascii(record_index, data, offset, data.len(), limits, findings);
    }
}

/// Pushes one ASCII finding for the byte range `start..end` when it qualifies.
///
/// The candidate is dropped when shorter than `limits.min_chars`, when the
/// finding cap is already reached, when the length arithmetic underflows, or when
/// the range is out of bounds.
///
/// # Arguments
///
/// * `record_index` - Index of the source record, stored on the finding.
/// * `data` - Payload bytes containing the candidate run.
/// * `start` - Inclusive start offset of the run.
/// * `end` - Exclusive end offset of the run.
/// * `limits` - Minimum string length and maximum finding count.
/// * `findings` - Output vector that the finding is appended to.
fn push_ascii(
    record_index: usize,
    data: &[u8],
    start: usize,
    end: usize,
    limits: Limits,
    findings: &mut Vec<StringFinding>,
) {
    let Some(len) = end.checked_sub(start) else {
        return;
    };
    if len < limits.min_chars || findings.len() >= limits.max_findings {
        return;
    }
    let Some(bytes) = data.get(start..end) else {
        return;
    };
    let value = String::from_utf8_lossy(bytes).into_owned();
    findings.push(StringFinding {
        record_index,
        offset: start,
        encoding: StringEncoding::Ascii,
        value,
    });
}

/// Scans payload bytes for printable UTF-16LE runs and appends findings.
///
/// Scans at both even and odd byte alignments so strings are found regardless of
/// their position, flushing a finding when a non-printable code unit ends a run
/// or the data is exhausted.
///
/// # Arguments
///
/// * `record_index` - Index of the source record, stored on each finding.
/// * `data` - Payload bytes to scan.
/// * `limits` - Minimum string length and maximum finding count.
/// * `findings` - Output vector that findings are appended to.
fn extract_utf16le(
    record_index: usize,
    data: &[u8],
    limits: Limits,
    findings: &mut Vec<StringFinding>,
) {
    for alignment in 0..2usize {
        let mut start = None;
        let mut cursor = alignment;
        while cursor.checked_add(1).is_some_and(|end| end < data.len()) {
            let Some(unit) = read_u16_at(data, cursor) else {
                return;
            };
            if is_utf16_string_unit(unit) {
                if start.is_none() {
                    start = Some(cursor);
                }
            } else if let Some(offset) = start {
                push_utf16(record_index, data, offset, cursor, limits, findings);
                start = None;
            }
            if findings.len() >= limits.max_findings {
                return;
            }
            let Some(next) = cursor.checked_add(2) else {
                return;
            };
            cursor = next;
        }
        if let Some(offset) = start {
            push_utf16(record_index, data, offset, cursor, limits, findings);
        }
    }
}

/// Pushes one UTF-16LE finding for the byte range `start..end` when it qualifies.
///
/// The candidate is dropped when its code-unit count is below `limits.min_chars`,
/// when the finding cap is already reached, when the length arithmetic
/// underflows, when the range is out of bounds, or when the bytes cannot be read
/// back as 16-bit units.
///
/// # Arguments
///
/// * `record_index` - Index of the source record, stored on the finding.
/// * `data` - Payload bytes containing the candidate run.
/// * `start` - Inclusive start byte offset of the run.
/// * `end` - Exclusive end byte offset of the run.
/// * `limits` - Minimum string length and maximum finding count.
/// * `findings` - Output vector that the finding is appended to.
fn push_utf16(
    record_index: usize,
    data: &[u8],
    start: usize,
    end: usize,
    limits: Limits,
    findings: &mut Vec<StringFinding>,
) {
    let Some(byte_len) = end.checked_sub(start) else {
        return;
    };
    let char_len = byte_len / 2;
    if char_len < limits.min_chars || findings.len() >= limits.max_findings {
        return;
    }
    let Some(bytes) = data.get(start..end) else {
        return;
    };
    let units: Option<Vec<u16>> = bytes
        .chunks_exact(2)
        .map(|chunk| read_u16_at(chunk, 0))
        .collect();
    let Some(units) = units else {
        return;
    };
    findings.push(StringFinding {
        record_index,
        offset: start,
        encoding: StringEncoding::Utf16Le,
        value: String::from_utf16_lossy(units.as_slice()),
    });
}

/// Reports whether a byte is treated as part of a printable ASCII string.
///
/// # Arguments
///
/// * `byte` - The byte to test.
///
/// # Returns
///
/// `true` for printable ASCII (`0x20..=0x7e`) or tab, `false` otherwise.
fn is_ascii_string_byte(byte: u8) -> bool {
    matches!(byte, 0x20..=0x7e | b'\t')
}

/// Reports whether a UTF-16 code unit is treated as part of a printable string.
///
/// # Arguments
///
/// * `unit` - The 16-bit code unit to test.
///
/// # Returns
///
/// `true` for printable ASCII-range units (`0x20..=0x7e`) or tab, `false`
/// otherwise.
fn is_utf16_string_unit(unit: u16) -> bool {
    matches!(unit, 0x20..=0x7e | 0x09)
}

/// Reads a little-endian `u16` from `data` at the given byte offset.
///
/// # Arguments
///
/// * `data` - The byte slice to read from.
/// * `offset` - Byte offset of the first byte of the 16-bit value.
///
/// # Returns
///
/// `Some` with the decoded `u16`, or `None` when the offset arithmetic overflows
/// or the two bytes lie outside `data`.
fn read_u16_at(data: &[u8], offset: usize) -> Option<u16> {
    let end = offset.checked_add(2)?;
    let bytes: [u8; 2] = data.get(offset..end)?.try_into().ok()?;
    Some(u16::from_le_bytes(bytes))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::au3::{DecodedString, DecompressionStatus, RecordTestParts};

    #[test]
    fn extracts_ascii_and_utf16_strings() -> Result<(), String> {
        let mut payload = Vec::from(b"\0abcd\x01\x01".as_slice());
        for unit in "WXYZ".encode_utf16() {
            payload.extend_from_slice(&unit.to_le_bytes());
        }
        payload.push(0);
        let record = test_record(payload)?;

        let findings = extract_from_records(
            &[record],
            Limits {
                min_chars: 4,
                max_findings: 8,
            },
        );

        check_eq(findings.len(), 2, "finding count")?;
        check_eq(
            findings.first().map(StringFinding::value),
            Some("abcd"),
            "ascii",
        )?;
        check_eq(
            findings.get(1).map(StringFinding::encoding),
            Some(StringEncoding::Utf16Le),
            "utf16 encoding",
        )?;
        check_eq(
            findings.get(1).map(StringFinding::value),
            Some("WXYZ"),
            "utf16",
        )
    }

    fn test_record(payload: Vec<u8>) -> Result<Record, String> {
        let payload_len = u32::try_from(payload.len()).map_err(|err| err.to_string())?;
        Ok(Record::from_parts_for_test(RecordTestParts {
            index: 3,
            offset: 0,
            subtype: DecodedString::from_text_for_test("artifact"),
            name: DecodedString::from_text_for_test("artifact.bin"),
            compressed: false,
            compressed_size: payload_len,
            uncompressed_size: payload_len,
            checksum: 0,
            checksum_valid: false,
            creation_time: 0,
            last_write_time: 0,
            encrypted_data: payload.clone(),
            decrypted_data: payload,
            decompressed_data: None,
            decompression_status: DecompressionStatus::NotCompressed,
            profile: crate::RecordProfile {
                encoding: crate::Encoding::Ea06,
                encryption: crate::EncryptionProfile::Ea06Lame,
                compression: crate::CompressionProfile::None,
            },
        }))
    }

    fn check_eq<T>(actual: T, expected: T, context: &str) -> Result<(), String>
    where
        T: core::fmt::Debug + PartialEq,
    {
        if actual == expected {
            Ok(())
        } else {
            Err(format!("{context}: got {actual:?}, expected {expected:?}"))
        }
    }
}