Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions scapy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2433,12 +2433,9 @@ def getfield(self, # type: ignore
nb_bytes = (self.size + bn - 1) // 8 + 1
w = s[:nb_bytes]

# split the substring byte by byte
_bytes = struct.unpack('!%dB' % nb_bytes, w)

b = 0
for c in range(nb_bytes):
b |= int(_bytes[c]) << (nb_bytes - c - 1) * 8
if len(w) < nb_bytes:
raise struct.error("unpack requires a buffer of %d bytes" % nb_bytes)
b = int.from_bytes(w, byteorder="big")

# get rid of high order bits
b &= (1 << (nb_bytes * 8 - bn)) - 1
Expand Down
25 changes: 25 additions & 0 deletions test/scapy/layers/ssh.uts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ assert pkts[8].pay.first_kex_packet_follows == 0

assert pkts[9].pay.e.value == 2350579254774455149352841074576538343990628078324267734527140871419932900538846241321452574779013468175018290651674793284015144587365340014962083771650859331476013596977123734998706481058518220105378857548427645559226229797476788395456646389818256564838400739135010680681163456095677232493468587230912056633743356721223955966756143014970734820639779746710511156996619195651512803235508645669051962031830043263352566212925544898655158819407252176433755590900240990111833619058714386338971655960765233885975850331922799954445954999296511309262036243757363804224821843032668273263064448847356873248470173458896243397517402866118125112555466428030166305568609671333602038983517505792245686243281766138834921907336198416449172686346486278292406454736650900489703602941311383274571253473117352192402069818356338637658276508681778175858698292872544113899589241205559671386224999719303843179598966006636814844667908804397048115462945951578163865384872376314062218622823399683168509281546378022234848416282276373248755506541244682438394426446625521247772730497030387798046748675856950435919346613694108323269457293633349708282520556429147475379754811181108827452704284587405668562299209768965780662855380191554093502874303015220051947466960323628390298028334555285261078171376959928596505395834904631983924930632066431620277301098016669514461539415396461120090857273167687140578309080011600491384868409678444601854368584606594031430672989514629489628693896623746874263590779323124144977231406480674784862894820443935735009785417326990153059454525335480905124180809168192695170554860881795940302149730125034860576014797577021907464402342887433222178995989216549376816454492040151004613910636289921361266911700572137074963622410473946132501034758965925448585513804452940921661377181371668124810916661795313840472724039889785883499146147917756012320556865741641210760458632895093416475238323450214341924898457846364890041182141641464569458439289152005646151462927271290045368143992045845833755058875621892664952349241777000175525150234301417133611325716295866942917806328460205857145603834255471170372323643072574234093090258963511137747272416302757220165305443240348220594316744411327905569373474701071080394539231361841208268626239088329642014216286141161796678306068065801822739617419769840590119143287370990841250896367782388086153939896000077437989471526045058990545907990089943059323343511158016141104461822684535344848072465778114834380180144470998703244485996404968078774187171096252472206846575112317045051585989901412734220984229769099373462781991394933642599850052765470790711255335450011529534223200229563089616493679704133500670071803056311472370457584460617950784473886654145020569892882621834458180061425761806601776138949785217977296683442504030198235793054259542236826904098257847794792743244091577002001218915723232763883537852453240602434246755006330557239933

= Decode an mpint with one bulk numeric conversion
from builtins import int as builtin_int
import scapy.fields as fields_module
from scapy.layers.ssh import Mpint

class CountingInt:
calls = 0
def __call__(self, value):
self.calls += 1
return value
def from_bytes(self, value, byteorder):
self.calls += 1
return builtin_int.from_bytes(value, byteorder)

field = Mpint(length=8).get_field("value")
counter = CountingInt()
fields_module.int = counter
try:
_, value = field.getfield(Mpint(length=8), b"\x01" * 8)
finally:
del fields_module.int

assert value == builtin_int.from_bytes(b"\x01" * 8, "big")
assert counter.calls == 1

= Check for SSH Kex DH Reply

assert isinstance(pkts[10].pay, SSHKexDHReply)
Expand Down
Loading