WorkShop n°21 : Changing html on the fly automatically
TcpCatcher comes with an API to code your own java hook and change HTTP packets programmatically. The setHttpContent(..) method and the use of regular expressions will let you perform powerful programmatic changes very simply. Packets may be chunked or zipped at network level, but all of that is supported transparently by TcpCatcher.
- To begin, we are going to make a very simple change on google Home Page: replacing the title of the main button
- Import the TcpCatcher jar (which contains the interface class) into your java project and write a class implementing the CustomHook interface and that will look like this one.
Note the your class must not belong to any package in order to be plugged at runtime.
- In the Settings panel, check "Apply an automatic transformation to packets" and select your compiled .class file after clicking on the "Choose Class" button.
A message should tell you that the hook has been successfully set up.
(Keep the hook synchronous because this is a modification hook.)
Click "Apply settings and start" and visit www.google.com
- Now let's go for a little bit more complex modification : in all web pages, we are going to fill automatically the html inputs.
We are going to look for every strings looking like <input .. value="..." .. > and replace them with <input .. value="TcpCatcher" .. >. In order to achieve that, java and regular expressions happen to be very powerful.
Let's look at this simple code :
- Now plug your new hook and let's visit, for instance, FaceBook's home page, and see how every input text field have been filled:
- Finally, let's notice that if the packet's content was zipped, the setHttpContent(String newHttpContent) method won't zipped it packet again by default because we considered it is not a valuable trade off. If you want to ask TcpCatcher to zip packet again after modification you can use the setHttpContent(String newHttpContent,boolean rezip) method.
The TcpCatcherPacket's methods insertHttpHeader(String headerName, String headerValue), removeHttpHeader(String headerName), setHttpContent(String newHttpContent) should allow you to do most of the desired operations on HTTP packet but any other low level modification can be done on packet by changing it's byte array 'TcpCatcherPacket.packetbytes' especially if the protocol is not HTTP based (in that case theses previous methods won't be of any use).