Skip to content

Commit c9962a6

Browse files
update pfx extraction script
1 parent 8e577fd commit c9962a6

2 files changed

Lines changed: 45 additions & 102 deletions

File tree

docs/readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ git clone https://github.com/ByteSizedMarius/rewerse-engineering; Push-Location
1717

1818
You can also quite easily extract the pfx manually.
1919
1. [Download the apk](https://apkpure.com/de/rewe-supermarkt/de.rewe.app.mobile/download). Version does not really matter. However, pay attention to which download button you click, as many of these sites have been introducing dark patterns to get you to install their store instead.
20-
2. Rename the file from `.apk` or `.apkx` to `.zip`.
20+
2. Rename the file from `.apk` or `.apkx` to `.zip` and open the zip.
2121
- If you had an `apkx`-file: Copy `de.rewe.app.mobile.apk` out of the zip and redo step 2 with this apk.
2222
3. Navigate to `/res/raw`, where you will find the `mtls_prod.pfx`.
2323
4. Extract key and pem from the `.pfx`.
@@ -33,7 +33,7 @@ You can also quite easily extract the pfx manually.
3333
- Optional: Provide a working directory (`-WorkingDirectory <Path>`); if not provided, the current working-directory is used
3434
- Optional: Provide the apk-file to use (`-ApkFile <Path>`); if not provided:
3535
- the working directory is searched for an apk file
36-
- if not found, rewe apk ver. 4.1.0 is downloaded from uptodown.net
36+
- if not found, rewe apk ver. 5.7.3 is downloaded from uptodown.net
3737
- Optional: Just extract key/pem from `mtls_prod.pfx` in current WorkingDirectory (`-Pfx`)
3838
- Optional: Just extract key/pem from pfx at path (`-PfxPath <Path>`)
3939
- Optional: Just download the apk to the current working directory (`-Dl`). Note: File ending is always .apk, even when it is an xapk.
@@ -44,6 +44,7 @@ Apk versions tested with the script:
4444

4545
| version | tested |
4646
|---------|---------|
47+
| 5.7.3 ||
4748
| 4.1.0 ||
4849
| 4.0.3 ||
4950
| 4.0.2 ||

docs/rewerse-engineering.ps1

Lines changed: 42 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -19,60 +19,36 @@ param (
1919
# Functions
2020
# ——————————————————————————————————————————————————————————————————————————————
2121

22-
# APK download target version; no need to update unless certificates change
23-
$targetVersion = "4.1.0"
22+
# APK download target version; update if this version is no longer available on UpToDown
23+
$targetVersion = "5.7.3"
2424

2525
function Get-ApkVersionUrl {
2626
try {
2727
$response = Invoke-WebRequest -UseBasicParsing -Uri "https://rewe.en.uptodown.com/android/versions"
28-
$htmlDoc = New-Object -ComObject "HTMLFile"
29-
$htmlDoc.IHTMLDocument2_write($response.Content)
30-
31-
# Get version info
32-
$versionsDiv = $htmlDoc.getElementById("versions-items-list")
33-
if ($null -eq $versionsDiv) {
34-
Write-Error "Could not find versions list on the page"
35-
return $null
36-
}
37-
38-
$versionDivs = $versionsDiv.getElementsByTagName("div")
39-
$targetVersionDiv = $null
40-
for ($i = 0; $i -lt $versionDivs.length; $i++) {
41-
$versionElement = $versionDivs[$i].getElementsByClassName("version")
42-
if ($versionElement.length -gt 0) {
43-
$versionText = $versionElement[0].innerText
44-
45-
# Check if this is our target version
46-
if ($versionText -eq $targetVersion) {
47-
$targetVersionDiv = $versionDivs[$i]
48-
break
49-
}
50-
}
51-
}
52-
53-
# Check if target version was found
54-
if ($null -eq $targetVersionDiv) {
55-
Write-Error "Target version $targetVersion not found on the page"
56-
return $null
28+
$content = $response.Content
29+
30+
# Find div with data-url, data-version-id, data-extra-url that contains target version
31+
# HTML: <div data-url="URL" data-version-id="ID" data-extra-url="download">...<span class="version">VERSION</span>
32+
$escapedVersion = [regex]::Escape($targetVersion)
33+
if ($content -match "<div[^>]*data-url=`"([^`"]+)`"[^>]*data-version-id=`"([^`"]+)`"[^>]*data-extra-url=`"([^`"]+)`"[^>]*>[\s\S]*?<span[^>]*class=`"version`"[^>]*>$escapedVersion</span>") {
34+
$baseUrl = $Matches[1]
35+
$versionId = $Matches[2]
36+
$extraUrl = $Matches[3]
37+
return "$baseUrl/$extraUrl/$versionId"
5738
}
58-
59-
return $targetVersionDiv.getAttribute("data-url")
39+
40+
Write-Error "Target version $targetVersion not found on the page"
41+
return $null
6042
} catch {
6143
Write-Error "Error extracting download URL: $_"
6244
return $null
63-
} finally {
64-
# Release COM object
65-
if ($null -ne $htmlDoc) {
66-
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($htmlDoc) | Out-Null
67-
}
68-
[System.GC]::Collect()
6945
}
7046
}
7147

7248
function Get-ApkDownloadUrl {
7349
[CmdletBinding()]
7450
param()
75-
51+
7652
try {
7753
# Get the initial URL for the app version page
7854
$initialUrl = Get-ApkVersionUrl
@@ -81,80 +57,47 @@ function Get-ApkDownloadUrl {
8157
return $null
8258
}
8359
Write-Verbose "Initial URL: $initialUrl"
84-
60+
8561
# Request the version page
8662
$versionPageResponse = Invoke-WebRequest -UseBasicParsing -Uri $initialUrl
87-
$htmlDoc = New-Object -ComObject "HTMLFile"
88-
$htmlDoc.IHTMLDocument2_write($versionPageResponse.Content)
89-
90-
# Extract data-version
91-
$variantButton = $null
92-
$buttons = $htmlDoc.getElementsByTagName("button")
93-
for ($i = 0; $i -lt $buttons.length; $i++) {
94-
if ($buttons[$i].className -eq "button variants") {
95-
$variantButton = $buttons[$i]
96-
break
97-
}
98-
}
99-
if ($null -eq $variantButton) {
63+
$content = $versionPageResponse.Content
64+
65+
# Extract data-version from button.variants
66+
if ($content -notmatch 'class="button variants"[^>]*data-version="([^"]+)"') {
10067
Write-Error "Could not find variants button"
10168
return $null
10269
}
103-
$dataVersion = $variantButton.getAttribute("data-version")
70+
$dataVersion = $Matches[1]
10471
Write-Verbose "Data Version: $dataVersion"
105-
106-
# Extract data-code
107-
$appNameH1 = $htmlDoc.getElementById("detail-app-name")
108-
if ($null -eq $appNameH1) {
72+
73+
# Extract data-code from #detail-app-name
74+
if ($content -notmatch 'id="detail-app-name"[^>]*data-code="([^"]+)"') {
10975
Write-Error "Could not find app name element"
11076
return $null
11177
}
112-
$dataCode = $appNameH1.getAttribute("data-code")
78+
$dataCode = $Matches[1]
11379
Write-Verbose "Data Code: $dataCode"
114-
80+
11581
# Construct and request the variants URL
11682
$variantsUrl = "https://rewe.en.uptodown.com/app/$dataCode/version/$dataVersion/files"
11783
Write-Verbose "Variants URL: $variantsUrl"
11884
$variantsResponse = Invoke-WebRequest -UseBasicParsing -Uri $variantsUrl
11985
$jsonResponse = $variantsResponse.Content | ConvertFrom-Json
12086
$htmlContent = $jsonResponse.content
121-
$variantsHtmlDoc = New-Object -ComObject "HTMLFile"
122-
$variantsHtmlDoc.IHTMLDocument2_write($htmlContent)
123-
124-
# Get all divs
125-
$allDivs = $variantsHtmlDoc.getElementsByTagName("div")
126-
Write-Verbose "Found $($allDivs.length) divs total"
127-
128-
# Find first div with class "v-version" and extract URL
129-
$downloadUrl = $null
130-
for ($i = 0; $i -lt $allDivs.length; $i++) {
131-
if ($allDivs[$i].className -eq "v-version") {
132-
$versionDiv = $allDivs[$i]
133-
$version = $versionDiv.innerText
134-
135-
# Get the HTML and extract the onclick using regex
136-
$html = $versionDiv.outerHTML
137-
if ($html -match 'onclick=[''"].*?location\.href=[''"]([^''"]+)[''"]') {
138-
$downloadUrl = $Matches[1]
139-
Write-Verbose "Found version: $version with URL: $downloadUrl"
140-
break
141-
}
142-
}
87+
88+
# Extract URL from onclick in v-version div
89+
# HTML: <div class="v-version" onclick="...location.href='URL';">
90+
if ($htmlContent -match 'class="v-version"[^>]*onclick="[^"]*location\.href=''([^'']+)''') {
91+
$downloadUrl = $Matches[1]
92+
Write-Verbose "Found download URL: $downloadUrl"
93+
return $downloadUrl
14394
}
144-
145-
return $downloadUrl
95+
96+
Write-Error "Could not find download URL in variants"
97+
return $null
14698
} catch {
14799
Write-Error "Error extracting download URL: $_"
148100
return $null
149-
} finally {
150-
# Release COM objects
151-
if ($null -ne $htmlDoc) {
152-
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($htmlDoc) | Out-Null
153-
}
154-
if ($null -ne $variantsHtmlDoc) {
155-
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($variantsHtmlDoc) | Out-Null
156-
}
157-
[System.GC]::Collect()
158101
}
159102
}
160103

@@ -231,7 +174,6 @@ function Download-ReweApk {
231174
}
232175

233176
# Function to get the APK download base URL
234-
# Note: This function should be defined elsewhere or adapted as needed
235177
$BaseUrl = Get-ApkDownloadUrl
236178

237179
try {
@@ -245,9 +187,9 @@ function Download-ReweApk {
245187
return
246188
}
247189

248-
# Match the regex pattern in the response content
249-
if ($Response.Content -match 'data-url="([^"]+)"') {
250-
$DataUrl = $matches[1]
190+
# Match the download data-url (the one with data-download-version attribute)
191+
if ($Response.Content -match 'data-url="([^"]+)"[^>]*data-download-version=') {
192+
$DataUrl = $matches[1]
251193
} else {
252194
Write-Error "No data-url found in the response content."
253195
return
@@ -257,9 +199,9 @@ function Download-ReweApk {
257199
Write-Host "Starting download. Please wait..."
258200
$ApkUrl = "https://dw.uptodown.com/dwn/$DataUrl"
259201
$ApkFile = Join-Path -Path $WorkingDirectory -ChildPath "rewe.apk"
260-
$ApkFileDownload = (New-Object Net.WebClient).DownloadFile($ApkUrl, $ApkFile)
202+
Invoke-WebRequest -Uri $ApkUrl -OutFile $ApkFile -UseBasicParsing
261203
Write-Host "Done"
262-
204+
263205
return $ApkFile
264206
}
265207

0 commit comments

Comments
 (0)