Posted on

Get signature info

Recently I had to verify the authenticity of digital signatures contained on APK files from system apps and .zip files belonging to OTA firmware updates of a specific android device. On Windows 10, all I had to do is use the unzip command together with the keytool command.

APK files:

Zip files:

As you can see, both commands are the same and can be used in different types of files – although an apk is basically a .zip file.

Posted on

Copy file from device system folder to PC (Windows)

Here’s a simple windows batch script that has the ability of copying a file from the device’s system folder to your desktop:

Just save the above text to a text file and rename the extension to .bat and execute.

Do not end the path to your PC with a back slash, it will not work.

Replace [system app name] with the remaining path to your system app and, replace [your username] with the remaining path to your desktop or, replace with some other path on your PC.

Tested on a rooted phone only.

That’s it!

Posted on

Change specific app internet speed

I developed an app for android with the purpose of controlling the daily internet quota assigned to the user and, for this to succeed, I was asked to slow down the internet speed of some apps like facebook, youtube, etc, while maintaining full speed to other apps critical for the device and business purposes. The main reason for this is that, if the user exceeds his daily quota of internet – about 1GB per day – the system should slow down internet speed for some apps while maintaining it for others. The purpose of slowing down some of the apps, is a way of telling the user to stop using them, at least until a new quota of 1GB is assigned to him – usually on demand or the next day.

All the searches related to traffic control lead me to two well known LINUX components: TC and IPTABLES. This is the “official” description of these components:

TC: Traffic control is the name given to the sets of queuing systems and mechanisms by which packets are received and transmitted on a router. This includes deciding which (and whether) packets to accept at what rate on the input of an interface and determining which packets to transmit in what order at what rate on the output of an interface.

