Android Question How to sign Android app with system signature? [Web server]

rtek1000

Active Member
Licensed User
Longtime User
Hi,

I would like make a server socket at 80 port, but I have not access:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Webserver
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim AStreams As AsyncStreams
    Dim Server As ServerSocket
    Dim Socket1 As Socket
End Sub
Sub Globals
    Dim EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
   
    If FirstTime Then
        Server.Initialize(80, "Server")
        'Server.Initialize(5500, "Server")
        Server.Listen
        Log("MyIp = " & Server.GetMyIP)
        EditText3.Text = "My Ip:Port = " & Server.GetMyIP & ":5500"
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)
    If UserClosed Then
        Log("closing")
        AStreams.Close
        Socket1.Close
    End If
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        ToastMessageShow("Connected", False)
        Socket1 = NewSocket
         'Can only use prefix mode if both sides of the connection implement the prefix protocol!!!
        'AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
        AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
    Else
        ToastMessageShow(LastException.Message, True)
    End If
    Server.Listen
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   
    EditText2.Text = msg
    Log(msg)
   
    Dim strHTML As String
             
    strHTML = "HTTP/1.1 200 OK"  & CRLF & _
              "Content-Type: text/html" & CRLF & _
              "Connection: close"  & CRLF & _
              "Refresh: 10" & CRLF & CRLF & _
              "<!DOCTYPE HTML>"  & CRLF & _
              "<HTML>" & CRLF & _
              "<BODY>" & CRLF & _
              "Date: " & _
              DateTime.Date(DateTime.Now) & _
              " & Time: " & _
              DateTime.Time(DateTime.Now) & _
              "<br />" & CRLF & _
              "</BODY>" & CRLF & _
              "</HTML>" & CRLF
   
    Log(strHTML)
   
    Dim Buffer() As Byte
    Buffer = strHTML.GetBytes("UTF8")
    AStreams.Write(Buffer)
   
    Do While(AStreams.OutputQueueSize > 0)
       
    Loop
   
    AStreams.Close
    Socket1.Close
End Sub

Sub AStreams_Error
    ToastMessageShow(LastException.Message, True)
    Log("AStreams_Error")
End Sub

Sub AStreams_Terminated
    Log("AStreams_Terminated")

End Sub

** Activity (main) Create, isFirst = true **
main_activity_create (java line: 344)
java.net.BindException: Permission denied
at org.apache.harmony.luni.platform.OSNetworkSystem.bind(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:268)
at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:157)
at java.net.ServerSocket.<init>(ServerSocket.java:123)
at java.net.ServerSocket.<init>(ServerSocket.java:74)
at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
at com.omnicorp.servertest.main._activity_create(main.java:344)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at com.omnicorp.servertest.main.afterFirstLayout(main.java:102)
at com.omnicorp.servertest.main.access$000(main.java:17)
at com.omnicorp.servertest.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
java.net.BindException: Permission denied

I found this way to sing for Eclipse App (explain below).

How do this for B4A App?

On top of signing Android 1.6 for Dream with certificates generated by myself, I've also managed to sign my app with the platform certificate and run it with the system sharedUserId. These are the steps I took:

  • Build and flash to your Dream your own Android using http://source.android.com/documentation/building-for-dream. Use the mkkey.sh script on http://pdk.android.com/online-pdk/guide/release_keys.html to create new certificates, including x509 certificates before you do 'make'.
  • In the AndroidManifest.xml of your application: under the <manifest> element, add the attribute android:sharedUserId="android.uid.system".
  • Export an unsigned version of your Android application using Eclipse: right-click on the project >> Android Tools >> Export Unsigned Application Package.
  • Use <root-of-android-source-tree>/out/host/<your-host>/framework/signapk.jar to sign your app using platform.x509.pem and platform.pk8 in <root-of-android-source-tree>/build/target/product/security generated earlier:
java -jar signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk YourApp-signed.apk.

  • Install the app to your device:

    adb install YourApp-signed.apk
  • Run your app
  • Use adb shell ps to confirm that your app is running as system.

Source
 

Attachments

  • Web server.zip
    8.4 KB · Views: 165

rtek1000

Active Member
Licensed User
Longtime User
All right, I found this explanation:

If you are working on a real device:
  1. What you need is the vendor signature that's used to sign all the modified Android system inside the device.
  2. You won't have any system signature of any producer unless they publish it.
  3. So, you will not get the signature or the platform files unless you have the vendor/security folder of the rom inside the device.
Source

And this tip (I did this using FX explorer, but it did not work):
You should go for rooting method after your application is ready to be installed.
  • Root your phone
  • Open a terminal on your pc, go to platform-tools folder and start adb executable
  • adb push /path/to/your/apk/your_apk.apk /sdcard/Download
  • Go into adb shell
  • su
  • mount -o remount,rw /system
  • cp /sdcard/Download/your_apk.apk /system/app
  • chmod 666 /system/app/your_apk.apk
  • Reboot your phone

But I found a simple alternative for this case:

"Configure your router to forward port 80 to port 8080" (8080 or other port your app uses)
Source

Thank you.
 
Upvote 0
Top