Frida Tutorial 1

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें

यह पोस्ट का सारांश है: https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1
APK: https://github.com/t0thkr1s/frida-demo/releases
स्रोत कोड: https://github.com/t0thkr1s/frida-demo

Python

Frida आपको एक चल रहे एप्लिकेशन के फ़ंक्शन के अंदर insert JavaScript code डालने की अनुमति देता है। लेकिन आप python का उपयोग करके hooks को call कर सकते हैं और यहां तक कि hooks के साथ interact भी कर सकते हैं।

यह एक आसान python स्क्रिप्ट है जिसे आप इस ट्यूटोरियल में दिए गए सभी उदाहरणों के साथ उपयोग कर सकते हैं:

#hooking.py
import frida, sys

with open(sys.argv[1], 'r') as f:
jscode = f.read()
process = frida.get_usb_device().attach('infosecadventures.fridademo')
script = process.create_script(jscode)
print('[ * ] Running Frida Demo application')
script.load()
sys.stdin.read()

स्क्रिप्ट को कॉल करें:

python hooking.py <hookN.js>

python के साथ frida का उपयोग करना उपयोगी है, लेकिन इन उदाहरणों के लिए आप command line frida tools का उपयोग करके सीधे Frida को कॉल भी कर सकते हैं:

frida -U --no-pause -l hookN.js -f infosecadventures.fridademo

Hook 1 - Boolean Bypass

यहाँ आप देख सकते हैं कि कैसे क्लास infosecadventures.fridademo.utils.PinUtil की boolean method checkPin को hook किया जा सकता है।

//hook1.js
Java.perform(function () {
console.log("[ * ] Starting implementation override...")
var MainActivity = Java.use("infosecadventures.fridademo.utils.PinUtil")
MainActivity.checkPin.implementation = function (pin) {
console.log("[ + ] PIN check successfully bypassed!")
return true
}
})
python hooking.py hook1.js

Mirar: La funcion recibe como parametro un String, no hace falta overload?

Hook 2 - Function Bruteforce

Non-Static Function

यदि आप किसी class का non-static function कॉल करना चाहते हैं, तो आपको सबसे पहले एक instance चाहिए। फिर, आप उस instance का उपयोग करके function को कॉल कर सकते हैं।\ ऐसा करने के लिए, आप कोई मौजूदा instance ढूँढ सकते हैं और उसका उपयोग कर सकते हैं:

Java.perform(function () {
console.log("[ * ] Starting PIN Brute-force, please wait...")
Java.choose("infosecadventures.fridademo.utils.PinUtil", {
onMatch: function (instance) {
console.log("[ * ] Instance found in memory: " + instance)
for (var i = 1000; i < 9999; i++) {
if (instance.checkPin(i + "") == true) {
console.log("[ + ] Found correct PIN: " + i)
break
}
}
},
onComplete: function () {},
})
})

इस मामले में यह काम नहीं कर रहा क्योंकि कोई instance मौजूद नहीं है और function Static है

Static Function

यदि function static है, तो आप इसे सीधे कॉल कर सकते हैं:

//hook2.js
Java.perform(function () {
console.log("[ * ] Starting PIN Brute-force, please wait...")
var PinUtil = Java.use("infosecadventures.fridademo.utils.PinUtil")

for (var i = 1000; i < 9999; i++) {
if (PinUtil.checkPin(i + "") == true) {
console.log("[ + ] Found correct PIN: " + i)
}
}
})

Hook 3 - Retrieving arguments and return value

आप किसी function को hook करके इसे print करवा सकते हैं ताकि यह passed arguments और return value का मान दिखाए:

//hook3.js
Java.perform(function () {
console.log("[ * ] Starting implementation override...")

var EncryptionUtil = Java.use(
"infosecadventures.fridademo.utils.EncryptionUtil"
)
EncryptionUtil.encrypt.implementation = function (key, value) {
console.log("Key: " + key)
console.log("Value: " + value)
var encrypted_ret = this.encrypt(key, value) //Call the original function
console.log("Encrypted value: " + encrypted_ret)
return encrypted_ret
}
})

Hooking हाल के Android संस्करणों (14/15/16)

  • From Frida 17.1.x+ Java hooking on Android 14–16 फिर से स्थिर है (ART quick entrypoint offsets ठीक कर दिए गए थे)। यदि Java.choose Android 14+ पर कुछ नहीं लौटाता है, तो frida-server/gadget और CLI/Python पैकेजों को >=17.1.5 में अपग्रेड करें।
  • प्रारंभिक anti-debug checks वाले ऐप्स अक्सर attach से पहले क्रैश हो जाते हैं। hooks को onCreate से पहले लोड करने के लिए spawn का उपयोग करें:
frida -U -f infosecadventures.fridademo -l hook1.js --no-pause
  • जब कई overloads मौजूद हों, तो target को स्पष्ट रूप से चुनें:
var Cls = Java.use("com.example.Class")
Cls.doThing.overload('java.lang.String', 'int').implementation = function(s, i) {
return this.doThing(s, i)
}

Zygisk Gadget के साथ अधिक Stealthy injection

कुछ ऐप्स ptrace या frida-server का पता लगा लेते हैं। Magisk/Zygisk modules Zygote के अंदर frida-gadget लोड कर सकते हैं ताकि कोई प्रोसेस ptraced न हो:

  1. एक Zygisk gadget module (उदा., zygisk-gadget) इंस्टॉल करें और रीबूट करें।
  2. लक्षित पैकेज और स्टार्टअप चेक बायपास करने के लिए वैकल्पिक देरी कॉन्फ़िगर करें:
adb shell "su -c 'echo infosecadventures.fridademo,5000 > /data/local/tmp/re.zyg.fri/target_packages'"
  1. ऐप लॉन्च करें और gadget name से attach करें:
frida -U -n Gadget -l hook3.js

क्योंकि gadget को Zygote द्वारा इंजेक्ट किया जाता है, APK की इंटीग्रिटी चेक अप्रभावित रहती है और बेसिक ptrace/Frida string checks सामान्यतः फेल हो जाते हैं।

महत्वपूर्ण

इस ट्यूटोरियल में आपने method के नाम और .implementation का उपयोग करके methods को हुक किया है। लेकिन अगर उसी नाम के साथ एक से अधिक method हों, तो आपको उस method को निर्दिष्ट करना होगा जिसे आप हुक करना चाहते हैं और तर्कों के प्रकार को संकेत करना होगा।

आप इसे the next tutorial में देख सकते हैं।

संदर्भ

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें