-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
243 lines (182 loc) · 11.7 KB
/
Copy pathindex.html
File metadata and controls
243 lines (182 loc) · 11.7 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
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="wedojava" />
<title>Binary I/O operation | 回风舞雪</title>
<meta name="description" content="I coped with character type coversion to let it looks like c type, and search bytes from *.dll to modify it. so, there is sth I learned and written down here.
# Binary I/O operation Define!
def hex_read(filepath: str) -&gt; bytearray: if os.path.isfile(filepath): with open(filepath, &#39;rb&#39;) as f: data = bytearray(f.read()) return data Usage:
dll_data = hex_read(&#39;D:\\test\\test.dll&#39;) Find bytes and replace it:
index_start = dll_data.find(b&#39;\x85\x69\xf0\x7f&#39;) index_end = index_start + 100 if index_start != -1: data_old = dll_data[index_start:index_end] data_new = replacebytes # replacebytes is the content you want to replace dll_data_new = dll_data.replace(data_old, data_new) with open(&#39;D:\\test\\test.dll&#39;, &#39;wb&#39;) as f: f.write(dll_data_new) " />
<meta name="keywords" content="Hugo, theme, zozo, huifeng" />
<link rel="shortcut icon" href="https://huifeng.me/images/favicon.ico" />
<link rel="stylesheet" type="text/css" media="screen" href="https://huifeng.me/css/normalize.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://huifeng.me/css/zozo.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://huifeng.me/css/search.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://huifeng.me/css/highlight.css" />
</head>
<body>
<div class="main animate__animated animate__fadeInDown">
<div class="nav_container animated fadeInDown">
<div class="site_nav" id="site_nav">
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/posts/">Archive</a>
</li>
<li>
<a href="/tags/">Tags</a>
</li>
<li>
<a href="/about/">About</a>
</li>
</ul>
</div>
<div class="menu_icon">
<a id="menu_icon"><i class="ri-menu-line"></i></a>
</div>
</div>
<div class="header animated fadeInDown">
<div class="site_title_container">
<div class="site_title">
<h1>
<a href="https://huifeng.me">
<span>回风舞雪</span>
</a>
</h1>
</div>
<div class="description">
<p class="sub_title">Love My Love</p>
<div class="my_socials">
<a href="https://github.com/wedojava" title="github" target="_blank"><i class="ri-github-fill"></i></a>
<a href="https://weibo.com/wedojava" title="weibo" target="_blank"><i class="ri-weibo-fill"></i></a>
<a href="https://huifeng.me/index.xml" type="application/rss+xml" title="rss" target="_blank"><i
class="ri-rss-fill"></i></a>
</div>
</div>
</div>
</div>
<div class="content">
<div class="post_page">
<div class="post animate__animated animate__fadeInDown">
<div class="post_title post_detail_title">
<h2><a href='/posts/binary-io-operation/'>Binary I/O operation</a></h2>
<span class="date">2019.07.26</span>
</div>
<div class="post_content markdown"><p>I coped with character type coversion to let it looks like c type, and search bytes from <code>*.dll</code> to modify it.
so, there is sth I learned and written down here.</p>
<h1 id="-binary-io-operation"># Binary I/O operation</h1>
<p>Define!</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">hex_read</span><span class="p">(</span><span class="n">filepath</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bytearray</span><span class="p">:</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filepath</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="k">return</span> <span class="n">data</span>
</code></pre></div><p>Usage:</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="n">dll_data</span> <span class="o">=</span> <span class="n">hex_read</span><span class="p">(</span><span class="s1">'D:</span><span class="se">\\</span><span class="s1">test</span><span class="se">\\</span><span class="s1">test.dll'</span><span class="p">)</span>
</code></pre></div><p>Find bytes and replace it:</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="n">index_start</span> <span class="o">=</span> <span class="n">dll_data</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="sa">b</span><span class="s1">'</span><span class="se">\x85\x69\xf0\x7f</span><span class="s1">'</span><span class="p">)</span>
<span class="n">index_end</span> <span class="o">=</span> <span class="n">index_start</span> <span class="o">+</span> <span class="mi">100</span>
<span class="k">if</span> <span class="n">index_start</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
<span class="n">data_old</span> <span class="o">=</span> <span class="n">dll_data</span><span class="p">[</span><span class="n">index_start</span><span class="p">:</span><span class="n">index_end</span><span class="p">]</span>
<span class="n">data_new</span> <span class="o">=</span> <span class="n">replacebytes</span> <span class="c1"># replacebytes is the content you want to replace</span>
<span class="n">dll_data_new</span> <span class="o">=</span> <span class="n">dll_data</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">data_old</span><span class="p">,</span> <span class="n">data_new</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'D:</span><span class="se">\\</span><span class="s1">test</span><span class="se">\\</span><span class="s1">test.dll'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">dll_data_new</span><span class="p">)</span>
</code></pre></div></div>
<div class="post_footer">
<div class="meta">
<div class="info">
<span class="field tags">
<i class="ri-stack-line"></i>
<a href="https://huifeng.me/tags/binary/">binary</a>
<a href="https://huifeng.me/tags/bytes/"> bytes</a>
<a href="https://huifeng.me/tags/read/"> read</a>
<a href="https://huifeng.me/tags/write/"> write</a>
<a href="https://huifeng.me/tags/python/"> python</a>
<a href="https://huifeng.me/tags/bytearray/"> bytearray</a>
</span>
</div>
</div>
</div>
</div>
<div class="doc_comments">
<div class="comments_block_title">发表评论</div>
<div id="vcomments"></div>
</div>
<link rel="stylesheet" href="https://huifeng.me/css/comments.css" />
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script type="text/javascript">
new Valine({
el: '#vcomments',
appId: 'yusajl3uDsnB888cn1ulQeKz-gzGzoHsz',
appKey: 'jYjHeNB1qVYREHe1zX4bRx7z',
placeholder: ' ',
visitor: 'true',
})
</script>
</div>
</div>
</div>
<a id="back_to_top" href="#" class="back_to_top"><i class="ri-arrow-up-s-line"></i></a>
<footer class="footer">
<div class="powered_by">
<a href="https://varkai.com">Designed by VarKai,</a>
<a href="http://www.gohugo.io/">Proudly published with Hugo</a>
</div>
<div class="footer_slogan">
<span>>_ Simple Is Correct.</span>
</div>
</footer>
<div id="fastSearch">
<input id="searchInput" tabindex="0">
<ul id="searchResults">
</ul>
</div>
<script src="/js/fuse.js"></script>
<script src="/js/fastsearch.js"></script>
<script src="https://huifeng.me/js/jquery-3.5.1.min.js"></script>
<link href="https://huifeng.me/css/fancybox.min.css" rel="stylesheet">
<script src="https://huifeng.me/js/fancybox.min.js"></script>
<script src="https://huifeng.me/js/zozo.js"></script>
<script type="text/javascript" async
src="https://cdn.bootcss.com/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\[\[', '\]\]']],
processEscapes: true,
processEnvironments: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
TeX: {
equationNumbers: { autoNumber: "AMS" },
extensions: ["AMSmath.js", "AMSsymbols.js"]
}
}
});
MathJax.Hub.Queue(function () {
var all = MathJax.Hub.getAllJax(), i;
for (i = 0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<style>
code.has-jax {
font: inherit;
font-size: 100%;
background: inherit;
border: inherit;
color: #515151;
}
</style>
</body>
</html>