Skip to content

New Artifact: SamsungGalleryLogEncodedStrings - #1900

Merged
abrignoni merged 13 commits into
abrignoni:mainfrom
dantitto:main
Nov 16, 2026
Merged

abrignoni merged 13 commits into
abrignoni:mainfrom
dantitto:main

Conversation

@dantitto

@dantitto dantitto commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Introduction

Adds a new artifact, SamsungGalleryLogEncodedStrings, that finds and decodes the obfuscated strings Samsung Gallery's Logger function writes into the '__log' column of the 'log' table of com.sec.android.gallery3d/databases/local.db. It covers three generations of the obfuscation in a single parser. Each log cell that holds encoded strings is reported twice: once with the strings decoded in place, so the entry can be read in context, and once more exactly as stored. The decoded text is an interpretation shown beside the raw value to allow comparison.

For a new or changed artifact

  • Ran the tool against a real extraction and confirmed the row counts, not just that it imports.
  • Ran python admin/scripts/check_artifact_output.py <report folder> on that report and fixed or documented every finding. An empty or constant column is often a real result (no group chats, coarse location denied); the fix for those is a sentence in the artifact's notes naming the column and saying it was empty or uniform, which is also what stops the checker reporting it.
  • Checked it against a second app data directory where the platform provides one (a second user, account, or container). --compare <multi-container report> reads the scaling for you: it should be exactly double.
  • notes, description and sample_data say only what the data shows, and the numbers were re-derived from the finished run.

Anything reviewers should know

Background

Samsung Gallery obfuscates certain strings (file paths, titles, transition names and similar) before they are logged or stored. Three encoding methodologies were identified during research:

  • V1 (seen in app v7.x, v10.x): no marker. A random 3–6 character prefix, then Base64(text with dummy U+25CF ● / U+2605 ★ characters interspersed), then a random 7 character suffix (later builds list a third dummy, U+25C6 ◆, which is stripped as well). The affix characters are drawn from the Base64 alphabet, so the value looks like ordinary Base64.
  • V2 (v14.x): " #G$E" + index + Base64(xorWithKey(text, sEncV2Key[index])). sEncV2Key is 8 × 4-byte keys; 'getEncodedString' emits indices 0–3.
  • V3 (v15.x): the same #G$E marker. sEncV3Key indices 0–3 are single bytes {0,1,2,3} and indices 4–7 are 256-byte keys; getEncodedString emits indices 4–7.

xorWithKey cycles the key (key[i % key.length]), as the decompiled source shows.

Decoding

  • Marker scan (V2/V3), every text column of local.db. For each #G$E token it tries every key defined at that index in both key tables and keeps whichever yields valid UTF-8, with a control-character check to reject a wrong key that still happens to produce decodable bytes. If both key tables give readable text for one string, the V3 reading is shown and Note lists both. V2 and V3 share the marker but use different index ranges and keys in practice, so this resolves them without needing to know the app version. On the tested images the strings decoded with V3 keys 4 to 7 and V2 key 3; the other keys were not exercised.
  • V1, on the log column of the log table. V1 has no marker, so it is searched for only where the logger writes it. V1_TARGETS holds (table, column) wildcard pairs, matched case-insensitively; the default ("log", "*log") covers the __log column without depending on its exact number of underscores, and further pairs can be added. Each Base64-like run carrying one of the short patterns a dummy character always leaves in Base64 is tried, so V1 values within log text (album=… path=…, or bracketed as […]) and cells that are a single value are both decoded in place. A reading is accepted only when the decoded text contains the encoder's dummy characters: the encoder puts one before each character with probability 2/3, so a genuine value carries them, while ordinary text and random Base64 virtually never decode to them. Measured in my own synthetic test (not part of this PR): no false positives across 200,000 random Base64 strings, 20,000 synthetic log lines and 30,000 path-heavy log lines with no V1 values; detection 100% for values of 10 or more characters and 99.9% at 6. Short values can be missed (about one in three at a single character). A missed value stays as stored when another value in the same cell decodes; otherwise the cell is not reported. No tested image held a V1 value, so V1 decoding has only been tested on synthetic data built from the decompiled code.
  • Several strings in one cell. Each is decoded in place, and Scheme/key lists the key that decoded each one, in order (e.g. V3[5], V3[7] or V1, V1). A #G$E token that does not decode is left exactly as stored and the reason is recorded in Note, so nothing is dropped.
  • Forgiving index. The #G$E index is matched as 1–2 digits and its width is resolved per token, so a future key array grown past 10 entries (a two-digit index) still parses. Today's single-digit data is unaffected.

