title: frida - hook socket inputstream in android apk
body: <p>I found InputStream hook code <a href="https://github.com/iddoeldor/frida-snippets#hook-io-inputstream" rel="nofollow noreferrer">here</a>! but I add below code in android, then try to hook it with the frida script, it's not been triggered:</p>
  <p>Java code (<a href="https://github.com/wjj1928/sharefolder/blob/master/SocketDemo.apk?raw=true" rel="nofollow noreferrer">SocketDemo.apk</a>):</p>
  <pre><code>  InputStream ios = sock.getInputStream();
    int len = ios.read(dataBuf);
  </code></pre>
  <p>frida script.js:</p>
  <pre><code>function buf2hex(array, readLimit) {
      var result = [];
      var ascii = &quot;&quot;
      readLimit = readLimit || array.length;
      for (var i=0; i&lt;readLimit;++i) {
          var elem = array[i] &amp; 0xFF
          var str = (0x100 + elem).toString(16).substr(1)
          result.push(str);
  
          if (elem &lt; 127 &amp;&amp; elem &gt;= 32) {
              ascii += String.fromCharCode(elem)
          } else {
              ascii += &quot;.&quot;
          }
      }
      return result.join(' ') + &quot; | &quot; + ascii
  }
  
  function hookInputStream() {
      Java.use('java.net.Socket')['connect'].overload('java.net.SocketAddress').implementation = function(sockaddr) {
          console.log(&quot;connect&quot;)
          var retval = this.connect(sockaddr)
          return retval
      }
      Java.use('java.io.InputStream')['read'].overload('[B').implementation = function(data) {
          var retval = this.read(data)
          console.log(&quot;----&quot; + data.length)
          var hexstr = buf2hex(data)
          console.log(hexstr)
          return retval
      }
  }
  
  Java.perform(hookInputStream);
  </code></pre>
  <p>command:</p>
  <pre><code>frida -Uf com.example.socketdemo -l script.js --no-pause
  </code></pre>