Evidence indicates that the microbiome plays a significant role in HIV immunopathogenesis and associated complications. This study aimed to characterize the oral and anal microbiome of Men who have Sex with Men (MSM) and Transgender Women (TGW), with and without HIV. One hundred and thirty oral and anal DNA-derived samples were obtained from 78 participants and subjected to shotgun metagenomics sequencing for further microbiome analysis. Significant differences in the microbiome composition were found among subjects associated with HIV infection, gender, sex behavior, CD4+ T-cell counts, antiretroviral therapy (ART), and the presence of HPV-associated precancerous anal lesions. Results confirm the occurrence of oncogenic viromes in this high HIV-risk population. The oral microbiome in HIV-associated cases exhibited an enrichment of bacteria associated with periodontal disease pathogenesis. Conversely, anal bacteria showed a significant decrease in HIV-infected subjects(Coprococcus comes, Finegoldia magna, Blautia obeum, Catenibacterium mitsuokai). TGW showed enrichment in species related to sexual transmission, which concurs that most recruited TGW are or have been sex workers.Prevotella bivia and Fusobacterium gonidiaformanswere positively associated with anal precancerous lesions among HIV-infected subjects. The enrichment ofHoldemanella biformisandC. comeswas associated with detectable viral load and ART-untreated patients. Metabolic pathways were distinctly affected by predominant factors linked to sexual behavior or HIV pathogenesis. Gene family analysis identified bacterial gene signatures as potential prognostic and predictive biomarkers for HIV/AIDS-associated malignancies. Conclusions: Identified microbial features at accessible sites are potential biomarkers for predicting precancerous anal lesions and therapeutic targets for HIV immunopathogenesis.