Multiple Android Users

The paths glob (*/com.sec.android.gallery3d/databases/local.db*) matches every user's copy of local.db and its -wal/-shm: user 0, user 150, user 10 and any others, under data/data, data/user/<n>, data/user_de/<n> and data_mirror. Files are read through unique_files from storagePathViews, so the duplicate storage views of one database (data/data, data/user/0, data_mirror) are read once, while a second user's own copy is kept. A trailing Android User column names the user whose app folder holds the copy when databases from more than one user are read, and is left blank otherwise, following the convention of other ALEAPP artifacts. Only local.db is read. The app's other databases are not searched; if encoded strings are found in another of them, its name can be added to paths.

Output

output_types = "standard" (HTML, TSV, LAVA and timeline via @artifact_processor). One row per log cell holding encoded strings.

Columns: Timestamp (as stored), Category (as stored), Table, Column, Row ID, Decoded (in context), Raw (obfuscated), Scheme/key, Note, Android User. The source database(s) are given on the report’s source line rather than in a column.

Timestamp and Category are the log row’s __timestamp and __category, so the timeline places each row at the log entry’s time. Both are shown as stored. In the decompiled v15.9.02.2 code, DebugLogger.insertLog writes __timestamp via TimeUtil.getIsoLocalDateTimeWithMillis, which sets no time zone, so the value is the device’s local time with no offset stored; it isn’t typed as datetime or converted for that reason. The notes cover the details, and list the DebugLogger method behind each category number.

The artifact returns a single plain list of rows, as most artifacts do, and uses no html_columns, so every column, the decoded text included, is escaped by the report writer.

Forensic Housekeeping

  • Databases are opened with the standard open_sqlite_db_readonly. When a -wal is staged beside the database, the committed contents of the write-ahead log are included, as for every other artifact.
  • The decoded text is labelled as an interpretation and the raw cell is always reported unchanged beside it.
  • Keys are embedded as an audited, version-pinned constant (as seen elsewhere in the LEAPPs) rather than derived at scan time, so the decode is reproducible and the key provenance is fixed and reviewable.

Reverse Engineering of Keys

Keys and logic were reverse-engineered from com.sec.android.gallery3d base APKs:

  • V2 sEncV2Key: from v14.0.00.10.
  • V3 sEncV3Key: from v15.9.00.52; identical in v15.1.03.15, v15.7.02.0 and v15.8.00.84.
  • V1 logic: from Logger.getEncodedString in v7.0.00.9 and v10.0.01.17; unchanged apart from the added ◆ dummy in build 1000100022.

Earlier Work