IPTABLES: used to set up, maintain, and inspect the tables of IPv4 packet filter rules in the Linux kernel. Several different tables may be defined. Each table contains a number of built-in chains and may also contain user-defined chains. Each chain is a list of rules which can match a set of packets. Each rule specifies what to do with a packet that matches. This is called a `target’, which may be a jump to a user-defined chain in the same table.

In other words, with TC I can create a set of rules that define the “speed” at which bytes (packets) are received on the device and, with iptables I can define which users/processes use the rules defined with TC. This is a rough explanation of my needs and obviously, you can do much more with these components. We’re not event scratching the surface here!

I decided to do a test using the device’s wifi connection and so, this is the TC script I created:

The above script, basically creates a 20kbit limit on the wlan0 interface and, two classes that I will use with iptables. This 20kbit limit will be similar to a GSM data connection speed: slow and annoying.

Next, I had to get the name of the users associated with the running processes and, do a test with YouTube (one of the apps being blocked) so, to get all the details about YouTube’s process was very easy, just ran this adb shell command:

Which returned the following details:

Capture

The string with the value u0_a99 is the user android associated with YouTube’s process. Fortunately, android creates a user for each process or you might say, for each running app.

Creating the iptables script was fairly easy:

The above commands are very easy to explain. It appends this rule to the OUTPUT chain, the -o wlan0 defines the output wifi interface, the -m owner defines the module being used and it’s options and, the final -j section specifies the target TC class being used 1:10.

I also used ip6tables here because after doing a netstat on the device, all active connections are ip6, here’e the output:

Capture

After doing this proof of concept, it was easy to develop a java solution to apply these shell scripts to the running apps.

If you want to know more about iptables, here’s the man page: http://ipset.netfilter.org/iptables.man.html

Traffic control: http://www.tldp.org/HOWTO/html_single/Traffic-Control-HOWTO/

So…that’s it!

 

Posted on

Change Android preferred network mode using AT commands

I needed a solution on how to change the preferred network mode on KitKat and Lollipop devices and, I had the “luck” of only having to target two rooted Samsung devices from the Galaxy Grand series.

My initial search lead me to a lot of stack overflow posts that mainly described solutions using code reflection of methods belonging to com.android.internal.telephony.PhoneFactory class. None of these seemed to work on more recent android versions, even if installing the app on the android folder reserved for system apps: /system/priv-app/.

One of the first clues on stack overflow was this post: http://stackoverflow.com/questions/5436251/how-to-access-setpreferrednetworktype-in-android-source

Nothing worked either: not the reflection code on the post, the app from TheMasterBaron/Toggle-2G, nothing!

A more useful clue came from this xda post: http://forum.xda-developers.com/galaxy-s2/help/how-to-talk-to-modem-commands-t1471241

The post talked among many things, of a way to execute AT commands from windows to android. I used PuTTY, made a serial COM4 connection to the device and in a few seconds, I was executing all sorts of AT commands and getting all sorts of responses. This was great but, I still had no clue on how to execute these commands on the android device itself.

Capture

I started to search about AT commands and one of the first things you learn is that they are different from device to device because different devices use different modems, etc.

A quick query of both devices using the command AT+CLAC showed a different list of supported commands and, event more surprising, in devices from the same company and series. The devices are more or less one year apart from each other in terms of release dates.

Capture2

I found a lot of documents regarding AT commands but, this one has many practical examples on how to use them: https://www.sierrawireless.com/resources/documents/support/2130617_supported_at_command_reference-v2.4.pdf

The first thing I had to know, was how to change the preferred network mode on both devices and, all the documentation found online was really helpful. Using PuTTY, I managed to change the network mode from 4G to GSM by using the following commands:

KitKat:

Lollipop:

All the network modes were easy to find by using the query version of the above commands, after changing between different network modes:
KitKat:

Lollipop:

One of the first things you got to search about, is what serial port does the modem uses: it’s on this serial port that all AT commands will be executed and all responses will be there also.

The initial search lead me to the ports ttyGS0 and smd0. The port smd0 seemed to work on KitKat and, by using this shell command I received my first OK response from android:

But, the happiness didn’t last long, I still had no clue on what port Lollipop was using so, I decided to use brute force, and executed ls /dev/ which returned a bunch of folders representing block/character devices. More info on this subject, here. I counted more or less 750 folders and, one of these folders, contained my serial port! All I had to do was find it! How? well, simple, just test them all in a simple way by trying to change the network mode, like this:

etc…

I did a search by blocks of 40 sequential commands from the 750 and almost at the end, eureka! The network mode changed! I found two serial ports that responded to the AT commands! These were the ones that did it:

The final version of the commands on both android versions, which changed the network mode from the default 4G to GSM:

KitKat:

Lollipop:

So, that’s it, I hope this helps someone.

Capture

 

 

 

Posted on

Outsourcing Nightmare

Outsourcing can be a real nightmare. If you’re lucky, you’ll be in a customer whose developer team is nice to you but in the other hand, you can find a team that is arrogant, they will make your job harder and in the end, every piece of problem that arises will be your fault and your teams fault only. The reasons? well, i think they are simple: if you do a good job, if you have all the technical skills desired, even so it will not be enough. The main reason is that the customer team fears you. They fear your knowledge is better than theirs, they fear you’ll take their job somehow.

They look at the huge piece of crap applications they build, like some kind of code virgin API wich cannot be changed unless you invite it to dinner 10 times. After 10 times dinner you can mess with it all the way!

It’s like everytime you add some pice of code to their API, it has to go to some inner workflow of approval that at the end, allways ends up approving your code but makes you waste time and, that’s the stupid part of it: they are paying for it!

This dark and obscure “workflow of approval”, usually does nothing; it doesn’t look at the code at all. It doesn’t do any benchmarking whatsoever. It’s just a waste of time created for you to understand that they are still in charge.

At the end, your code is allways approved and why? they don’t understand it! it works so…they use it!

 

Another curious thing to observe is the quality of the code you’ll be messing with. Be ready to see all kinds of
code quality. It can go from the very well crafted piece of code, to the mother of all spaghetti code you’ll ever find.
This can be explained: large teams with a variety of skills, newbies recently thrown from University, etc.

The most curious thing is that i’m talking about companies that deal with the life of thousands if not millions of people and they do it, by using software applications that still work, mainly by luck or you can say that murphy is just being distracted!

Above, IIS source code schizophrenic class relationship.