Aiming at the difficulty of identification and localization in the detection of contraband targets in X-ray images, as well as the demand for upgrading and deployment of security equipment during the peak flow stage, a lightweight X-ray security contraband detection algorithm is proposed and optimized for YOLOv8. Firstly, the number of channels in the original network is reduced according to a predetermined ratio, which effectively reduces model parameters and computational complexity and achieves better results than the current mainstream lightweight methods, such as model pruning and detection backbone optimization. Secondly, due to the presence of objects of various scales and sizes in X-ray security inspection images, we introduced the BiFPN feature extraction strategy in the neck. We designed a simplified version of BiFPN based on the YOLOv8 network structure to effectively integrate feature information from different levels and scales. Finally, we incorporated the idea of Focal Loss to address the issue of imbalance between positive and negative samples and proposed a new regression loss function, Focal-GIoU Loss, which combines GIoU Loss. This increases the loss weight for important but difficult-to-detect samples, such as small targets and overlapping targets, making the model more focused on these critical samples. Results show that our proposed YOLO-CBF improves mAP by 1.5%, 0.9%, and 0.5% on three datasets with different levels of occlusion in PIDray, while reducing the parameter count and computational cost from the original 3.0M and 8.1G to 2.1M and 6.3G.Moreover, it performs well in comparative experiments between different models and in generalization experiments across different datasets.