The V1 scheme was described publicly by Adrian Leong (Cheeky4n6Monkey) in January 2022, with standalone scripts for the log table in Gallery v10.x and v11.x (https://cheeky4n6monkey.blogspot.com/2022/01), (https://github.com/cheeky4n6monkey/4n6-scripts/tree/master/Samsung_Gallery3d_2022). SamsungGalleryLogEncodedStrings was developed independently using the app's Logger source and does not use that earlier code. To my knowledge, the V2 and V3 (#G$E) schemes have not been published or parsed elsewhere.

Work completed offline for new ALEAPP parser: SamsungGalleryLogEncodedStrings
@abrignoni

Copy link
Copy Markdown
Owner

Thanks @dantitto, this is good work. The decoding is right. I ran it on every image in our corpus that has Gallery's local.db and checked each decoded value against my own decode, and all of them matched. The multi-user handling works too. A few changes before I merge:

  1. Please add the log row's __timestamp as the first column, with __category after it. Without the time, the trash paths can't be placed on a timeline, and the timeline output right now uses the text log as the row's time. If the Logger code shows what time zone it writes, say so in the notes. If it doesn't, report the value as stored and write that the zone is not established. Don't type it as datetime unless you can source it.
  2. Rename the artifact so it has no / in it, for example Samsung Gallery Log Encoded Strings. The framework warns about it on every run because report file names can't hold a slash.
  3. Regenerate the four result files after these changes. They still have the old lowercase name, and input_zip_path uses Windows backslashes. Forward slashes, please.
  4. sample_data: add "cookbook_a11": "Android 11 | com.sec.android.gallery3d vc 1410400002 | 1 row". That image has a real V2 value (key 3), so V2 is no longer synthetic-only. Your anne_a15 and sharon_a14 version codes match packages.xml.
  5. Notes. Please change these so they match what the code and the data show:
    • Add: "No tested image held a V1 value. V1 decoding was tested only on synthetic data built from the decompiled code."
    • Add: "On the tested images the strings decoded with V3 keys 4 to 7 and V2 key 3. The other keys were not exercised."
    • Replace the "one to three characters ... shown as stored" part with: "A value is accepted only when the decoded text holds the dummy characters, so short values can be missed. A missed value stays as stored when another value in the same cell decodes. Otherwise the cell is not reported."
    • Add: "When both key tables give readable text for one string, the V3 reading is shown and Note lists both." The PR body says it doesn't guess, but line 208 shows the V3 reading.
    • Name the columns the output checker flags. On real data Table was always log, Column was always __log and Note was always empty. Also write "Android User is blank unless databases from more than one user are read." The checker only clears a column when the notes name its header.
    • Description: the #G$E scan covers every table, not only log, so say that, and that on the tested images the strings were only in log.__log. Drop "Secure Folder". Nothing in the data says what user 150 is, so just give the user numbers.
  6. Small cleanup: _iter_markers, decode_v1, TARGET_TABLES and TARGET_COLUMNS are never used. Remove them unless you plan to use them.

Last point: the PR body's false-positive numbers (200,000 strings and so on) come from a run that isn't in the PR. Either add the script or say they come from your own synthetic test.

@dantitto

Copy link
Copy Markdown
Contributor Author

@abrignoni Thank you for the detailed review, I hope all the points you've raised are addressed as you meant for them to be🤞. It's good to be on the contribution side of things for a change with this kind of project! I've tried to mimic the structure of your points to assist in working through them:

  1. Timestamp and category. __timestamp and __category are now the first two columns, headed "Timestamp (as stored)" and "Category (as stored)", so the timeline places each row at the log entry's time instead of "log". Both are reported as stored (text such as 2025-07-12 11:07:10.573) and are blank for a row from a table without those columns. I found the code that writes the rows in Gallery v15.9.02.2: DebugLogger.insertLog sets __timestamp to TimeUtil.getIsoLocalDateTimeWithMillis(System.currentTimeMillis()), which builds the text from java.util.Date's date and time getters and sets no time zone (the same class has a separate IsoUtcDateTime formatter that does set UTC). On that code the value is the device's local time in the zone set when the row was written, with no offset stored, so I've not typed it as datetime: converting it would need the zone in effect at each write. The notes cite this, and say the row is written on a background thread, that a zone or daylight saving change affects later rows, and that the tested images ran earlier app versions whose code I haven't reviewed. __category is the number the DebugLogger instance was created with, and the notes list the factory method for each number (2 is getDeleteInstance) as method names rather than documented meanings.

  2. Artifact name. Renamed to "Samsung Gallery Log Encoded Strings", with no slash.

  3. Results. All four regenerated with TZ=UTC. They carry the new name, and input_zip_path uses forward slashes. The old result files are deleted. The synthetic fixtures (case3 and case4) now use the real log table schema (_id, __category, __timestamp, hash, volume, __log) with synthetic timestamps, so the new columns are tested there too.

  4. sample_data. cookbook_a11 added as given. Thanks for confirming the anne_a15 and sharon_a14 version codes.

  5. Notes. Your four sentence suggestions have been added:
    a. "No tested image held a V1 value. V1 decoding was tested only on synthetic data built from the decompiled code."
    b. "On the tested images the strings decoded with V3 keys 4 to 7 and V2 key 3. The other keys were not exercised."
    c. "A value is accepted only when the decoded text holds the dummy characters, so short values can be missed. A missed value stays as stored when another value in the same cell decodes. Otherwise the cell is not reported."
    d. "When both key tables give readable text for one string, the V3 reading is shown and Note lists both."

    • Output checker. The notes now name the checker's columns: Category (as stored), Table and Column held one value on every row, and Note was empty on every row. They also say "Android User is blank unless databases from more than one user are read." I scoped the Category sentence to anne_a15 and sharon_a14 (value 2), as those are the images I could check.
    • Description. It now says the #G$E scan covers each text column of each table, and that on the tested images the strings were only in log.__log. "Secure Folder" is dropped; it gives user numbers only.
  6. Cleanup. _iter_markers, decode_v1, TARGET_TABLES and TARGET_COLUMNS are removed.

PR body. The false-positive and round-trip figures are now labelled as coming from my own synthetic test, which isn't part of the PR. The "rather than guessing" wording is replaced with what the code does: V3 shown, with Note listing both readings.

Cheers,

Dan

@abrignoni
abrignoni merged commit 6ba13f8 into abrignoni:main Nov 16, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants