HMAC 개념 자료 : http://crono.tistory.com/3?category=779861
Main.java
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 | import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Main extends AppCompatActivity { private Button button; private EditText ed; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Hmac hmsha256 = new Hmac(); button = (Button)findViewById(R.id.button); ed = (EditText)findViewById(R.id.input); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View view){ String result; if (ed.toString() != ""){ result = hmsha256.hget(ed.getText().toString()); Log.d("HMAC-SHA256",result); Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); } } }); } } |
Hmac.java
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 | import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; public class Hmac { // hash 알고리즘 선택 private static final String ALGOLISM = "HmacSHA256"; // hash 암호화 key private static final String key = "nsHc6458"; public static String hget(String message) { try { // hash 알고리즘과 암호화 key 적용 Mac hasher = Mac.getInstance(ALGOLISM); hasher.init(new SecretKeySpec(key.getBytes(), ALGOLISM)); // messages를 암호화 적용 후 byte 배열 형태의 결과 리턴 byte[] hash = hasher.doFinal(message.getBytes()); return byteToString(hash); } catch (NoSuchAlgorithmException e){ e.printStackTrace(); } catch (InvalidKeyException e){ e.printStackTrace(); } return ""; } // byte[]의 값을 16진수 형태의 문자로 변환하는 함수 private static String byteToString(byte[] hash) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < hash.length; i++) { int d = hash[i]; d += (d < 0)? 256 : 0; if (d < 16) { buffer.append("0"); } buffer.append(Integer.toString(d, 16)); } return buffer.toString(); } } |
EditText 문자를 입력 받아 공유된 key를 바탕으로 sha256으로 암호화하여 hash를 반환하는 코드들로 검색 당시 많은 예제 코드에서 위에서 사용된 암호화 class 와 method를 사용하고 있었다.
하여, 위 코드를 바탕으로 HMAC 적용 App 진단 시 공략 포인트를 체크하고자 한다.
SecreKeySpec( ) class 초기화 시 Key와 hash알고리즘 정보가 인자로 전달되므로 암호화 관련 주요한 정보를 얻을 수 있는 공략 포인트가 될 것이다.
*.doFinal( ) method에서는 메시지의 byte형태의 데이터를 받아 Hash화 하는 함수로 Hook을 통해 변환될 데이터를 확인할 수 있다.
만약 진단 대상 APP에 hget( ) 과 같은 Method가 있다면 hook을 통해 입력 메세지를 얻을 수 있고, return 값을 조작하여 데이터 조작도 가능할 것이다.
return 값 조작 시에는 동일한 hash알고리즘과 Key를 이용하여 조작 메시지를 hash화하여야 한다.
hash값 조작 시 사용할 HMAC 파이썬(v2) 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import hashlib import hmac import base64 message = bytes("nshc is possible").encode('utf-8') shared_key = bytes("nsHc6458").encode('utf-8') hash = hmac.new(shared_key, message, hashlib.sha256) print hash result = hash.hexdigest() print "Hash : %s(%d)" % (result, len(result)) #print "Base64(hash) : %s" % base64.b64encode(result) |
'Mobile 진단 > Android' 카테고리의 다른 글
Frida를 이용한 훅스크립트 자동 생성 Tool 2 (0) | 2018.09.30 |
---|---|
Frida를 이용한 훅스크립트 자동 생성 Tool 1 (0) | 2018.09.15 |