The WP_Query class can match IDs as well as search terms. An idea would be to use the pre_get_posts
action to detect if the search term is numeric and, if it is, set the query to work with attachments while passing the search as the ID.
function wpse223307_allow_search_by_attachment_id( $query ) {
//Only alter the query if we are in a search screen,...
if( ! is_search() ) :
return;
endif;
//...the search term has been set...
if( ! isset( $query->query_vars['s'] ) ) :
return;
endif;
$search_term = $query->query_vars['s'];
//...and the search term is a number
if( ! is_numeric( $search_term ) ) :
return;
endif;
//Set the post type and post status to work with attachments (assuming you want to exclude other post types in numeric searches)
$query->set( 'post_type', array( 'attachment' ) );
$query->set( 'post_status', array( 'inherit' ) );
//Match the search term with the attachment's ID and remove it from the query
$query->set( 'p', $search_term );
$query->set( 's', '' );
}
add_action( 'pre_get_posts', 'wpse223307_allow_search_by_attachment_id');