Extrapulmonary tuberculosis (EPTB) is characterized by atypical clinical symptoms, difficulty in diagnosis, a high rate of disability, and a high mortality rate. Early EPTB diagnosis aids recovery. The gold standard for EPTB diagnosis needs surgery, puncture, and other invasive testing to collect a lesion sample for mycobacterium tuberculosis culture and Xpert. However, early diagnosis of EPTB has been challenging due to the lack of specificity and inability of current diagnostic methods to differentiate between active and latent EPTB infections. As a result, there is an urgent clinical need to develop new methods to improve the early detection of EPTB. In this study, we employed bioinformatics and machine learning methods to identify EPTB hallmark genes. Furthermore, we looked at the relationship between these genes and immune cell infiltration. We obtained 97 differentially expressed genes (DEGs) from the analysis. The genes were split into 14 modules by weighted gene co-expression network analysis (WGCNA). Six of the intersecting genes, GBP5, UBE2L6, IFITM3, SERPING1, C1QB, and FCGR1B, were identified as EPTB hub genes at final screening using the last absolute shrinkage and selection operator (LASSO) and random Forest. The presence of some immune cells in EPTB correlated with the expression of these genes.