This paper proposes RefPentester, a knowledge-based self-reflective framework, to solve the __T212271_____ of large-scale language model (LLM)-based automated penetration testing (AutoPT). Existing LLM-based AutoPTs underperform human experts due to knowledge imbalance, short-term planning, and hallucinations. RefPentester models the penetration testing stages as a seven-stage state machine, selects appropriate tactics and techniques for each stage, and learns from previous failures to address these issues. In the evaluation on Hack The Box's Sau machine, RefPentester demonstrates 16.7% better performance than the baseline GPT-4o, and also has higher success rates for each stage.