Create a SharePoint custom web part to replace the search box and include all your custom logics.
At the Elements.xml file of the web part include the following section.
xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
This will register your control through feature elements. You must specify the following properties;
Id: Give an Id for the custom control. The Id name of the search control is ‘SmallSearchInputBox’ (as specified in the master page).
Sequence: Define a lowest sequence number. If you define another control with same Id and with a lowest sequence number, the lowest value will be used.
ControlClass: Specify the code behind class file (fully qualified name) of the control.
You have created the search box. Next step is to replace the OOTB search box at the master page. You need to modify the master page to complete this.
Find the DelegateControl used for the search box.
If you have specified a different name for the control, you need to give the correct Control Id instead.
Deploy the feature and check; the OOTB search box is replaced.